Skip to content

Commit

Permalink
add content to generated conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Sep 29, 2024
1 parent f2b8613 commit 89ff46e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/Casts/GeneratedConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(
public ?int $width = null,
public ?float $aspect_ratio = null,
public ?string $average_color = null,
public ?string $content = null,
public array $metadata = [],
public Collection $generated_conversions = new Collection,
?Carbon $created_at = null,
Expand Down Expand Up @@ -66,6 +67,7 @@ public static function make(array $attributes): self
width: Arr::get($attributes, 'width'),
aspect_ratio: Arr::get($attributes, 'aspect_ratio'),
average_color: Arr::get($attributes, 'average_color'),
content: Arr::get($attributes, 'content'),
metadata: Arr::get($attributes, 'metadata', []),
generated_conversions: collect(Arr::get($attributes, 'generated_conversions', []))->map(fn ($item) => self::make($item)),
created_at: $created_at ? Carbon::parse($created_at) : null,
Expand Down
6 changes: 4 additions & 2 deletions src/Jobs/MediaConversionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ public function getMediaConversion(): ?MediaConversion

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

public function getGeneratedParentConversion(): ?GeneratedConversion
{
if ($this->isNestedConversion()) {
return $this->media->getGeneratedParentConversion($this->conversionName);
return $this->media->getGeneratedConversion(
str($this->conversionName)->beforeLast('.')->value()
);
}

return null;
Expand Down
39 changes: 19 additions & 20 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function url(): Attribute
return Attribute::get(fn () => $this->getUrl());
}

public function getConversionKey(string $conversion): string
public function makeGeneratedConversionKey(string $conversion): string
{
return str_replace('.', '.generated_conversions.', $conversion);
}
Expand All @@ -106,7 +106,10 @@ public function getConversionKey(string $conversion): string
*/
public function getGeneratedConversion(string $conversion, ?string $state = null): ?GeneratedConversion
{
$generatedConversion = data_get($this->generated_conversions, $this->getConversionKey($conversion));
$generatedConversion = data_get(
$this->generated_conversions,
$this->makeGeneratedConversionKey($conversion)
);

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

public function getGeneratedParentConversion(string $conversion, ?string $state = null): ?GeneratedConversion
{
$genealogy = explode('.', $conversion);
$parents = implode('.', array_slice($genealogy, 0, -1));

return $this->getGeneratedConversion($parents, $state);
}

public function hasGeneratedConversion(string $conversion, ?string $state = null): bool
{
return (bool) $this->getGeneratedConversion($conversion, $state);
Expand All @@ -136,8 +131,10 @@ public function hasGeneratedConversion(string $conversion, ?string $state = null
* /conversionName
* files
*/
public function generateBasePath(?string $conversion = null): string
{
public function makePath(
?string $conversion = null,
?string $fileName = null
): string {
$prefix = config('media.generated_path_prefix', '');

$root = Str::of($prefix)
Expand All @@ -148,11 +145,12 @@ public function generateBasePath(?string $conversion = null): string
if ($conversion) {
return $root
->append('generated_conversions/')
->append(str_replace('.', '/', $this->getConversionKey($conversion)))
->finish('/');
->append(str_replace('.', '/', $this->makeGeneratedConversionKey($conversion)))
->finish('/')
->append($fileName ?? '');
}

return $root;
return $root->append($fileName ?? '');
}

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

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

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

$this->name = Str::limit(
File::sanitizeFilename($name ?? File::name($file)),
Expand Down Expand Up @@ -405,7 +403,7 @@ public function storeConversionFromHttpFile(
name: $name,
extension: $extension,
file_name: $file_name,
path: Str::of($basePath ?? $this->generateBasePath($conversion))->finish('/')->append($file_name),
path: Str::of($basePath ?? $this->makePath($conversion))->finish('/')->append($file_name),
mime_type: $mime_type,
type: $type,
state: $state,
Expand Down Expand Up @@ -610,9 +608,10 @@ public function deleteGeneratedConversion(string $conversion): ?GeneratedConvers
return null;
}

$this->deleteGeneratedConversionFiles($conversion);
$this->forgetGeneratedConversion($conversion);
$this->save();
$this
->deleteGeneratedConversionFiles($conversion)
->forgetGeneratedConversion($conversion)
->save();

return $generatedConversion;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
/** @var Media $media */
$media = MediaFactory::new()->make();

expect($media->getConversionKey('poster'))->toBe('poster');
expect($media->getConversionKey('poster.480p'))->toBe('poster.generated_conversions.480p');
expect($media->getConversionKey('poster.square.480p'))->toBe('poster.generated_conversions.square.generated_conversions.480p');
expect($media->makeGeneratedConversionKey('poster'))->toBe('poster');
expect($media->makeGeneratedConversionKey('poster.480p'))->toBe('poster.generated_conversions.480p');
expect($media->makeGeneratedConversionKey('poster.square.480p'))->toBe('poster.generated_conversions.square.generated_conversions.480p');
});

it('retrieve the generated conversion', function () {
Expand Down

0 comments on commit 89ff46e

Please sign in to comment.