Skip to content

Commit

Permalink
support upload conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Nov 25, 2023
1 parent 23cae7c commit 504d2c4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 24 deletions.
6 changes: 6 additions & 0 deletions database/factories/MediaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class MediaFactory extends Factory
public function definition()
{
return [
'name' => 'empty',
'file_name' => 'empty.jpg',
'size' => 10,
'path' => "/uuid/empty.jpg",
'type' => MediaType::Image,
'collection_name' => config('media.default_collection_name'),
'disk' => config('media.disk'),
'model_id' => 0,
'model_type' => '\App\Models\Fake',
Expand Down
34 changes: 22 additions & 12 deletions src/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Http\File as HttpFile;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File as SupportFile;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -95,13 +96,13 @@ public function getPath(string $conversion = null): ?string
*/
protected function generatePath(): string
{
return $this->generateBasePath().$this->file_name;
return $this->generateBasePath() . $this->file_name;
}

protected function generateBasePath(string $conversion = null): string
{
if ($conversion) {
return "/{$this->uuid}/conversions/".str_replace('.', '/', $this->getConversionKey($conversion)).'/';
return "/{$this->uuid}/conversions/" . str_replace('.', '/', $this->getConversionKey($conversion)) . '/';
}

return "/{$this->uuid}/";
Expand All @@ -116,15 +117,19 @@ public function getUrl(string $conversion = null)
return Storage::disk($this->disk)->url($this->getPath($conversion));
}

public function addGeneratedConversion(string $name, GeneratedConversion $generatedConversion, string $parent = null): static
public function addGeneratedConversion(string $name, GeneratedConversion $generatedConversion): static
{
if ($parent) {
$conversion = $this->getGeneratedConversion($parent);
$conversion->conversions->put($name, $generatedConversion);
$genealogy = explode('.', $name);

if (count($genealogy) > 1) {
$child = Arr::last($genealogy);
$parents = implode('.', array_slice($genealogy, 0, count($genealogy) - 1));
$conversion = $this->getGeneratedConversion($parents);
$conversion->conversions->put($child, $generatedConversion);
} else {
$this->generated_conversions->put($name, $generatedConversion);
}

$this->generated_conversions->put($name, $generatedConversion);

return $this;
}

Expand Down Expand Up @@ -175,10 +180,15 @@ public function storeFileFromUpload(
return $this;
}

public function storeFile(string|UploadedFile $file, string $name = null): static
{
public function storeFile(
string|UploadedFile $file,
string $collection_name = null,
string $basePath = null,
string $name = null,
string $disk = null
): static {
if ($file instanceof UploadedFile) {
return $this->storeFileFromUpload($file, $name);
return $this->storeFileFromUpload($file, $collection_name, $basePath, $name, $disk);
}

return $this;
Expand All @@ -204,7 +214,7 @@ public function storeConversion(
name: $name,
extension: $extension,
file_name: $file_name,
path: ($basePath ?? $this->generateBasePath($conversion)).$file_name,
path: ($basePath ?? $this->generateBasePath($conversion)) . $file_name,
mime_type: $mime_type,
type: $type,
state: 'success',
Expand Down
81 changes: 69 additions & 12 deletions tests/Feature/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@
disk: config('media.disk')
));

$media->addGeneratedConversion('poster-optimized', new GeneratedConversion(
$media->addGeneratedConversion('poster.poster-optimized', new GeneratedConversion(
file_name: 'poster-optimized.png',
name: 'poster-optimized',
state: 'pending',
path: 'poster/conversions/optimized/poster-optimized.png',
type: MediaType::Image,
disk: config('media.disk')
), 'poster');
));

expect($media->hasGeneratedConversion('optimized'))->toBe(true);
expect($media->hasGeneratedConversion('poster.poster-optimized'))->toBe(true);
Expand All @@ -85,7 +85,17 @@
'poster' => MediaFactory::generatedConversion(),
]);

expect($media->getGeneratedConversion('poster')?->state)->tobe('success');
$generatedConversion = $media->getGeneratedConversion('poster');
$media->save();

expect($generatedConversion->state)->tobe('success');

$generatedConversion->state = 'failure';
$media->save();

$media->refresh();

expect($generatedConversion->state)->tobe('failure');
});

