diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..19982ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +composer.lock +vendor \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..54cc65c --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,12 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## Unreleased + +## 1.0.0 - 2024-01-26 + +- Initial release. \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..431ad11 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Thomas Vantuycom + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..96fabaa --- /dev/null +++ b/README.md @@ -0,0 +1,75 @@ +# Flysystem adapter for Cloudinary + +This is a [Flysystem](https://flysystem.thephpleague.com/docs/) adapter for [Cloudinary](https://cloudinary.com/). Although not the first of its kind, this package strives to be the ultimate and dependable Flysystem adapter for Cloudinary. + +## Installation + +```bash +composer require thomasvantuycom/flysystem-cloudinary +``` + +## Usage + +Configure a Cloudinary client by supplying the cloud name, API key, and API secret accessible in the [Cloudinary console](https://console.cloudinary.com/pm/getting-started/). Then, pass the client to the adapter and initialize a new filesystem using the adapter. + +```php +use Cloudinary\Cloudinary; +use League\Flysystem\Filesystem; +use ThomasVantuycom\FlysystemCloudinary\CloudinaryAdapter; + +$client = new Cloudinary([ + 'cloud' => [ + 'cloud_name' => 'CLOUD_NAME', + 'api_key' => 'API_KEY', + 'api_secret' => 'API_SECRET', + ], + 'url' => [ + 'forceVersion' => false, + ], +]); + +$adapter = new CloudinaryAdapter($client); + +$filesystem = new Filesystem($adapater); +``` + +### Storing assets in a subfolder + +By default, the root folder of the filesystem corresponds to Cloudinary's root folder. If you prefer to store your assets in a subfolder on Cloudinary, you can provide a second argument to the Cloudinary adapter. + +```php +$adapter = new CloudinaryAdapter($client, 'path/to/folder'); +``` + +### Customizing mime type detection + +By default, the adapter employs `League\MimeTypeDetection\FinfoMimeTypeDetector` for mime type detection and setting the resource type accordingly. If you wish to modify this behavior, you can supply a third argument to the Cloudinary adapter, implementing `League\MimeTypeDetection\MimeTypeDetector`. + +```php +$adapter = new CloudinaryAdapter($client, '', $mimeTypeDetector); +``` + +### Enabling dynamic folders mode. + +By default, the adapter operates under the assumption that your Cloudinary cloud uses fixed folder mode. If you wish to support [dynamic folders](https://cloudinary.com/documentation/dynamic_folders), set the fourth argument to `true`. + +```php +$adapter = new CloudinaryAdapter($client, '', null, true); +``` + +## Limitations + +- The adapter heavily relies on the Cloudinary admin API to implement most of Flysystem's operations. Because the admin API has rate limits, you may run into timeouts. The Cloudinary API poses challenges in how it distinguishes between images, videos, and other assets, making the task of ensuring seamless operation across all file types quite intricate and expensive in API calls. It's important to highlight that deleting folders can be particularly resource-intensive. +- The adapter is compatible with Cloudinary's dynamic folders mode. However, it operates on the assumption that the public IDs of your assets do not include a path. + +## Testing + +```bash +composer test +``` + +The tests exhibit some flakiness due to delays in Cloudinary's upload and delete responses. + +## License + +The MIT License. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..518ac45 --- /dev/null +++ b/composer.json @@ -0,0 +1,62 @@ +{ + "name": "thomasvantuycom/flysystem-cloudinary", + "description": "Cloudinary filesystem adapter for Flysystem.", + "type": "library", + "keywords": [ + "cloudinary", + "flysystem", + "filesystem", + "storage", + "file", + "files" + ], + "license": "MIT", + "authors": [ + { + "name": "Thomas Vantuycom", + "email": "thomasvantuycom@proton.me", + "homepage": "https://thomasvantuycom.com", + "role": "Developer" + } + ], + "support": { + "email": "thomasvantuycom@proton.me", + "issues": "https://github.com/thomasvantuycom/flysystem-cloudinary/issues", + "source": "https://github.com/thomasvantuycom/flysystem-cloudinary", + "docs": "https://github.com/thomasvantuycom/flysystem-cloudinary", + "rss": "https://github.com/thomasvantuycom/flysystem-cloudinary/releases.atom" + }, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/thomasvantuycom" + } + ], + "require": { + "php": "^8.0.2", + "league/flysystem": "^3.23", + "cloudinary/cloudinary_php": "^2.12" + }, + "require-dev": { + "phpunit/phpunit": "^10.5", + "phpstan/phpstan": "^1.10", + "league/flysystem-adapter-test-utilities": "^3.21" + }, + "autoload": { + "psr-4": { + "ThomasVantuycom\\FlysystemCloudinary\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "ThomasVantuycom\\FlysystemCloudinary\\Tests\\": "tests/" + } + }, + "scripts": { + "test": "phpunit tests", + "phpstan": "phpstan analyse --level=4 src" + }, + "config": { + "sort-packages": true + } +} diff --git a/src/CloudinaryAdapter.php b/src/CloudinaryAdapter.php new file mode 100644 index 0000000..1f62534 --- /dev/null +++ b/src/CloudinaryAdapter.php @@ -0,0 +1,468 @@ +client = $client; + $this->prefixer = new PathPrefixer($prefix); + $this->mimeTypeDetector = $mimeTypeDetector ?? new FinfoMimeTypeDetector(); + $this->dynamicFolders = $dynamicFolders; + } + + public function fileExists(string $path): bool + { + try { + $resourceType = $this->pathToResourceType($path); + $publicId = $this->pathToPublicId($path, $resourceType); + $this->client->adminApi()->asset($publicId, [ + "resource_type" => $resourceType, + ]); + } catch (NotFound $e) { + return false; + } catch (Throwable $e) { + throw UnableToCheckFileExistence::forLocation($path, $e); + } + return true; + } + + public function directoryExists(string $path): bool + { + try { + $path = $this->prefixer->prefixPath($path); + $expression = "path=$path"; + $response = $this->client + ->searchFoldersApi() + ->expression($expression) + ->maxResults(1) + ->execute(); + return $response["total_count"] === 1; + } catch (Throwable $e) { + throw UnableToCheckDirectoryExistence::forLocation($path, $e); + } + } + + public function write(string $path, string $contents, Config $config): void + { + try { + $stream = Utils::streamFor($contents); + $this->writeStream($path, $stream, $config); + } catch (Throwable $e) { + throw UnableToWriteFile::atLocation($path, $e->getMessage(), $e); + } + } + + public function writeStream(string $path, $contents, Config $config): void + { + try { + $resourceType = $this->pathToResourceType($path); + $publicId = $this->pathToPublicId($path, $resourceType); + $options = [ + "filename" => $path, + "invalidate" => true, + "overwrite" => true, + "public_id" => $publicId, + "resource_type" => $resourceType, + ]; + if ($this->dynamicFolders) { + $folder = $this->pathToFolder($path); + if ($folder !== "" && $folder !== ".") { + $options["asset_folder"] = $folder; + } + } + $this->client->uploadApi()->upload($contents, $options); + } catch (Throwable $e) { + throw UnableToWriteFile::atLocation($path, $e->getMessage(), $e); + } + } + + public function read(string $path): string + { + try { + $stream = $this->readStream($path); + $contents = Utils::tryGetContents($stream); + return $contents; + } catch (Throwable $e) { + throw UnableToReadFile::fromLocation($path, $e->getMessage(), $e); + } + } + + public function readStream(string $path) + { + try { + $resourceType = $this->pathToResourceType($path); + $publicId = $this->pathToPublicId($path, $resourceType); + $url = $this->pathToUrl($publicId, $resourceType); + $contents = Utils::tryFopen($url, "rb"); + return $contents; + } catch (Throwable $e) { + throw UnableToReadFile::fromLocation($path, $e->getMessage(), $e); + } + } + + public function delete(string $path): void + { + try { + $resourceType = $this->pathToResourceType($path); + $publicId = $this->pathToPublicId($path, $resourceType); + $this->client->adminApi()->deleteAssets( + [$publicId], + [ + "invalidate" => true, + "resource_type" => $resourceType, + ] + ); + } catch (Throwable $e) { + throw UnableToDeleteFile::atLocation($path, $e->getMessage(), $e); + } + } + + public function deleteDirectory(string $path): void + { + try { + $path = $this->prefixer->prefixPath($path); + if ($this->dynamicFolders) { + $resources = []; + $response = null; + do { + $response = $this->client + ->searchApi() + ->expression("asset_folder=\"$path/*\"") + ->maxResults(500) + ->nextCursor($response["next_cursor"] ?? null) + ->execute(); + array_push($resources, ...$response["resources"]); + } while (isset($response["next_cursor"])); + foreach ([AssetType::IMAGE, AssetType::VIDEO, AssetType::RAW] as $resourceType) { + $resourcesOfType = array_filter( + $resources, + fn($resource) => $resource["resource_type"] === $resourceType + ); + for ($i = 0; $i < count($resourcesOfType); $i += 100) { + $this->client + ->adminApi() + ->deleteAssets( + array_map( + fn($resource) => $resource["public_id"], + array_slice($resources, $i, $i + 100) + ), + [ + "invalidate" => true, + "resource_type" => $resourceType, + ] + ); + } + } + } else { + foreach ([AssetType::IMAGE, AssetType::VIDEO, AssetType::RAW] as $resourceType) { + $response = null; + do { + $response = $this->client->adminApi()->deleteAssetsByPrefix("$path/", [ + "invalidate" => true, + "next_cursor" => $response["next_cursor"] ?? null, + "resource_type" => $resourceType, + ]); + } while (isset($response["next_cursor"])); + } + } + $this->client->adminApi()->deleteFolder($path); + } catch (Throwable $e) { + throw UnableToDeleteDirectory::atLocation($path, $e->getMessage(), $e); + } + } + + public function createDirectory(string $path, Config $config): void + { + try { + $path = $this->prefixer->prefixPath($path); + $this->client->adminApi()->createFolder($path); + } catch (Throwable $e) { + throw UnableToCreateDirectory::atLocation($path, $e->getMessage(), $e); + } + } + + public function setVisibility(string $path, string $visibility): void + { + throw UnableToSetVisibility::atLocation( + $path, + "Cloudinary does not support this operation." + ); + } + + public function visibility(string $path): FileAttributes + { + try { + $resourceType = $this->pathToResourceType($path); + $publicId = $this->pathToPublicId($path, $resourceType); + $this->client->adminApi()->asset($publicId, [ + "resource_type" => $resourceType, + ]); + return new FileAttributes($path, null, Visibility::PUBLIC); + } catch (Throwable $e) { + throw UnableToRetrieveMetadata::visibility($path, $e->getMessage(), $e); + } + } + + public function mimeType(string $path): FileAttributes + { + try { + $resourceType = $this->pathToResourceType($path); + $publicId = $this->pathToPublicId($path, $resourceType); + $response = $this->client->adminApi()->asset($publicId, [ + "resource_type" => $resourceType, + ]); + $detector = new FinfoMimeTypeDetector(); + $mimeType = $detector->detectMimeTypeFromPath($path); + if ($mimeType === null) { + throw UnableToRetrieveMetadata::mimeType($path); + } + return new FileAttributes($path, null, null, null, $mimeType); + } catch (Throwable $e) { + throw UnableToRetrieveMetadata::mimeType($path, $e->getMessage(), $e); + } + } + + public function lastModified(string $path): FileAttributes + { + try { + $resourceType = $this->pathToResourceType($path); + $publicId = $this->pathToPublicId($path, $resourceType); + $response = $this->client->adminApi()->asset($publicId, [ + "resource_type" => $resourceType, + ]); + $lastModified = strtotime( + $response["last_updated"]["updated_at"] ?? $response["created_at"] + ); + return new FileAttributes($path, null, null, $lastModified); + } catch (Throwable $e) { + throw UnableToRetrieveMetadata::lastModified($path, $e->getMessage(), $e); + } + } + + public function fileSize(string $path): FileAttributes + { + try { + $resourceType = $this->pathToResourceType($path); + $publicId = $this->pathToPublicId($path, $resourceType); + $response = $this->client->adminApi()->asset($publicId, [ + "resource_type" => $resourceType, + ]); + $fileSize = $response["bytes"]; + return new FileAttributes($path, $fileSize); + } catch (Throwable $e) { + throw UnableToRetrieveMetadata::fileSize($path, $e->getMessage(), $e); + } + } + + public function listContents(string $path, bool $deep): iterable + { + try { + $path = $this->prefixer->prefixPath($path); + $path = trim($path, "/"); + $originalPath = $path; + if ($path === "" || $path === ".") { + $expression = $deep ? "" : "folder=\"\""; + } else { + $expression = $deep ? "folder=\"$path/*\"" : "folder=\"$path\""; + } + $response = null; + do { + $response = $this->client + ->searchApi() + ->expression($expression) + ->maxResults(500) + ->nextCursor($response["next_cursor"] ?? null) + ->execute(); + foreach ($response["resources"] as $resource) { + $path = + $resource["resource_type"] === "raw" + ? $resource["public_id"] + : $resource["public_id"] . "." . $resource["format"]; + if ($this->dynamicFolders && $resource["asset_folder"] !== "") { + $path = $resource["asset_folder"] . "/" . $path; + } + $path = $this->prefixer->stripPrefix($path); + $filesize = $resource["bytes"]; + $visibility = $resource["access_mode"] === "public" ? "public" : "private"; + $lastModified = strtotime( + $resource["last_updated"]["updated_at"] ?? $resource["created_at"] + ); + $detector = new FinfoMimeTypeDetector(); + $mimeType = $detector->detectMimeTypeFromPath($path); + yield new FileAttributes( + $path, + $filesize, + $visibility, + $lastModified, + $mimeType + ); + } + } while (isset($response["next_cursor"])); + + $path = $originalPath; + if ($deep) { + if ($path === "" || $path === "/" || $path === ".") { + $expression = ""; + } else { + $expression = "path=\"$path/*\""; + } + $response = null; + do { + $response = $this->client + ->searchFoldersApi() + ->expression($expression) + ->maxResults(500) + ->nextCursor($response["next_cursor"] ?? null) + ->execute(); + foreach ($response["folders"] as $resource) { + $path = $this->prefixer->stripPrefix($resource["path"]); + yield new DirectoryAttributes($path); + } + } while (isset($response["next_cursor"])); + } else { + $response = null; + do { + $response = $this->client->adminApi()->subFolders($path, [ + "next_cursor" => $response["next_cursor"] ?? null, + ]); + foreach ($response["folders"] as $resource) { + $path = $this->prefixer->stripPrefix($resource["path"]); + yield new DirectoryAttributes($path); + } + } while (isset($response["next_cursor"])); + } + } catch (Throwable $e) { + throw UnableToListContents::atLocation($path, $deep, $e); + } + } + + public function move(string $source, string $destination, Config $config): void + { + try { + $resourceType = $this->pathToResourceType($source); + $publicId = $this->pathToPublicId($source, $resourceType); + $newResourceType = $this->pathToResourceType($destination); + $newPublicId = $this->pathToPublicId($destination, $newResourceType); + $options = [ + "invalidate" => true, + "overwrite" => true, + "resource_type" => $newResourceType, + ]; + if ($this->dynamicFolders) { + $folder = $this->pathToFolder($destination); + if ($folder !== "" && $folder !== ".") { + $options["asset_folder"] = $folder; + } + } + $this->client->uploadApi()->rename($publicId, $newPublicId, $options); + } catch (Throwable $e) { + throw UnableToMoveFile::fromLocationTo($source, $destination, $e); + } + } + + public function copy(string $source, string $destination, Config $config): void + { + try { + $resourceType = $this->pathToResourceType($source); + $publicId = $this->pathToPublicId($source, $resourceType); + $url = $this->pathToUrl($publicId, $resourceType); + $newResourceType = $this->pathToResourceType($destination); + $newPublicId = $this->pathToPublicId($destination, $newResourceType); + $options = [ + "overwrite" => true, + "public_id" => $newPublicId, + "resource_type" => $newResourceType, + ]; + if ($this->dynamicFolders) { + $folder = $this->pathToFolder($destination); + if ($folder !== "" && $folder !== ".") { + $options["asset_folder"] = $folder; + } + } + $this->client->uploadApi()->upload($url, $options); + } catch (Throwable $e) { + throw UnableToMoveFile::fromLocationTo($source, $destination, $e); + } + } + + private function pathToUrl(string $publicId, string $resourceType): string + { + return $this->client->$resourceType($publicId)->toUrl(); + } + + private function pathToPublicId(string $path, string $resourceType): string + { + $path = $this->prefixer->prefixPath($path); + // For resources of type 'raw', the extension is included in the Public ID. + if ($resourceType === AssetType::RAW) { + return $this->dynamicFolders ? pathinfo($path, PATHINFO_BASENAME) : $path; + } + + // For resources of type 'image' or 'video', the extension is excluded from the Public ID. + $pathInfo = pathinfo($path); + $dirname = $pathInfo["dirname"]; + $filename = $pathInfo["filename"]; + return $dirname !== "." && !$this->dynamicFolders ? "$dirname/$filename" : $filename; + } + + private function pathToResourceType(string $path): string + { + $mimeType = $this->mimeTypeDetector->detectMimeTypeFromPath($path); + + if ($mimeType === null) { + return AssetType::RAW; + } + + switch (true) { + case str_starts_with($mimeType, "image/"): + return AssetType::IMAGE; + case str_starts_with($mimeType, "video/"): + case str_starts_with($mimeType, "audio/"): + return AssetType::VIDEO; + default: + return AssetType::RAW; + } + } + + private function pathToFolder(string $path): string + { + $path = $this->prefixer->prefixPath($path); + return pathinfo($path, PATHINFO_DIRNAME); + } +} diff --git a/tests/CloudinaryAdapterTest.php b/tests/CloudinaryAdapterTest.php new file mode 100644 index 0000000..27ccf4d --- /dev/null +++ b/tests/CloudinaryAdapterTest.php @@ -0,0 +1,56 @@ + [ + 'cloud_name' => getenv('CLOUDINARY_CLOUD_NAME'), + 'api_key' => getenv('CLOUDINARY_API_KEY'), + 'api_secret' => getenv('CLOUDINARY_API_SECRET'), + ], + "url" => [ + "analytics" => false, + "forceVersion" => false, + ], + ]); + + return new CloudinaryAdapter($client); + } + + public function writing_a_file_with_an_empty_stream(): void + { + $this->markTestSkipped("Cloudinary doesn't support empty files."); + } + + public function overwriting_a_file(): void + { + $this->runScenario(function () { + $this->givenWeHaveAnExistingFile("path.txt", "contents"); + $adapter = $this->adapter(); + + $adapter->write("path.txt", "new contents", new Config()); + + $contents = $adapter->read("path.txt"); + $this->assertEquals("new contents", $contents); + }); + } + + public function setting_visibility(): void + { + $this->markTestSkipped("Cloudinary doesn't support setting visibility."); + } + + public function setting_visibility_on_a_file_that_does_not_exist(): void + { + $this->markTestSkipped("Cloudinary doesn't support setting visibility."); + } +}