Skip to content

Commit 8e68c4b

Browse files
committed
allow to set formatter for conversions
1 parent 8aadd3e commit 8e68c4b

File tree

4 files changed

+99
-28
lines changed

4 files changed

+99
-28
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
**/.DS_Store
12
.idea
23
.phpunit.cache
34
build
@@ -8,4 +9,4 @@ phpunit.xml
89
phpstan.neon
910
testbench.yaml
1011
vendor
11-
node_modules
12+
node_modules

src/Concerns/InteractWithFiles.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Elegantly\Media\Enums\MediaType;
1111
use Elegantly\Media\Helpers\File;
1212
use Elegantly\Media\TemporaryDirectory;
13+
use Elegantly\Media\UrlFormatters\AbstractUrlFormatter;
1314
use Illuminate\Contracts\Filesystem\Filesystem;
1415
use Illuminate\Database\Eloquent\Model;
1516
use Illuminate\Http\File as HttpFile;
@@ -36,6 +37,17 @@
3637
*/
3738
trait InteractWithFiles
3839
{
40+
/**
41+
* @return class-string<AbstractUrlFormatter>
42+
*/
43+
public function getDefaultUrlFormatter(): string
44+
{
45+
/** @var class-string<AbstractUrlFormatter> */
46+
$formatter = config()->string('media.default_url_formatter');
47+
48+
return $formatter;
49+
}
50+
3951
public function getDisk(): ?Filesystem
4052
{
4153
if (! $this->disk) {
@@ -45,13 +57,27 @@ public function getDisk(): ?Filesystem
4557
return Storage::disk($this->disk);
4658
}
4759

48-
public function getUrl(): ?string
49-
{
60+
/**
61+
* @param null|array<array-key, mixed> $parameters
62+
* @param null|class-string<AbstractUrlFormatter> $formatter
63+
*/
64+
public function getUrl(
65+
?array $parameters = null,
66+
?string $formatter = null
67+
): ?string {
5068
if (! $this->path) {
5169
return null;
5270
}
5371

54-
return $this->getDisk()?->url($this->path);
72+
$url = $this->getDisk()?->url($this->path);
73+
74+
if ($url) {
75+
$formatter ??= $this->getDefaultUrlFormatter();
76+
77+
return (new $formatter)->format($url, $parameters);
78+
}
79+
80+
return null;
5581
}
5682

5783
/**

src/Models/Media.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class Media extends Model
5555
use HasFactory;
5656

5757
use HasUuid;
58-
use InteractWithFiles;
58+
use InteractWithFiles {
59+
getUrl as traitGetUrl;
60+
}
5961

6062
/**
6163
* @var array<int, string>
@@ -643,22 +645,11 @@ public function getPath(
643645
return null;
644646
}
645647

646-
/**
647-
* @return class-string<AbstractUrlFormatter>
648-
*/
649-
public function getDefaultUrlFormatter(): string
650-
{
651-
/** @var class-string<AbstractUrlFormatter> */
652-
$formatter = config()->string('media.default_url_formatter');
653-
654-
return $formatter;
655-
}
656-
657648
/**
658649
* Retreive the url of a conversion or nested conversion
659650
* Ex: $media->getUrl('poster.480p')
660651
*
661-
* @param null|bool|string|array<int, string> $fallback
652+
* @param null|bool|string|array<int, string|bool> $fallback
662653
* @param null|array<array-key, mixed> $parameters
663654
* @param null|class-string<AbstractUrlFormatter> $formatter
664655
*/
@@ -668,20 +659,24 @@ public function getUrl(
668659
?array $parameters = null,
669660
?string $formatter = null
670661
): ?string {
662+
671663
$url = null;
672664

673665
if ($conversion) {
674-
$url = $this->getConversion($conversion)?->getUrl();
666+
$url = $this->getConversion($conversion)?->getUrl(
667+
parameters: $parameters,
668+
formatter: $formatter,
669+
);
675670
} elseif ($this->path) {
676671
/** @var null|string $url */
677-
$url = $this->getDisk()?->url($this->path);
672+
$url = $this->traitGetUrl(
673+
parameters: $parameters,
674+
formatter: $formatter
675+
);
678676
}
679677

680678
if ($url) {
681-
$formatter ??= $this->getDefaultUrlFormatter();
682-
683-
return (new $formatter)->format($url, $parameters);
684-
679+
return $url;
685680
} elseif ($fallback === true) {
686681
return $this->getUrl(
687682
parameters: $parameters,
@@ -692,11 +687,20 @@ public function getUrl(
692687
parameters: $parameters
693688
) ?? $fallback;
694689
} elseif (is_array($fallback)) {
695-
return $this->getUrl(
696-
conversion: array_shift($fallback),
697-
fallback: $fallback,
698-
parameters: $parameters
699-
);
690+
$fallbackValue = array_shift($fallback);
691+
692+
if ($fallbackValue === true) {
693+
return $this->getUrl(
694+
parameters: $parameters
695+
);
696+
} elseif (is_string($fallbackValue)) {
697+
return $this->getUrl(
698+
conversion: $fallbackValue,
699+
fallback: $fallback,
700+
parameters: $parameters
701+
);
702+
}
703+
700704
}
701705

702706
return null;

tests/Feature/MediaTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Elegantly\Media\Models\Media;
88
use Elegantly\Media\Models\MediaConversion;
99
use Elegantly\Media\Tests\Models\Test;
10+
use Elegantly\Media\UrlFormatters\CloudflareImageUrlFormatter;
11+
use Elegantly\Media\UrlFormatters\CloudflareVideoUrlFormatter;
1012
use FFMpeg\Coordinate\Dimension;
1113
use Illuminate\Http\UploadedFile;
1214
use Illuminate\Support\Facades\Storage;
@@ -308,13 +310,36 @@
308310
$media = MediaFactory::new()->make();
309311

310312
expect($media->getUrl())->toBe('/storage/{uuid}/empty.jpg');
313+
314+
});
315+
316+
it('retrieve the formatted url', function () {
317+
/** @var Media $media */
318+
$media = MediaFactory::new()->make();
319+
311320
expect($media->getUrl(
312321
parameters: [
313322
'width' => 200,
314323
'height' => 200,
315324
]
316325
))->toBe('/storage/{uuid}/empty.jpg?width=200&height=200');
317326

327+
expect($media->getUrl(
328+
parameters: [
329+
'width' => 200,
330+
'height' => 200,
331+
],
332+
formatter: CloudflareImageUrlFormatter::class
333+
))->toBe('/cdn-cgi/image/width=200,height=200//storage/{uuid}/empty.jpg');
334+
335+
expect($media->getUrl(
336+
parameters: [
337+
'width' => 200,
338+
'height' => 200,
339+
],
340+
formatter: CloudflareVideoUrlFormatter::class
341+
))->toBe('/cdn-cgi/media/width=200,height=200//storage/{uuid}/empty.jpg');
342+
318343
});
319344

320345
it('retrieve the fallback url', function () {
@@ -331,6 +356,16 @@
331356
fallback: true,
332357
))->toBe('/storage/{uuid}/empty.jpg');
333358

359+
expect($media->getUrl(
360+
conversion: 'none',
361+
fallback: ['none-2', true],
362+
))->toBe('/storage/{uuid}/empty.jpg');
363+
364+
expect($media->getUrl(
365+
conversion: 'none',
366+
fallback: ['none-2'],
367+
))->toBe(null);
368+
334369
expect($media->getUrl(
335370
conversion: 'poster',
336371
fallback: true,
@@ -361,6 +396,11 @@
361396
]
362397
))->toBe('/storage/{uuid}/conversions/poster/poster.jpg?width=200&height=200');
363398

399+
expect($media->getUrl(
400+
conversion: 'none',
401+
fallback: ['poster'],
402+
))->toBe('/storage/{uuid}/conversions/poster/poster.jpg');
403+
364404
});
365405

366406
it('store a file within a prefixed path', function () {

0 commit comments

Comments
 (0)