laravel-cloudinary
laravel-cloudinary copied to clipboard
(Question) Usage with `statamic/cms` and `statamic/eloquent-driver`
Hey @yoelpc4, we have a usecase where we need to integrate Cloudinary asset management with Statamic. Usually this is trivial because you just pass it a filesystem disk and it will figure out the rest. This is unfortunately not the case with this package. I managed to get it working by doing two things:
- Update
listContents: By default, only assets are fetched. Our assets are all within folders in Cloudinary, so nothing shows up. This can be fixed with the following:
public function listContents(string $path, bool $deep): iterable
{
$resources = [];
$response = null;
$foldersResponse = (array) $this->adminApi->subFolders($path); // <- this
$allFolders = $foldersResponse['folders'];
try {
do {
$response = (array) $this->adminApi->assets([
'type' => 'upload',
'prefix' => $path,
'max_results' => 500,
'next_cursor' => $response['next_cursor'] ?? null,
]);
$resources = array_merge($resources, $response['resources']);
} while (array_key_exists('next_cursor', $response));
$allFiles = array_map(fn(array $resource) => $this->getFileAttributes($resource), $resources);
// merge files with directories
$allDirectories = array_map(fn(array $folder) => new DirectoryAttributes($folder['path']), $allFolders);
return [
...$allFiles,
...$allDirectories,
];
} catch (Throwable $e) {
throw UnableToListContents::fromLocation($path, $e->getMessage(), $e);
}
}
- Statamic requires us to keep a record of all assets in the database (because we also use
statamic/eloquent-driver). IngetFileAttributes, theFileAttributesis constructed with the path just being the$asset['public_id'], which has no file extension included by default. It seems though as if the Statamic package tries to parse the file extension from the$pathproperty, resulting in a faulty extension that we save to the database. This can easily be fixed by appending$asset['format']when creating the$path. However I'm concerned that this will break existing projects that depend on your package, however we might be able to make it somehow configurable? Are there any implications that I didn't think of?
Thanks in advance! I'd be more than happy to contribute the PR for that :)