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'); } } }