From b755cb707e42c9f5b595dbdd05268cf071d10e2d Mon Sep 17 00:00:00 2001 From: Brasdrive Date: Sun, 9 Nov 2025 22:40:09 -0400 Subject: [PATCH] 3.0.0 --- config/flysystem-offload.php | 19 +++++++++++-------- src/Media/MediaHooks.php | 34 +++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/config/flysystem-offload.php b/config/flysystem-offload.php index 9f52af0..5dae355 100644 --- a/config/flysystem-offload.php +++ b/config/flysystem-offload.php @@ -3,17 +3,20 @@ declare(strict_types=1); return [ - 'provider' => getenv('FLYSYSTEM_OFFLOAD_PROVIDER') ?: 's3', + 'provider' => getenv('FLYSYSTEM_OFFLOAD_PROVIDER') ?: 'webdav', 'visibility' => getenv('FLYSYSTEM_OFFLOAD_DEFAULT_VISIBILITY') ?: 'public', 'stream' => [ 'protocol' => getenv('FLYSYSTEM_OFFLOAD_STREAM_PROTOCOL') ?: 'flysystem', 'root_prefix' => getenv('FLYSYSTEM_OFFLOAD_STREAM_ROOT_PREFIX') ?: '', - 'host' => getenv('FLYSYSTEM_OFFLOAD_STREAM_HOST') ?: 'uploads', + 'host' => getenv('FLYSYSTEM_OFFLOAD_STREAM_HOST') ?: '', ], 'uploads' => [ - 'base_url' => getenv('FLYSYSTEM_OFFLOAD_BASE_URL') ?: 'https://your-bucket.s3.amazonaws.com', + 'base_url' => getenv('FLYSYSTEM_OFFLOAD_BASE_URL') + ?: (getenv('FLYSYSTEM_OFFLOAD_WEBDAV_BASE_URL') + ? rtrim(getenv('FLYSYSTEM_OFFLOAD_WEBDAV_BASE_URL'), '/') . '/' . trim(getenv('FLYSYSTEM_OFFLOAD_WEBDAV_PREFIX') ?: 'wordpress/uploads', '/') + : content_url('uploads')), 'delete_remote' => filter_var( getenv('FLYSYSTEM_OFFLOAD_DELETE_REMOTE') ?: 'true', FILTER_VALIDATE_BOOLEAN @@ -26,7 +29,7 @@ return [ 'admin' => [ 'enabled' => filter_var( - getenv('FLYSYSTEM_OFFLOAD_ADMIN_ENABLED') ?: 'false', + getenv('FLYSYSTEM_OFFLOAD_ADMIN_ENABLED') ?: 'true', FILTER_VALIDATE_BOOLEAN ), ], @@ -49,16 +52,16 @@ return [ 'webdav' => [ 'enabled' => filter_var( - getenv('FLYSYSTEM_OFFLOAD_WEBDAV_ENABLED') ?: 'false', + getenv('FLYSYSTEM_OFFLOAD_WEBDAV_ENABLED') ?: 'true', FILTER_VALIDATE_BOOLEAN ), - 'base_url' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_BASE_URL') ?: '', - 'endpoint' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_ENDPOINT') ?: '', + 'base_url' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_BASE_URL') ?: 'https://webdav.example.com', + 'endpoint' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_ENDPOINT') ?: getenv('FLYSYSTEM_OFFLOAD_WEBDAV_BASE_URL') ?: '', 'prefix' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_PREFIX') ?: 'wordpress/uploads', 'credentials' => [ 'username' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_USERNAME') ?: '', 'password' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_PASSWORD') ?: '', - 'auth_type' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_AUTH_TYPE') ?: null, + 'auth_type' => getenv('FLYSYSTEM_OFFLOAD_WEBDAV_AUTH_TYPE') ?: 'basic', ], 'stream' => [ 'register' => filter_var( diff --git a/src/Media/MediaHooks.php b/src/Media/MediaHooks.php index 9c97a31..9a72c77 100644 --- a/src/Media/MediaHooks.php +++ b/src/Media/MediaHooks.php @@ -22,10 +22,10 @@ final class MediaHooks { public function __construct(FilesystemOperator $filesystem, array $config) { $this->filesystem = $filesystem; $this->config = $config; - $this->protocol = (string) $config['stream']['protocol']; + $this->protocol = (string) ($config['stream']['protocol'] ?? 'flysystem'); $this->streamRootPrefix = PathHelper::normalize((string) ($config['stream']['root_prefix'] ?? '')); - $this->streamHost = PathHelper::normalize((string) ($config['stream']['host'] ?? 'uploads')) ?: 'uploads'; + $this->streamHost = PathHelper::normalize((string) ($config['stream']['host'] ?? '')); $this->s3Prefix = PathHelper::normalize((string) ($config['s3']['prefix'] ?? '')); $this->baseUrl = $this->normaliseBaseUrl((string) ($config['uploads']['base_url'] ?? content_url('uploads'))); @@ -34,6 +34,15 @@ final class MediaHooks { $this->deleteRemote = (bool) ($config['uploads']['delete_remote'] ?? true); $this->preferLocal = (bool) ($config['uploads']['prefer_local_for_missing'] ?? false); + + error_log(sprintf( + '[MediaHooks] Initialized - protocol: %s, host: %s, root_prefix: %s, base_url: %s, effective_base_url: %s', + $this->protocol, + $this->streamHost, + $this->streamRootPrefix, + $this->baseUrl, + $this->effectiveBaseUrl + )); } public function register(): void { @@ -50,7 +59,7 @@ final class MediaHooks { $normalizedSubdir = $subdir !== '' ? PathHelper::normalize($subdir) : ''; $streamSubdir = $normalizedSubdir !== '' ? '/' . $normalizedSubdir : ''; - $streamBase = sprintf('%s://%s', $this->protocol, $this->streamHost); + $streamBase = sprintf('%s://%s', $this->protocol, $this->streamHost ?: 'default'); $uploads['path'] = $streamBase . $streamSubdir; $uploads['basedir'] = $streamBase; @@ -63,6 +72,12 @@ final class MediaHooks { $uploads['flysystem_host'] = $this->streamHost; $uploads['flysystem_root_prefix'] = $this->streamRootPrefix; + error_log(sprintf( + '[MediaHooks] Upload dir filtered - path: %s, url: %s', + $uploads['path'], + $uploads['url'] + )); + return $uploads; } @@ -176,6 +191,7 @@ final class MediaHooks { private function toRemotePath(string $file): string { $segments = []; + // ✅ Solo agregar si NO están vacíos if ($this->streamRootPrefix !== '') { $segments[] = $this->streamRootPrefix; } @@ -186,7 +202,15 @@ final class MediaHooks { $segments[] = $file; - return PathHelper::join(...$segments); + $remotePath = PathHelper::join(...$segments); + + error_log(sprintf( + '[MediaHooks] toRemotePath - file: %s, remote: %s', + $file, + $remotePath + )); + + return $remotePath; } private function normaliseBaseUrl(string $baseUrl): string { @@ -220,7 +244,7 @@ final class MediaHooks { return $baseUrl; } - return $baseUrl . '/' . $pathPrefix; + return $baseUrl; } private function buildPublicUrl(string $relativePath): string {