<?php
    if ( ! defined( 'ABSPATH' ) ) exit;

    class cloudS3StorageAttach {

        public function __construct()
        {
    
        }

        static function getRelativePathByAttach($attachment) {
            $metadata = wp_get_attachment_metadata($attachment->ID);
            $base_path = dirname(get_attached_file($attachment->ID)) . '/';
            
            $original_file = '';
            if (is_array($metadata) && isset($metadata['file'])) {
                $original_file = basename($metadata['file']);
            } else {
                // fallback se 'file' non esiste
                $original_file = basename(get_attached_file($attachment->ID));
            }

            return [
                'relative_path' => self::sanitize_key(str_replace(wp_upload_dir()['basedir'], '', $base_path . $original_file)),
                'checked_en'    => get_post_meta($attachment->ID, 'disable_media_field', true) ?: false,
                'checked_ds'    => get_post_meta($attachment->ID, 'enable_media_field', true) ?: false
            ];
        }

        static function sanitize_key($key) {
            return trim($key, '/');
        }

    }
