mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-13 16:11:07 +03:30
feat: complete upgrade media-library packages
This commit is contained in:
Executable
+34
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Services\Media\Media;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
class AddUUIDToMedia extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'app:add-uuid-to-media';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Command description';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
Media::query()->cursor()->each(
|
||||||
|
fn(Media $media) => $media->update(['uuid' => Str::uuid()])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration {
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('media', function (Blueprint $table) {
|
||||||
|
$table->char('uuid', 36)->nullable()->after('id');
|
||||||
|
$table->string('conversions_disk', 255)->nullable()->after('disk');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('transfers', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('conversions_disk');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
+81
@@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Services\Media\Media;
|
||||||
|
use Illuminate\Contracts\Filesystem\Factory;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Spatie\MediaLibrary\Support\PathGenerator\PathGeneratorFactory;
|
||||||
|
|
||||||
|
return new class extends Migration {
|
||||||
|
|
||||||
|
const OLD_COLLECTION_NAME = 'medialibrary_original';
|
||||||
|
const NEW_COLLECTION_NAME = 'media_library_original';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
$this->change(self::OLD_COLLECTION_NAME, self::NEW_COLLECTION_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
$this->change(self::NEW_COLLECTION_NAME, self::OLD_COLLECTION_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function change(string $from, string $to)
|
||||||
|
{
|
||||||
|
/** @var Factory $filesystem */
|
||||||
|
$filesystem = app(Factory::class);
|
||||||
|
|
||||||
|
|
||||||
|
// Find media with the old collection name is present
|
||||||
|
Media::query()
|
||||||
|
->withoutGlobalScopes()
|
||||||
|
->whereNotNull('responsive_images->' . $from)
|
||||||
|
->cursor()
|
||||||
|
->each(function ($media) use ($from, $to, $filesystem) {
|
||||||
|
// Change the old collection key
|
||||||
|
$responsive_images = array_merge(
|
||||||
|
$media->responsive_images,
|
||||||
|
[
|
||||||
|
$to => $media->responsive_images[$from],
|
||||||
|
$from => null
|
||||||
|
]
|
||||||
|
);
|
||||||
|
// Remove it completely
|
||||||
|
unset($responsive_images[$from]);
|
||||||
|
|
||||||
|
// Responsive image path for this media
|
||||||
|
$pathGenerator = PathGeneratorFactory::create($media);
|
||||||
|
$directory = $pathGenerator->getPathForResponsiveImages($media);
|
||||||
|
// Media disk
|
||||||
|
$disk = $filesystem->disk($media->disk);
|
||||||
|
|
||||||
|
foreach ($responsive_images[$to]['urls'] as &$filename) {
|
||||||
|
// Replace the old collection name with the new one
|
||||||
|
$newFilename = str_replace(
|
||||||
|
$from,
|
||||||
|
$to,
|
||||||
|
$filename
|
||||||
|
);
|
||||||
|
// If the old file exists move it on disk
|
||||||
|
if ($disk->exists($directory . $filename)) {
|
||||||
|
$disk->move($directory . $filename, $directory . $newFilename);
|
||||||
|
// Update the new array by ref
|
||||||
|
$filename = $newFilename;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Save the new array
|
||||||
|
$media->responsive_images = $responsive_images;
|
||||||
|
$media->save();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||||
|
|
||||||
|
return new class extends Migration {
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
if (!Schema::hasColumn('media', 'generated_conversions')) {
|
||||||
|
Schema::table('media', function (Blueprint $table) {
|
||||||
|
$table->json('generated_conversions')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Media::query()
|
||||||
|
->where(function ($query) {
|
||||||
|
$query->whereNull('generated_conversions')
|
||||||
|
->orWhere('generated_conversions', '')
|
||||||
|
->orWhereRaw("JSON_TYPE(generated_conversions) = 'NULL'");
|
||||||
|
})
|
||||||
|
->whereRaw("JSON_LENGTH(custom_properties) > 0")
|
||||||
|
->update([
|
||||||
|
'generated_conversions' => DB::raw("JSON_EXTRACT(custom_properties, '$.generated_conversions')"),
|
||||||
|
// OPTIONAL: Remove the generated conversions from the custom_properties field as well:
|
||||||
|
// 'custom_properties' => DB::raw("JSON_REMOVE(custom_properties, '$.generated_conversions')")
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
/* Restore the 'generated_conversions' field in the 'custom_properties' column if you removed them in this migration
|
||||||
|
Media::query()
|
||||||
|
->whereRaw("JSON_TYPE(generated_conversions) != 'NULL'")
|
||||||
|
->update([
|
||||||
|
'custom_properties' => DB::raw("JSON_SET(custom_properties, '$.generated_conversions', generated_conversions)")
|
||||||
|
]);
|
||||||
|
*/
|
||||||
|
|
||||||
|
Schema::table('media', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('generated_conversions');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user