Skip to content

Commit ab0dc2d

Browse files
Merge pull request #30 from SlimGee/master
2 parents 98b3dbc + 45b4509 commit ab0dc2d

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ $result->getExtension(); // Get the extension of the uploaded file
9090
$result->getWidth(); // Get the width of the uploaded file
9191
$result->getHeight(); // Get the height of the uploaded file
9292
$result->getTimeUploaded(); // Get the time the file was uploaded
93+
94+
/**
95+
* You can also retrieve a url if you have a public id
96+
*/
97+
98+
$url = Storage::disk('cloudinary')->url($publicId);
9399
```
94100

95101
**Attach Files** to Laravel **Eloquent Models**:

src/CloudinaryAdapter.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace CloudinaryLabs\CloudinaryLaravel;
44

55
use Cloudinary\Cloudinary;
6+
use Cloudinary\Api\Exception\NotFound;
67
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
78
use League\Flysystem\AdapterInterface;
89
use League\Flysystem\Config;
10+
use Illuminate\Support\Str;
911

1012

1113
/**
@@ -186,7 +188,7 @@ public function delete($path)
186188
*/
187189
public function deleteDir($dirname)
188190
{
189-
$this->adminApi()->deleteResourcesByPrefix($dirname);
191+
$this->adminApi()->deleteAssetsByPrefix($dirname);
190192

191193
return true;
192194
}
@@ -253,7 +255,7 @@ public function read($path)
253255
*/
254256
public function readStream($path)
255257
{
256-
$resource = (array)$this->adminApi()->resource($path);
258+
$resource = (array)$this->adminApi()->asset($path);
257259

258260
$stream = fopen($resource['secure_url'], 'rb');
259261

@@ -275,7 +277,7 @@ public function listContents($directory = '', $hasRecursive = false)
275277
// get resources array
276278
$response = null;
277279
do {
278-
$response = (array)$this->adminApi()->resources(
280+
$response = (array)$this->adminApi()->assets(
279281
[
280282
'type' => 'upload',
281283
'prefix' => $directory,
@@ -366,7 +368,7 @@ public function getMetadata($path)
366368
*/
367369
public function getResource($path)
368370
{
369-
return (array)$this->adminApi()->resource($path);
371+
return (array)$this->adminApi()->asset($path);
370372
}
371373

372374
/**
@@ -404,4 +406,24 @@ public function getTimestamp($path)
404406
{
405407
return $this->prepareTimestamp($this->getResource($path));
406408
}
409+
410+
/**
411+
* Get the url of a file
412+
*
413+
* @param string $path
414+
*
415+
* @return string|false
416+
*/
417+
public function getUrl($path)
418+
{
419+
if ($path == '/') {
420+
return $path;
421+
}
422+
try {
423+
$resource = $this->getResource(Str::beforeLast($path, '.'));
424+
return $resource['secure_url'] ?? '';
425+
} catch (NotFound $e) {
426+
return '';
427+
}
428+
}
407429
}

tests/CloudinaryAdapterTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Orchestra\Testbench\TestCase;
4+
use Illuminate\Http\UploadedFile;
5+
use Illuminate\Database\Eloquent\Model;
6+
use CloudinaryLabs\CloudinaryLaravel\MediaAlly;
7+
use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine;
8+
use Illuminate\Support\Facades\Storage;
9+
10+
/**
11+
*
12+
*/
13+
class CloudinaryAdapterTest extends TestCase
14+
{
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
}
20+
21+
protected function getEnvironmentSetup($app)
22+
{
23+
$app['config']->set('cloudinary.cloud_url', env('CLOUDINARY_URL'));
24+
}
25+
26+
protected function getPackageProviders($app)
27+
{
28+
return ['CloudinaryLabs\CloudinaryLaravel\CloudinaryServiceProvider'];
29+
}
30+
31+
public function test_can_get_url_given_public_id()
32+
{
33+
//file already exists
34+
$this->assertEquals('https://res.cloudinary.com/dwinzyahj/image/upload/v1616774832/sample_qgqooj.jpg', Storage::disk('cloudinary')->url('sample_qgqooj.jpg'));
35+
}
36+
}

0 commit comments

Comments
 (0)