it('store an uploaded image', function () {
Expand All @@ -94,44 +104,46 @@

Storage::fake('media');

$file = UploadedFile::fake()->image('avatar.jpg', width: 16, height: 9);
$file = UploadedFile::fake()->image('foo.jpg', width: 16, height: 9);

$media->storeFileFromUpload(
file: $file,
collection_name: 'avatar',
name: 'foo',
name: 'avatar',
disk: 'media'
);

expect($media->width)->toBe(16);
expect($media->height)->toBe(9);
expect($media->aspect_ratio)->toBe((new Dimension(16, 9))->getRatio(false)->getValue());
expect($media->collection_name)->toBe('avatar');
expect($media->name)->toBe('foo');
expect($media->file_name)->toBe('foo.jpg');
expect($media->name)->toBe('avatar');
expect($media->file_name)->toBe('avatar.jpg');
expect($media->type)->toBe(MediaType::Image);
expect($media->path)->toBe("/{$media->uuid}/foo.jpg");
expect($media->path)->toBe("/{$media->uuid}/avatar.jpg");

Storage::disk('media')->assertExists($media->path);
});

it('store a conversion image', function () {
it('store a conversion image of a media', function () {
/** @var Media $media */
$media = MediaFactory::new()->make();

Storage::fake('media');

$orginial = UploadedFile::fake()->image('foo.jpg', width: 16, height: 9);

$media->storeFileFromUpload(
file: UploadedFile::fake()->image('avatar.jpg', width: 16, height: 9),
file: $orginial,
collection_name: 'avatar',
name: 'avatar',
disk: 'media'
);

$file = UploadedFile::fake()->image('avatar-poster.jpg', width: 16, height: 9);
$poster = UploadedFile::fake()->image('foo-poster.jpg', width: 16, height: 9);

$media->storeConversion(
file: $file->getPathname(),
file: $poster->getPathname(),
conversion: 'poster',
name: 'avatar-poster'
);
Expand All @@ -151,3 +163,48 @@

Storage::disk('media')->assertExists($generatedConversion->path);
});

it('store a conversion image of a conversion', function () {
/** @var Media $media */
$media = MediaFactory::new()->make();

Storage::fake('media');

$media->storeFileFromUpload(
file: UploadedFile::fake()->image('foo.jpg', width: 16, height: 9),
collection_name: 'avatar',
name: 'avatar',
disk: 'media'
);

$poster = UploadedFile::fake()->image('foo-poster.jpg', width: 16, height: 9);

$media->storeConversion(
file: $poster->getPathname(),
conversion: 'poster',
name: 'avatar-poster'
);

$small = UploadedFile::fake()->image('foo-poster-small.jpg', width: 16, height: 9);

$media->storeConversion(
file: $small->getPathname(),
conversion: 'poster.small',
name: 'avatar-poster-small'
);

$generatedConversion = $media->getGeneratedConversion('poster.small');

expect($generatedConversion)->toBeInstanceof(GeneratedConversion::class);

expect($generatedConversion->width)->toBe(16);
expect($generatedConversion->height)->toBe(9);
expect($generatedConversion->aspect_ratio)->toBe((new Dimension(16, 9))->getRatio(false)->getValue());
expect($generatedConversion->name)->toBe('avatar-poster-small');
expect($generatedConversion->file_name)->toBe('avatar-poster-small.jpg');
expect($generatedConversion->type)->toBe(MediaType::Image);
expect($generatedConversion->path)->toBe("/{$media->uuid}/conversions/poster/conversions/small/avatar-poster-small.jpg");
expect($generatedConversion->path)->toBe($media->getPath('poster.small'));

Storage::disk('media')->assertExists($generatedConversion->path);
});

0 comments on commit 504d2c4

Please sign in to comment.