From b14be6c70b9bc501a6eb80d25375f8fd76e565b8 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Thu, 18 Apr 2024 00:25:46 +0200 Subject: [PATCH] Added an upgrade script Kepping it in case of need but should be deprecated by now --- app/Console/Commands/Upgrade.php | 143 +++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 app/Console/Commands/Upgrade.php diff --git a/app/Console/Commands/Upgrade.php b/app/Console/Commands/Upgrade.php new file mode 100644 index 0000000..2f2b8d6 --- /dev/null +++ b/app/Console/Commands/Upgrade.php @@ -0,0 +1,143 @@ +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'); + } + } +}