Skip to content

Commit

Permalink
V3
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Oct 12, 2024
1 parent 31c7f15 commit 0ed7736
Show file tree
Hide file tree
Showing 62 changed files with 2,258 additions and 2,831 deletions.
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,23 +271,6 @@ return [
*/
'queue' => null,

/**
* Customize WithoutOverlapping middleware settings
*/
'queue_overlapping' => [
/**
* The release value should be longer than the longest conversion job that might run
* Default is: 1 minute. Increase it if your jobs are longer.
*/
'release_after' => 60,
/**
* The expire value allows you to forget a lock in case of an unexpected job failure
*
* @see https://laravel.com/docs/10.x/queues#preventing-job-overlaps
*/
'expire_after' => 60 * 60,
],

];
```

Expand Down
19 changes: 2 additions & 17 deletions config/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
'model' => Media::class,

'temporary_storage_path' => 'app/tmp/media',

/**
* The default disk used for storing files
*/
Expand Down Expand Up @@ -58,21 +60,4 @@
*/
'queue' => null,

/**
* Customize WithoutOverlapping middleware settings
*/
'queue_overlapping' => [
/**
* The release value should be longer than the longest conversion job that might run
* Default is: 1 minute. Increase it if your jobs are longer.
*/
'release_after' => 60,
/**
* The expire value allows you to forget a lock in case of an unexpected job failure
*
* @see https://laravel.com/docs/10.x/queues#preventing-job-overlaps
*/
'expire_after' => 60 * 60,
],

];
38 changes: 38 additions & 0 deletions database/factories/MediaConversionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Elegantly\Media\Database\Factories;

use Elegantly\Media\Enums\MediaType;
use Elegantly\Media\Models\MediaConversion;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<MediaConversion>
*/
class MediaConversionFactory extends Factory
{
protected $model = MediaConversion::class;

public function definition()
{
return [
'conversion_name' => 'name',
'state' => 'success',
'state_set_at' => now(),
'disk' => config('media.disk'),
'path' => '{uuid}/conversions/name/fileName.jpg',
'type' => MediaType::Image,
'name' => 'fileName',
'extension' => 'jpg',
'file_name' => 'fileName.jpg',
'mime_type' => 'image/jpeg',
'width' => 16,
'height' => 9,
'aspect_ratio' => 16 / 9,
'average_color' => null,
'size' => 800,
'duration' => null,
'metadata' => [],
];
}
}
58 changes: 19 additions & 39 deletions database/factories/MediaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Elegantly\Media\Database\Factories;

use Elegantly\Media\Casts\GeneratedConversion;
use Elegantly\Media\Enums\MediaType;
use Elegantly\Media\Models\Media;
use Elegantly\Media\Models\MediaConversion;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @template TModel of Media
* @extends Factory<Media>
*/
class MediaFactory extends Factory
{
Expand All @@ -20,51 +20,31 @@ public function definition()
'name' => 'empty',
'file_name' => 'empty.jpg',
'size' => 10,
'path' => '/uuid/empty.jpg',
'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',
];
}

public function withPoster(): static
{
return $this->state(function (array $attributes) {
return [
'generated_conversions' => collect($attributes['generated_conversions'] ?? [])
->put('poster', new GeneratedConversion(
state: 'success',
type: MediaType::Image,
file_name: 'poster.png',
name: 'poster',
path: '/uuid/poster/poster.png',
disk: $attributes['disk'],
)),
];
});
}

public static function generatedConversion(?string $disk = null)
{
return new GeneratedConversion(
state: 'success',
type: MediaType::Image,
file_name: 'poster.png',
name: 'poster',
path: '/poster/poster.png',
disk: $disk ?? config('media.disk'),
generated_conversions: collect([
'480p' => new GeneratedConversion(
state: 'success',
type: MediaType::Image,
file_name: 'poster-480p.png',
name: 'poster-480p',
path: '/poster/generated_conversions/480p/poster-480p.png',
disk: $disk ?? config('media.disk'),
),
])
return $this->has(
MediaConversion::factory()
->state(fn ($attributes) => [
'conversion_name' => 'poster',
'disk' => $attributes['disk'],
'path' => '{uuid}/conversions/poster/poster.jpg',
'type' => MediaType::Image,
'name' => 'poster',
'extension' => 'jpg',
'file_name' => 'poster.jpg',
'mime_type' => 'image/jpeg',
'width' => $attributes['width'] ?? null,
'height' => $attributes['height'] ?? null,
'aspect_ratio' => $attributes['aspect_ratio'] ?? null,
]),
'conversions'
);
}
}
49 changes: 49 additions & 0 deletions database/migrations/create_media_conversions_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::create('media_conversions', function (Blueprint $table) {
$table->id();
$table->uuid('uuid')->unique();

$table->string('conversion_name');

$table->foreignId('media_id');

$table->string('state')->nullable();
$table->dateTime('state_set_at')->nullable();

$table->longText('contents')->nullable();

$table->string('disk')->nullable();

$table->text('path')->nullable();
$table->string('name')->nullable();
$table->string('file_name')->nullable();
$table->string('extension')->nullable();
$table->string('mime_type')->nullable();
$table->string('type')->nullable();
$table->unsignedBigInteger('size')->nullable();
$table->unsignedBigInteger('width')->nullable();
$table->unsignedBigInteger('height')->nullable();
$table->decimal('aspect_ratio', 8, 2, true)->nullable();
$table->string('average_color')->nullable();
$table->decimal('duration', 19, 2, true)->nullable();

$table->json('metadata')->nullable();

$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('media_conversations');
}
};
25 changes: 14 additions & 11 deletions database/migrations/create_media_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,39 @@ return new class extends Migration
{
Schema::create('media', function (Blueprint $table) {
$table->id();
$table->uuid('uuid')->unique();

$table->nullableMorphs('model');
$table->uuid('uuid')->unique()->index();
$table->string('collection_name')->index();
$table->string('collection_group')->nullable();
$table->string('disk')->nullable();

$table->text('path')->nullable();
$table->string('name')->nullable();
$table->string('file_name')->nullable();
$table->string('extension')->nullable();
$table->string('mime_type')->nullable();
$table->string('disk')->nullable();
$table->unsignedBigInteger('size')->nullable();
$table->unsignedBigInteger('order_column')->nullable()->index();
$table->json('generated_conversions')->nullable();

$table->string('path')->nullable();
$table->string('type')->nullable();
$table->string('extension')->nullable();
$table->unsignedBigInteger('size')->nullable();
$table->unsignedBigInteger('width')->nullable();
$table->unsignedBigInteger('height')->nullable();
$table->decimal('aspect_ratio', 8, 2, true)->nullable();
$table->string('average_color')->nullable();
$table->decimal('duration', 19, 2, true)->nullable();

$table->unsignedBigInteger('order_column')->nullable()->index();
$table->json('metadata')->nullable();

$table->json('generated_conversions')->nullable();

$table->nullableMorphs('model');

$table->timestamps();

$table->index(['model_type', 'model_id', 'collection_name']);
});
}

public function down(){
public function down()
{
Schema::dropIfExists('media');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

use Elegantly\Media\Models\Media;
use Elegantly\Media\Models\MediaConversion;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Arr;

return new class extends Migration
{
public function up()
{
Media::query()
->chunkById(5_000, function ($items) {

foreach ($items as $media) {

if (! $media->generated_conversions) {
continue;
}

/** @var array<int, array> $generatedConversions */
$generatedConversions = json_decode($media->generated_conversions, true);

if (empty($generatedConversions)) {
continue;
}

$conversions = $this->generatedConversionsToMediaConversions(
$generatedConversions,
);

$media->conversions()->saveMany($conversions);

}

});

Schema::create('media', function (Blueprint $table) {
$table->dropColumn('generated_conversions');
});

}

public function down()
{
Schema::create('media', function (Blueprint $table) {
$table->json('generated_conversions')->nullable();
});
}

/**
* @return array<int, MediaConversion>
*/
public function generatedConversionsToMediaConversions(
array $generatedConversions,
?string $parent = null,
): array {

return collect($generatedConversions)
->flatMap(function (array $generatedConversion, string $conversionName) use ($parent) {

$fullName = $parent ? "{$parent}.{$conversionName}" : $conversionName;

$root = new MediaConversion([
'conversion_name' => $fullName,
...Arr::only($generatedConversion, [
'state',
'state_set_at',
'disk',
'path',
'type',
'name',
'extension',
'file_name',
'mime_type',
'width',
'height',
'aspect_ratio',
'average_color',
'size',
'duration',
'metadata',
'created_at',
'updated_at',
]),
]);

if ($children = data_get($generatedConversion, 'generated_conversions')) {
return [
$root,
...$this->generatedConversionsToMediaConversions($children, $fullName),
];
}

return [$root];

})
->toArray();

}
};
3 changes: 1 addition & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ includes:
- phpstan-baseline.neon

parameters:
level: 4
level: 9
paths:
- src
- config
- database
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true

Loading

0 comments on commit 0ed7736

Please sign in to comment.