From 0e784abbd46f0940643e2989ffe4f49b91ffc8c0 Mon Sep 17 00:00:00 2001 From: Mathieu De Keyzer Date: Sat, 12 Oct 2024 13:45:58 +0200 Subject: [PATCH] ref: StorageManager::extractFromArchive --- .../src/Helper/Asset/AssetHelperRuntime.php | 18 +++--------- .../src/Storage/StorageManager.php | 17 +++++++++++ EMS/common-bundle/src/Twig/AssetRuntime.php | 28 ++++--------------- EMS/helpers/src/File/TempDirectory.php | 15 ++++++++++ 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/EMS/client-helper-bundle/src/Helper/Asset/AssetHelperRuntime.php b/EMS/client-helper-bundle/src/Helper/Asset/AssetHelperRuntime.php index ed2a50d0d..6d62296c4 100644 --- a/EMS/client-helper-bundle/src/Helper/Asset/AssetHelperRuntime.php +++ b/EMS/client-helper-bundle/src/Helper/Asset/AssetHelperRuntime.php @@ -6,11 +6,8 @@ use EMS\ClientHelperBundle\Helper\Elasticsearch\ClientRequestManager; use EMS\CommonBundle\Helper\EmsFields; -use EMS\CommonBundle\Helper\MimeTypeHelper; use EMS\CommonBundle\Storage\StorageManager; use EMS\CommonBundle\Twig\AssetRuntime; -use EMS\Helpers\File\TempFile; -use EMS\Helpers\Html\MimeTypes; use Symfony\Component\Filesystem\Filesystem; use Twig\Extension\RuntimeExtensionInterface; @@ -53,17 +50,10 @@ public function assets(string $hash, string $saveDir = 'bundles', bool $addEnvir $directory = $basePath.$hash; try { - if (!$this->filesystem->exists($directory)) { - $archiveFile = TempFile::create()->loadFromStream($this->storageManager->getStream($hash)); - $type = MimeTypeHelper::getInstance()->guessMimeType($archiveFile->path); - switch ($type) { - case MimeTypes::APPLICATION_ZIP->value: - AssetRuntime::extractZip($archiveFile, $directory); - break; - default: - throw new \RuntimeException(\sprintf('Archive format %s not supported', $type)); - } - $this->filesystem->touch($directory.\DIRECTORY_SEPARATOR.$hash); + if (!$this->filesystem->exists($directory.\DIRECTORY_SEPARATOR.$hash)) { + $tempDir = $this->storageManager->extractFromArchive($hash); + $tempDir->touch($hash); + $tempDir->moveTo($directory); } if (!$addEnvironmentSymlink) { return $directory; diff --git a/EMS/common-bundle/src/Storage/StorageManager.php b/EMS/common-bundle/src/Storage/StorageManager.php index dbca522e1..de579c3df 100644 --- a/EMS/common-bundle/src/Storage/StorageManager.php +++ b/EMS/common-bundle/src/Storage/StorageManager.php @@ -545,6 +545,23 @@ public function getStreamFromArchive(string $hash, string $path): StreamWrapper throw new \RuntimeException(\sprintf('Archive format %s not supported', $mimeType)); } + public function extractFromArchive(string $hash): TempDirectory + { + $archiveFile = TempFile::create()->loadFromStream($this->getStream($hash)); + $type = MimeTypeHelper::getInstance()->guessMimeType($archiveFile->path); + switch ($type) { + case MimeTypes::APPLICATION_ZIP->value: + $tempDir = TempDirectory::createFromZipArchive($archiveFile->path); + break; + default: + throw new \RuntimeException(\sprintf('Archive format %s not supported', $type)); + } + $tempDir->touch($hash); + $archiveFile->clean(); + + return $tempDir; + } + private function getStreamFromZipArchive(string $hash, string $path, TempFile $zipFile): StreamWrapper { $dir = TempDirectory::createFromZipArchive($zipFile->path); diff --git a/EMS/common-bundle/src/Twig/AssetRuntime.php b/EMS/common-bundle/src/Twig/AssetRuntime.php index bd5c84b72..f9780b26b 100644 --- a/EMS/common-bundle/src/Twig/AssetRuntime.php +++ b/EMS/common-bundle/src/Twig/AssetRuntime.php @@ -11,6 +11,7 @@ use EMS\CommonBundle\Storage\Processor\Config; use EMS\CommonBundle\Storage\Processor\Processor; use EMS\CommonBundle\Storage\StorageManager; +use EMS\Helpers\File\TempDirectory; use EMS\Helpers\File\TempFile; use EMS\Helpers\Standard\Json; use Psr\Log\LoggerInterface; @@ -35,17 +36,17 @@ public function unzip(string $hash, string $saveDir, bool $mergeContent = false) { @\trigger_error(\sprintf('The function emsch_unzip is deprecated and should not be used anymore. use the function ems_file_from_archive or the route EMS\CommonBundle\Controller\FileController::assetInArchive instead"'), E_USER_DEPRECATED); try { - $checkFilename = $saveDir.\DIRECTORY_SEPARATOR.$this->storageManager->computeStringHash($saveDir); - $checkHash = \file_exists($checkFilename) ? \file_get_contents($checkFilename) : false; + $checkFilename = $saveDir.\DIRECTORY_SEPARATOR.$hash; - if ($checkHash !== $hash) { + if (!\file_exists($checkFilename)) { if (!$mergeContent && $this->filesystem->exists($saveDir)) { $this->filesystem->remove($saveDir); } $tempFile = TempFile::create()->loadFromStream($this->storageManager->getStream($hash)); - self::extractZip($tempFile, $saveDir); - \file_put_contents($checkFilename, $hash); + $tempDir = TempDirectory::createFromZipArchive($tempFile->path); + $tempDir->moveTo($saveDir); + $tempDir->touch($hash); } $excludeCheckFile = fn (SplFileInfo $f) => $f->getPathname() !== $checkFilename; @@ -69,23 +70,6 @@ public function temporaryFile(string $hash): ?string ->path; } - public static function extractZip(TempFile $tempFile, string $destination): bool - { - $zip = new \ZipArchive(); - if (true !== $open = $zip->open($tempFile->path)) { - throw new \RuntimeException(\sprintf('Failed opening zip %s (ZipArchive %s)', $tempFile->path, $open)); - } - - if (!$zip->extractTo($destination)) { - throw new \RuntimeException(\sprintf('Extracting of zip file failed (%s)', $destination)); - } - - $zip->close(); - $tempFile->clean(); - - return true; - } - /** * @param array $fileField * @param array $assetConfig diff --git a/EMS/helpers/src/File/TempDirectory.php b/EMS/helpers/src/File/TempDirectory.php index 5c323bf47..3a66a2973 100644 --- a/EMS/helpers/src/File/TempDirectory.php +++ b/EMS/helpers/src/File/TempDirectory.php @@ -5,6 +5,7 @@ namespace EMS\Helpers\File; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; class TempDirectory { @@ -63,4 +64,18 @@ public static function createFromZipArchive(string $zipFile): self return $tempDir; } + + public function touch(string $hash): void + { + $this->filesystem->touch($this->path.\DIRECTORY_SEPARATOR.$hash); + } + + public function moveTo(string $directory): void + { + $this->filesystem->mkdir($directory); + $finder = Finder::create(); + foreach ($finder->in($this->path)->depth('< 1') as $file) { + $this->filesystem->rename($file->getPathname(), $directory.\DIRECTORY_SEPARATOR.$file->getRelativePathname()); + } + } }