diff --git a/app/Console/Commands/Bundle/Upgrade.php b/app/Console/Commands/Bundle/Upgrade.php index 3612d6b..639a80e 100644 --- a/app/Console/Commands/Bundle/Upgrade.php +++ b/app/Console/Commands/Bundle/Upgrade.php @@ -31,11 +31,64 @@ public function handle() $path = $this->argument('path') ?? '/'; $bundles = Bundle::findBundles($disk, $path, true); - dd('Nothing to do'); - foreach ($bundles as $bundle) { $this->output->write(sprintf('Upgrading %s... ', $bundle->getPath())); + $disk->move($bundle->getPath() . 'images', $bundle->getDataDir() . 'attachments/images'); + $disk->move($bundle->getPath() . 'sounds', $bundle->getDataDir() . 'attachments/sounds'); + $disk->move($bundle->getPath() . 'videos', $bundle->getDataDir() . 'attachments/videos'); + $disk->move($bundle->getPath() . 'index.json', $bundle->getDataDir() . 'index.json'); + $disk->move($bundle->getPath() . 'metadata.json', $bundle->getDataDir() . 'metadata.json'); + + if ($disk->exists($bundle->getPath() . 'attachments.json')) { + $attachments = json_decode($disk->get($bundle->getPath() . 'attachments.json'), true); + $images = []; + $sounds = []; + $videos = []; + + foreach ($attachments as $ref => $data) { + $kind = $data['kind']; + + $data['filename'] = 'attachments/' . $data['url']; + + unset($data['url']); + unset($data['kind']); + + switch ($kind) { + case 'images': + $images['files'][$ref] = $data; + break; + case 'sounds': + $sounds['files'][$ref] = $data; + break; + case 'videos': + $videos['files'][$ref] = $data; + break; + } + } + + if (!empty($images)) { + $disk->put($bundle->getDataDir() . 'images.json', json_encode($images, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); + } + + if (!empty($sounds)) { + $disk->put($bundle->getDataDir() . 'sounds.json', json_encode($sounds, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); + } + + if (!empty($videos)) { + $disk->put($bundle->getDataDir() . 'videos.json', json_encode($videos, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); + } + } + + $filesToDelete = [ + 'links.json', + 'attachments.json', + ]; + + $filesToDelete = array_map(fn ($filename) => $bundle->getPath() . $filename, $filesToDelete); + + $disk->delete($filesToDelete); + $this->info('OK'); } }