Cleanup
This commit is contained in:
parent
214caa263b
commit
26df25a5e7
|
@ -1,143 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Classes\AttachmentsManager;
|
||||
use App\Classes\Bundle;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use League\Flysystem\StorageAttributes;
|
||||
|
||||
class Upgrade extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'upgrade';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Upgrade old articles to bundles';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$indexes = Storage::disk('content')
|
||||
->listContents('/', true)
|
||||
->filter(fn (StorageAttributes $attributes) => ($attributes->isFile() && Str::endsWith($attributes->path(), '.md')))
|
||||
->map(fn (StorageAttributes $attributes) => dirname($attributes->path()))
|
||||
->toArray();
|
||||
|
||||
foreach ($indexes as $oldBundle) {
|
||||
$this->output->write(sprintf('Upgrading %s... ', $oldBundle));
|
||||
|
||||
Storage::disk('content')->move($oldBundle . '/images', $oldBundle . '/data/attachments/images');
|
||||
Storage::disk('content')->move($oldBundle . '/sounds', $oldBundle . '/data/attachments/sounds');
|
||||
Storage::disk('content')->move($oldBundle . '/videos', $oldBundle . '/data/attachments/videos');
|
||||
|
||||
Storage::disk('content')->deleteDirectory($oldBundle . '/wikidata');
|
||||
|
||||
Storage::disk('content')->move($oldBundle . '/index.json', $oldBundle . '/data/index.json');
|
||||
Storage::disk('content')->move($oldBundle . '/metadata.json', $oldBundle . '/data/metadata.json');
|
||||
Storage::disk('content')->move($oldBundle . '/attachments.json', $oldBundle . '/data/attachments.json');
|
||||
|
||||
Storage::disk('content')->delete([
|
||||
$oldBundle . '/attachments_unused.json',
|
||||
$oldBundle . '/data/attachments_unused.json',
|
||||
$oldBundle . '/links.json',
|
||||
]);
|
||||
|
||||
$bundle = new Bundle($oldBundle, Storage::disk('content'));
|
||||
|
||||
$oldAttachments = json_decode(Storage::disk('content')->get($oldBundle . '/data/attachments.json') ?? '[]', true);
|
||||
|
||||
foreach ($oldAttachments as $oldRef => $oldAttachment) {
|
||||
$kind = $oldAttachment['kind'];
|
||||
|
||||
$oldAttachment['url'] = 'attachments/' . $oldAttachment['url'];
|
||||
|
||||
unset($oldAttachment['kind']);
|
||||
|
||||
switch ($kind) {
|
||||
case 'images':
|
||||
$bundle->attachments(AttachmentsManager::Images)->upsert($oldRef, $oldAttachment);
|
||||
break;
|
||||
case 'sounds':
|
||||
$bundle->attachments(AttachmentsManager::Sounds)->upsert($oldRef, $oldAttachment);
|
||||
break;
|
||||
case 'videos':
|
||||
$bundle->attachments(AttachmentsManager::Videos)->upsert($oldRef, $oldAttachment);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (['images', 'sounds', 'videos'] as $kind) {
|
||||
$oldAttachments = json_decode(Storage::disk('content')->get($oldBundle . '/data/' . $kind . '.json') ?? '[]', true);
|
||||
|
||||
if (empty($oldAttachments['files'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($oldAttachments['files'] as $oldRef => $oldAttachment) {
|
||||
$filename = basename($oldAttachment['url']);
|
||||
$slug = Str::slug(pathinfo($filename, PATHINFO_FILENAME));
|
||||
$fullPath = $oldBundle . '/data/' . $oldAttachment['url'];
|
||||
|
||||
if (Storage::disk('content')->exists($fullPath)) {
|
||||
$realPath = Storage::disk('content')->path($fullPath);
|
||||
$realFilename = basename($realPath);
|
||||
|
||||
if ($slug !== $realFilename) {
|
||||
$tempFilename = Str::random(6) . '.' . pathinfo($fullPath, PATHINFO_EXTENSION);
|
||||
$tempFile = $oldBundle . '/data/attachments/' . $kind . '/' . $tempFilename;
|
||||
$newFile = $oldBundle . '/data/attachments/' . $kind . '/' . $slug . '.' . pathinfo($fullPath, PATHINFO_EXTENSION);
|
||||
|
||||
Storage::disk('content')->move($fullPath, $tempFile);
|
||||
Storage::disk('content')->move($tempFile, $newFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$bundle->load();
|
||||
|
||||
$cover = $bundle->metadata()->get('cover');
|
||||
|
||||
if (!empty($cover) && is_array($cover)) {
|
||||
$url = $cover['url'];
|
||||
$url = 'attachments/' . $url;
|
||||
|
||||
$knownFiles = $bundle->attachments('images')->manager()->get('files');
|
||||
|
||||
foreach ($knownFiles as $ref => $data) {
|
||||
if ($data['url'] === $url) {
|
||||
$bundle->metadata()->set('cover', $ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$bundle->save();
|
||||
|
||||
Storage::disk('content')->delete([
|
||||
$oldBundle . '/data/attachments.json',
|
||||
$oldBundle . '/attachments_unused.json',
|
||||
$oldBundle . '/data/attachments_unused.json',
|
||||
$oldBundle . '/links.json',
|
||||
]);
|
||||
|
||||
if (collect(Storage::disk('content')->listContents($oldBundle . '/data/attachments'))->count() === 0) {
|
||||
Storage::disk('content')->deleteDirectory($oldBundle . '/data/attachments');
|
||||
}
|
||||
|
||||
$this->info('OK');
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user