From 917c25c99be037ddf3c06c3bcc122275915231fb Mon Sep 17 00:00:00 2001 From: Brasdrive Date: Sun, 9 Nov 2025 16:28:21 -0400 Subject: [PATCH] 3.0.0 --- flysystem-offload.php | 2 +- src/Filesystem/Adapters/WebdavAdapter.php | 58 +++++++++++++++++++---- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/flysystem-offload.php b/flysystem-offload.php index efe7ab8..64484a0 100644 --- a/flysystem-offload.php +++ b/flysystem-offload.php @@ -31,7 +31,7 @@ if (file_exists($autoload)) { } use FlysystemOffload\Plugin; -use Throwable; +// use Throwable; add_action('plugins_loaded', static function (): void { if (! class_exists(Plugin::class)) { diff --git a/src/Filesystem/Adapters/WebdavAdapter.php b/src/Filesystem/Adapters/WebdavAdapter.php index 401268f..56f5813 100644 --- a/src/Filesystem/Adapters/WebdavAdapter.php +++ b/src/Filesystem/Adapters/WebdavAdapter.php @@ -6,6 +6,8 @@ namespace FlysystemOffload\Filesystem\Adapters; use InvalidArgumentException; use League\Flysystem\FilesystemAdapter; +use League\Flysystem\UnixVisibility\PortableVisibilityConverter; +use League\Flysystem\Visibility; use League\Flysystem\WebDAV\WebDAVAdapter; use Sabre\DAV\Client; @@ -13,20 +15,20 @@ final class WebdavAdapter { public function createAdapter(array $config): FilesystemAdapter { - $webdav = $config['webdav'] ?? null; - - if (!is_array($webdav)) { + if (!isset($config['webdav']) || !is_array($config['webdav'])) { throw new InvalidArgumentException('La configuración de WebDAV no está definida.'); } - $endpoint = $this->requiredString($webdav, 'endpoint'); + $webdav = $config['webdav']; + + $endpoint = $this->requiredString($webdav, 'endpoint', 'webdav.endpoint'); $credentials = $webdav['credentials'] ?? []; $username = $this->requiredString($credentials, 'username', 'webdav.credentials.username'); $password = $this->requiredString($credentials, 'password', 'webdav.credentials.password'); $clientConfig = [ - 'baseUri' => $this->normaliseEndpoint($endpoint), + 'baseUri' => $this->normaliseEndpoint($endpoint), 'userName' => $username, 'password' => $password, ]; @@ -36,6 +38,18 @@ final class WebdavAdapter $clientConfig['authType'] = $authType; } + if (!empty($webdav['default_headers']) && is_array($webdav['default_headers'])) { + $clientConfig['headers'] = $this->normaliseHeaders($webdav['default_headers']); + } + + if (!empty($webdav['timeout'])) { + $clientConfig['timeout'] = (int) $webdav['timeout']; + } + + if (!empty($webdav['curl_options']) && is_array($webdav['curl_options'])) { + $clientConfig['curl.options'] = $webdav['curl_options']; + } + $client = new Client($clientConfig); $prefix = ''; @@ -43,14 +57,20 @@ final class WebdavAdapter $prefix = trim((string) $webdav['prefix'], '/'); } - return new WebDAVAdapter($client, $prefix); + $defaultVisibility = $webdav['default_visibility'] + ?? ($config['visibility'] ?? Visibility::PRIVATE); + + $visibility = $this->normaliseVisibility($defaultVisibility); + $visibilityConverter = PortableVisibilityConverter::fromArray([], $visibility); + + return new WebDAVAdapter($client, $prefix, $visibilityConverter); } private function requiredString(array $config, string $key, ?string $path = null): string { $value = $config[$key] ?? null; - if (!is_string($value) || $value === '') { + if (!is_string($value) || trim($value) === '') { $path ??= $key; throw new InvalidArgumentException(sprintf( @@ -64,6 +84,28 @@ final class WebdavAdapter private function normaliseEndpoint(string $endpoint): string { - return rtrim($endpoint, '/') . '/'; + return rtrim(trim($endpoint), '/') . '/'; + } + + private function normaliseHeaders(array $headers): array + { + $normalised = []; + + foreach ($headers as $header => $value) { + if (!is_string($header) || $header === '') { + continue; + } + + $normalised[$header] = is_array($value) ? implode(',', $value) : (string) $value; + } + + return $normalised; + } + + private function normaliseVisibility(string $visibility): string + { + return strtolower($visibility) === Visibility::PUBLIC + ? Visibility::PUBLIC + : Visibility::PRIVATE; } }