Skip to content

Commit f85826b

Browse files
authored
Merge pull request #49 from codebar-ag/feature-geturl
Feature Optimise
2 parents 16b3d77 + 682dd67 commit f85826b

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.php_cs.cache
44
.php-cs-fixer.cache
55
.phpunit.result.cache
6+
.phpunit.cache
67
build
78
composer.lock
89
coverage

src/FlysystemCloudinaryAdapter.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,30 @@ private function extractExtraMetadata(array $metadata): array
520520
return $extracted;
521521
}
522522

523-
public function getUrl(string $path): string|false
523+
/**
524+
* Get the URL of an image with optional transformation parameters
525+
*
526+
* @return string
527+
*/
528+
public function getUrl(string|array $path): string|false
529+
{
530+
$options = [];
531+
532+
if (is_array($path)) {
533+
$options = $path['options'] ?? [];
534+
$path = $path['path'];
535+
}
536+
537+
$path = $this->ensureFolderIsPrefixed(trim($path, '/'));
538+
539+
try {
540+
return (string) $this->cloudinary->image($path)->toUrl(implode(',', $options));
541+
} catch (NotFound) {
542+
return false;
543+
}
544+
}
545+
546+
public function getUrlViaRequest(string $path): string|false
524547
{
525548
$path = $this->ensureFolderIsPrefixed(trim($path, '/'));
526549

tests/Feature/AdapterTest.php

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 4);
270270
});
271271

272-
it('can get url', function () {
272+
it('can get url via request', function () {
273273
Http::fake();
274274
$mock = $this->mock(Cloudinary::class, function (MockInterface $mock) {
275275
$mock->shouldReceive('uploadApi->explicit')->once()->andReturn(new ApiResponse([
@@ -279,8 +279,77 @@
279279
});
280280
$adapter = new FlysystemCloudinaryAdapter($mock);
281281

282-
$url = $adapter->getUrl('::path::');
282+
$url = $adapter->getUrlViaRequest('::path::');
283283

284284
$this->assertSame('::secure-url::', $url);
285285
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 2);
286286
});
287+
288+
it('can get url', function () {
289+
// Secure URL
290+
291+
$cloudinary = new Cloudinary([
292+
'cloud_name' => env('CLOUDINARY_CLOUD_NAME'),
293+
'api_key' => env('CLOUDINARY_API_KEY'),
294+
'api_secret' => env('CLOUDINARY_API_SECRET'),
295+
'url' => [
296+
'secure' => true,
297+
],
298+
]);
299+
300+
$adapter = new FlysystemCloudinaryAdapter($cloudinary);
301+
302+
$url = $adapter->getUrl('::path::');
303+
304+
expect($url)
305+
->toContain('https://', '::path::')
306+
->not->toContain('http://');
307+
308+
// Unsecure URL
309+
310+
$cloudinary = new Cloudinary([
311+
'cloud_name' => env('CLOUDINARY_CLOUD_NAME'),
312+
'api_key' => env('CLOUDINARY_API_KEY'),
313+
'api_secret' => env('CLOUDINARY_API_SECRET'),
314+
'url' => [
315+
'secure' => false,
316+
],
317+
]);
318+
319+
$adapter = new FlysystemCloudinaryAdapter($cloudinary);
320+
321+
$url = $adapter->getUrl('::path::');
322+
323+
expect($url)->toContain('http://', '::path::')
324+
->not->toContain('https://');
325+
});
326+
327+
it('can get url with option', function () {
328+
// Secure URL
329+
330+
$cloudinary = new Cloudinary([
331+
'cloud_name' => env('CLOUDINARY_CLOUD_NAME'),
332+
'api_key' => env('CLOUDINARY_API_KEY'),
333+
'api_secret' => env('CLOUDINARY_API_SECRET'),
334+
'url' => [
335+
'secure' => true,
336+
],
337+
]);
338+
339+
$adapter = new FlysystemCloudinaryAdapter($cloudinary);
340+
341+
$url = $adapter->getUrl([
342+
'path' => '::path::',
343+
'options' => [
344+
'w_64',
345+
'h_64',
346+
'c_fill',
347+
'auto',
348+
],
349+
]);
350+
351+
expect($url)
352+
->toContain('https://', '::path::')
353+
->toContain('w_64', 'h_64', 'c_fill', 'auto')
354+
->not->toContain('http://');
355+
});

0 commit comments

Comments
 (0)