Skip to content

Commit 6568c04

Browse files
committed
🐛 Find raw and video file types
1 parent bf5d4a1 commit 6568c04

File tree

4 files changed

+68
-31
lines changed

4 files changed

+68
-31
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

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

5+
## 1.0.2 - 2021-06-24
6+
7+
- 🐛 Bug Fix: Folder prefix working correct now
8+
- 🐛 Bug Fix: List files now works with raw and image files
9+
510
## 1.0.1 - 2021-06-23
611

712
- 🐛 Bug Fix: List folders

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ Storage::disk('cloudinary')->getUrl('meow');
141141
This should increase the trust to store and retrieve your assets from the
142142
correct folder.
143143

144+
## 🔋 Rate limit gotchas
145+
146+
All files in Cloudinary are stored with a resource type. There are three kinds
147+
of it: `image`, `raw` and `video`. For example if we want to check if a video
148+
exists, we need to make up to 3 requests. Every type needs to be checked on
149+
their own with a separate request.
150+
151+
Keep this in mind because the admin API is rate limited to 500 calls per hour.
152+
153+
The package does check in following sequence:
154+
- `image` ➡️ `raw` ➡️ `video`
155+
144156
## 🔧 Configuration file
145157

146158
You can publish the config file with:

src/FlysystemCloudinaryAdapter.php

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,12 @@ public function has($path): array | bool | null
246246
{
247247
$path = $this->ensureFolderIsPrefixed(trim($path, '/'));
248248

249-
$options = [
250-
'type' => 'upload',
251-
];
252-
253249
try {
254-
$response = $this
255-
->cloudinary
256-
->uploadApi()
257-
->explicit($path, $options);
250+
$this->explicit($path);
258251
} catch (NotFound) {
259252
return false;
260253
}
261254

262-
event(new FlysystemCloudinaryResponseLog($response));
263-
264255
return true;
265256
}
266257

@@ -317,22 +308,12 @@ public function readStream($path): array | false
317308
*/
318309
protected function readObject(string $path): array | bool
319310
{
320-
$options = [
321-
'type' => 'upload',
322-
];
323-
324311
try {
325-
/** @var ApiResponse $response */
326-
$response = $this
327-
->cloudinary
328-
->uploadApi()
329-
->explicit($path, $options);
312+
$response = $this->explicit($path);
330313
} catch (NotFound) {
331314
return false;
332315
}
333316

334-
event(new FlysystemCloudinaryResponseLog($response));
335-
336317
['secure_url' => $url] = $response->getArrayCopy();
337318

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

464-
$options = [
465-
'type' => 'upload',
466-
];
467-
468445
try {
469-
/** @var ApiResponse $response */
470-
$response = $this
471-
->cloudinary
472-
->uploadApi()
473-
->explicit($path, $options);
446+
$response = $this->explicit($path);
474447
} catch (NotFound) {
475448
return false;
476449
}
@@ -489,6 +462,53 @@ public function getUrl(string $path): string | false
489462
return $url;
490463
}
491464

465+
protected function explicit(string $path): ApiResponse
466+
{
467+
$options = [
468+
'type' => 'upload',
469+
];
470+
471+
try {
472+
$options['resource_type'] = 'image';
473+
$response = $this
474+
->cloudinary
475+
->uploadApi()
476+
->explicit($path, $options);
477+
478+
event(new FlysystemCloudinaryResponseLog($response));
479+
480+
return $response;
481+
} catch (NotFound) {
482+
}
483+
484+
try {
485+
$options['resource_type'] = 'raw';
486+
$response = $this
487+
->cloudinary
488+
->uploadApi()
489+
->explicit($path, $options);
490+
491+
event(new FlysystemCloudinaryResponseLog($response));
492+
493+
return $response;
494+
} catch (NotFound) {
495+
}
496+
497+
try {
498+
$options['resource_type'] = 'video';
499+
$response = $this
500+
->cloudinary
501+
->uploadApi()
502+
->explicit($path, $options);
503+
504+
event(new FlysystemCloudinaryResponseLog($response));
505+
506+
return $response;
507+
} catch (NotFound $e) {
508+
throw $e;
509+
}
510+
}
511+
492512
protected function ensureFolderIsPrefixed(string $path): string
493513
{
494514
if (config('flysystem-cloudinary.folder')) {

tests/Feature/AdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,6 @@ public function it_can_get_url()
397397
$url = $adapter->getUrl('::path::');
398398

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

0 commit comments

Comments
 (0)