Skip to content

Commit c6d0364

Browse files
committed
feat: get asset url using storage facade url method
1 parent 696f928 commit c6d0364

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/CloudinaryAdapter.php

Lines changed: 22 additions & 0 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
/**
@@ -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/CloudinaryAdpterTest.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)