generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31c7f15
commit 0ed7736
Showing
62 changed files
with
2,258 additions
and
2,831 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => [], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
database/migrations/create_media_conversions_table.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
database/migrations/migrate_generated_conversions_to_media_conversions_table.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.