Skip to content

Commit

Permalink
🐛 Find raw and video file types
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslansteiger committed Jun 24, 2021
1 parent bf5d4a1 commit 6568c04
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `laravel-cloudinary` will be documented in this file.

## 1.0.2 - 2021-06-24

- 🐛 Bug Fix: Folder prefix working correct now
- 🐛 Bug Fix: List files now works with raw and image files

## 1.0.1 - 2021-06-23

- 🐛 Bug Fix: List folders
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ Storage::disk('cloudinary')->getUrl('meow');
This should increase the trust to store and retrieve your assets from the
correct folder.

## 🔋 Rate limit gotchas

All files in Cloudinary are stored with a resource type. There are three kinds
of it: `image`, `raw` and `video`. For example if we want to check if a video
exists, we need to make up to 3 requests. Every type needs to be checked on
their own with a separate request.

Keep this in mind because the admin API is rate limited to 500 calls per hour.

The package does check in following sequence:
- `image` ➡️ `raw` ➡️ `video`

## 🔧 Configuration file

You can publish the config file with:
Expand Down
80 changes: 50 additions & 30 deletions src/FlysystemCloudinaryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,12 @@ public function has($path): array | bool | null
{
$path = $this->ensureFolderIsPrefixed(trim($path, '/'));

$options = [
'type' => 'upload',
];

try {
$response = $this
->cloudinary
->uploadApi()
->explicit($path, $options);
$this->explicit($path);
} catch (NotFound) {
return false;
}

event(new FlysystemCloudinaryResponseLog($response));

return true;
}

Expand Down Expand Up @@ -317,22 +308,12 @@ public function readStream($path): array | false
*/
protected function readObject(string $path): array | bool
{
$options = [
'type' => 'upload',
];

try {
/** @var ApiResponse $response */
$response = $this
->cloudinary
->uploadApi()
->explicit($path, $options);
$response = $this->explicit($path);
} catch (NotFound) {
return false;
}

event(new FlysystemCloudinaryResponseLog($response));

['secure_url' => $url] = $response->getArrayCopy();

try {
Expand Down Expand Up @@ -461,16 +442,8 @@ public function getUrl(string $path): string | false
{
$path = $this->ensureFolderIsPrefixed(trim($path, '/'));

$options = [
'type' => 'upload',
];

try {
/** @var ApiResponse $response */
$response = $this
->cloudinary
->uploadApi()
->explicit($path, $options);
$response = $this->explicit($path);
} catch (NotFound) {
return false;
}
Expand All @@ -489,6 +462,53 @@ public function getUrl(string $path): string | false
return $url;
}

protected function explicit(string $path): ApiResponse
{
$options = [
'type' => 'upload',
];

try {
$options['resource_type'] = 'image';
$response = $this
->cloudinary
->uploadApi()
->explicit($path, $options);

event(new FlysystemCloudinaryResponseLog($response));

return $response;
} catch (NotFound) {
}

try {
$options['resource_type'] = 'raw';
$response = $this
->cloudinary
->uploadApi()
->explicit($path, $options);

event(new FlysystemCloudinaryResponseLog($response));

return $response;
} catch (NotFound) {
}

try {
$options['resource_type'] = 'video';
$response = $this
->cloudinary
->uploadApi()
->explicit($path, $options);

event(new FlysystemCloudinaryResponseLog($response));

return $response;
} catch (NotFound $e) {
throw $e;
}
}

protected function ensureFolderIsPrefixed(string $path): string
{
if (config('flysystem-cloudinary.folder')) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,6 @@ public function it_can_get_url()
$url = $adapter->getUrl('::path::');

$this->assertSame('::secure-url::', $url);
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 1);
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 2);
}
}

0 comments on commit 6568c04

Please sign in to comment.