Skip to content

Commit

Permalink
fix(common/storage): put zip archive in cache (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 authored Dec 19, 2024
1 parent 354c48e commit 2d230f7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -47,15 +53,60 @@ 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');

$progressBar = $this->io->createProgressBar($this->storageManager->getSize($this->archiveHash));

$archiveFile = TempFile::create()->loadFromStream($this->storageManager->getStream($this->archiveHash), $this->output->isQuiet() ? null : function ($size) use ($progressBar): void {
$progressBar->advance($size);
});
$mimeType = MimeTypeHelper::getInstance()->guessMimeType($archiveFile->path);
$this->io->newLine();

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();
});
$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));
}
}
11 changes: 11 additions & 0 deletions EMS/common-bundle/src/Storage/StorageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 6 additions & 2 deletions EMS/helpers/src/File/TempFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,20 @@ public function exists(): bool
return \file_exists($this->path);
}

public function loadFromStream(StreamInterface $stream): self
public function loadFromStream(StreamInterface $stream, callable $callback = null): self
{
if (!$handle = \fopen($this->path, 'w')) {
throw new \RuntimeException(\sprintf('Can\'t open a temporary file %s', $this->path));
}

while (!$stream->eof()) {
if (false === \fwrite($handle, $stream->read(8192))) {
$size = \fwrite($handle, $stream->read(File::DEFAULT_CHUNK_SIZE));
if (false === $size) {
throw new \RuntimeException(\sprintf('Can\'t write in temporary file %s', $this->path));
}
if (null !== $callback) {
$callback($size);
}
}

if (false === \fclose($handle)) {
Expand Down

0 comments on commit 2d230f7

Please sign in to comment.