From 66c93943e5bc261fd0e2efc9c54317a27a421ed5 Mon Sep 17 00:00:00 2001 From: Mathieu De Keyzer Date: Wed, 18 Dec 2024 20:55:29 +0100 Subject: [PATCH] fix: put zip archive in cache --- .../LoadArchiveItemsInCacheCommand.php | 51 ++++++++++++++++++- .../src/Storage/StorageManager.php | 11 ++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/EMS/common-bundle/src/Command/Storage/LoadArchiveItemsInCacheCommand.php b/EMS/common-bundle/src/Command/Storage/LoadArchiveItemsInCacheCommand.php index 0eec5dde4..098e82f9b 100644 --- a/EMS/common-bundle/src/Command/Storage/LoadArchiveItemsInCacheCommand.php +++ b/EMS/common-bundle/src/Command/Storage/LoadArchiveItemsInCacheCommand.php @@ -6,12 +6,18 @@ use EMS\CommonBundle\Commands; use EMS\CommonBundle\Common\Command\AbstractCommand; +use EMS\CommonBundle\Helper\MimeTypeHelper; use EMS\CommonBundle\Storage\Archive; use EMS\CommonBundle\Storage\StorageManager; +use EMS\Helpers\File\TempDirectory; +use EMS\Helpers\File\TempFile; +use EMS\Helpers\Html\MimeTypes; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; class LoadArchiveItemsInCacheCommand extends AbstractCommand { @@ -47,7 +53,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $this->io->title('Load archive\'s items in storage cache'); - $archive = Archive::fromStructure($this->storageManager->getContents($this->archiveHash), $this->storageManager->getHashAlgo()); + $this->io->section('Downloading archive'); + $archiveFile = TempFile::create()->loadFromStream($this->storageManager->getStream($this->archiveHash)); + $mimeType = MimeTypeHelper::getInstance()->guessMimeType($archiveFile->path); + $this->io->newLine(); + $this->io->writeln(\sprintf('It\'s an %s', $mimeType)); + + switch ($mimeType) { + case MimeTypes::APPLICATION_ZIP->value: + case MimeTypes::APPLICATION_GZIP->value: + $this->loadZipArchive($archiveFile); + break; + case MimeTypes::APPLICATION_JSON->value: + $this->loadEmsArchive($archiveFile); + break; + default: + throw new \RuntimeException(\sprintf('Archive format %s not supported', $mimeType)); + } + + return self::EXECUTE_SUCCESS; + } + + private function loadEmsArchive(TempFile $tempFile): void + { + $archive = Archive::fromStructure($tempFile->getContents(), $this->storageManager->getHashAlgo()); $progressBar = $this->io->createProgressBar($archive->getCount()); $this->storageManager->loadArchiveItemsInCache($this->archiveHash, $archive->skip($this->continue), $this->output->isQuiet() ? null : function () use ($progressBar) { $progressBar->advance(); @@ -55,7 +84,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int $progressBar->finish(); $this->io->newLine(); $this->io->writeln(\sprintf('%d files have been pushed in cache', $archive->getCount())); + } - return self::EXECUTE_SUCCESS; + private function loadZipArchive(TempFile $archiveFile): void + { + $dir = TempDirectory::createFromZipArchive($archiveFile->path); + $archiveFile->clean(); + $finder = new Finder(); + $finder->in($dir->path)->files(); + $this->io->progressStart($finder->count()); + $mimeTypeHelper = MimeTypeHelper::getInstance(); + $counter = 0; + /** @var SplFileInfo $file */ + foreach ($finder as $file) { + $mimeType = $mimeTypeHelper->guessMimeType($file->getPathname()); + $counter += $this->storageManager->addFileInArchiveCache($this->archiveHash, $file, $mimeType) ? 1 : 0; + $this->io->progressAdvance(); + } + $this->io->progressFinish(); + $this->io->newLine(); + $this->io->writeln(\sprintf('%d files have been pushed in cache', $counter)); } } diff --git a/EMS/common-bundle/src/Storage/StorageManager.php b/EMS/common-bundle/src/Storage/StorageManager.php index 3c4f7a75c..1f56aa259 100644 --- a/EMS/common-bundle/src/Storage/StorageManager.php +++ b/EMS/common-bundle/src/Storage/StorageManager.php @@ -706,4 +706,15 @@ public function loadArchiveItemsInCache(string $archiveHash, Archive $archive, c } } } + + public function addFileInArchiveCache(string $hash, SplFileInfo $file, string $mimeType): bool + { + foreach ($this->adapters as $adapter) { + if ($adapter->addFileInArchiveCache($hash, $file, $mimeType)) { + return true; + } + } + + return false; + } }