Skip to content

Commit 89ff46e

Browse files
committed
add content to generated conversions
1 parent f2b8613 commit 89ff46e

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

src/Casts/GeneratedConversion.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function __construct(
3535
public ?int $width = null,
3636
public ?float $aspect_ratio = null,
3737
public ?string $average_color = null,
38+
public ?string $content = null,
3839
public array $metadata = [],
3940
public Collection $generated_conversions = new Collection,
4041
?Carbon $created_at = null,
@@ -66,6 +67,7 @@ public static function make(array $attributes): self
6667
width: Arr::get($attributes, 'width'),
6768
aspect_ratio: Arr::get($attributes, 'aspect_ratio'),
6869
average_color: Arr::get($attributes, 'average_color'),
70+
content: Arr::get($attributes, 'content'),
6971
metadata: Arr::get($attributes, 'metadata', []),
7072
generated_conversions: collect(Arr::get($attributes, 'generated_conversions', []))->map(fn ($item) => self::make($item)),
7173
created_at: $created_at ? Carbon::parse($created_at) : null,

src/Jobs/MediaConversionJob.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ public function getMediaConversion(): ?MediaConversion
8484

8585
public function isNestedConversion(): bool
8686
{
87-
return count(explode('.', $this->conversionName)) > 1;
87+
return str($this->conversionName)->contains('.');
8888
}
8989

9090
public function getGeneratedParentConversion(): ?GeneratedConversion
9191
{
9292
if ($this->isNestedConversion()) {
93-
return $this->media->getGeneratedParentConversion($this->conversionName);
93+
return $this->media->getGeneratedConversion(
94+
str($this->conversionName)->beforeLast('.')->value()
95+
);
9496
}
9597

9698
return null;

src/Models/Media.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function url(): Attribute
9595
return Attribute::get(fn () => $this->getUrl());
9696
}
9797

98-
public function getConversionKey(string $conversion): string
98+
public function makeGeneratedConversionKey(string $conversion): string
9999
{
100100
return str_replace('.', '.generated_conversions.', $conversion);
101101
}
@@ -106,7 +106,10 @@ public function getConversionKey(string $conversion): string
106106
*/
107107
public function getGeneratedConversion(string $conversion, ?string $state = null): ?GeneratedConversion
108108
{
109-
$generatedConversion = data_get($this->generated_conversions, $this->getConversionKey($conversion));
109+
$generatedConversion = data_get(
110+
$this->generated_conversions,
111+
$this->makeGeneratedConversionKey($conversion)
112+
);
110113

111114
if ($state) {
112115
return $generatedConversion?->state === $state ? $generatedConversion : null;
@@ -115,14 +118,6 @@ public function getGeneratedConversion(string $conversion, ?string $state = null
115118
return $generatedConversion;
116119
}
117120

118-
public function getGeneratedParentConversion(string $conversion, ?string $state = null): ?GeneratedConversion
119-
{
120-
$genealogy = explode('.', $conversion);
121-
$parents = implode('.', array_slice($genealogy, 0, -1));
122-
123-
return $this->getGeneratedConversion($parents, $state);
124-
}
125-
126121
public function hasGeneratedConversion(string $conversion, ?string $state = null): bool
127122
{
128123
return (bool) $this->getGeneratedConversion($conversion, $state);
@@ -136,8 +131,10 @@ public function hasGeneratedConversion(string $conversion, ?string $state = null
136131
* /conversionName
137132
* files
138133
*/
139-
public function generateBasePath(?string $conversion = null): string
140-
{
134+
public function makePath(
135+
?string $conversion = null,
136+
?string $fileName = null
137+
): string {
141138
$prefix = config('media.generated_path_prefix', '');
142139

143140
$root = Str::of($prefix)
@@ -148,11 +145,12 @@ public function generateBasePath(?string $conversion = null): string
148145
if ($conversion) {
149146
return $root
150147
->append('generated_conversions/')
151-
->append(str_replace('.', '/', $this->getConversionKey($conversion)))
152-
->finish('/');
148+
->append(str_replace('.', '/', $this->makeGeneratedConversionKey($conversion)))
149+
->finish('/')
150+
->append($fileName ?? '');
153151
}
154152

155-
return $root;
153+
return $root->append($fileName ?? '');
156154
}
157155

158156
public function putGeneratedConversion(string $conversion, GeneratedConversion $generatedConversion): static
@@ -231,7 +229,7 @@ public function storeFileFromHttpFile(
231229

232230
$file = $this->performMediaTransformations($file);
233231

234-
$basePath = Str::finish($basePath ?? $this->generateBasePath(), '/');
232+
$basePath = Str::finish($basePath ?? $this->makePath(), '/');
235233

236234
$this->name = Str::limit(
237235
File::sanitizeFilename($name ?? File::name($file)),
@@ -405,7 +403,7 @@ public function storeConversionFromHttpFile(
405403
name: $name,
406404
extension: $extension,
407405
file_name: $file_name,
408-
path: Str::of($basePath ?? $this->generateBasePath($conversion))->finish('/')->append($file_name),
406+
path: Str::of($basePath ?? $this->makePath($conversion))->finish('/')->append($file_name),
409407
mime_type: $mime_type,
410408
type: $type,
411409
state: $state,
@@ -610,9 +608,10 @@ public function deleteGeneratedConversion(string $conversion): ?GeneratedConvers
610608
return null;
611609
}
612610

613-
$this->deleteGeneratedConversionFiles($conversion);
614-
$this->forgetGeneratedConversion($conversion);
615-
$this->save();
611+
$this
612+
->deleteGeneratedConversionFiles($conversion)
613+
->forgetGeneratedConversion($conversion)
614+
->save();
616615

617616
return $generatedConversion;
618617
}

tests/Feature/MediaTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@
6767
/** @var Media $media */
6868
$media = MediaFactory::new()->make();
6969

70-
expect($media->getConversionKey('poster'))->toBe('poster');
71-
expect($media->getConversionKey('poster.480p'))->toBe('poster.generated_conversions.480p');
72-
expect($media->getConversionKey('poster.square.480p'))->toBe('poster.generated_conversions.square.generated_conversions.480p');
70+
expect($media->makeGeneratedConversionKey('poster'))->toBe('poster');
71+
expect($media->makeGeneratedConversionKey('poster.480p'))->toBe('poster.generated_conversions.480p');
72+
expect($media->makeGeneratedConversionKey('poster.square.480p'))->toBe('poster.generated_conversions.square.generated_conversions.480p');
7373
});
7474

7575
it('retrieve the generated conversion', function () {

0 commit comments

Comments
 (0)