From a426f501d865fd19979ea057ab105502dff01b0d Mon Sep 17 00:00:00 2001 From: Mathieu De Keyzer Date: Sat, 12 Oct 2024 23:16:56 +0200 Subject: [PATCH] feat: saved ems assets archive in target dir --- .../src/Storage/StorageManager.php | 7 +++++++ EMS/helpers/src/File/TempDirectory.php | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/EMS/common-bundle/src/Storage/StorageManager.php b/EMS/common-bundle/src/Storage/StorageManager.php index de579c3df..1e301a858 100644 --- a/EMS/common-bundle/src/Storage/StorageManager.php +++ b/EMS/common-bundle/src/Storage/StorageManager.php @@ -553,6 +553,13 @@ public function extractFromArchive(string $hash): TempDirectory case MimeTypes::APPLICATION_ZIP->value: $tempDir = TempDirectory::createFromZipArchive($archiveFile->path); break; + case MimeTypes::APPLICATION_JSON->value: + $archive = Archive::fromStructure($archiveFile->getContents(), $this->hashAlgo); + $tempDir = TempDirectory::create(); + foreach ($archive->iterator() as $file) { + $tempDir->add($this->getStream($file->getHash()), $file->getFilename()); + } + break; default: throw new \RuntimeException(\sprintf('Archive format %s not supported', $type)); } diff --git a/EMS/helpers/src/File/TempDirectory.php b/EMS/helpers/src/File/TempDirectory.php index 3a66a2973..1d0c54d26 100644 --- a/EMS/helpers/src/File/TempDirectory.php +++ b/EMS/helpers/src/File/TempDirectory.php @@ -4,6 +4,7 @@ namespace EMS\Helpers\File; +use Psr\Http\Message\StreamInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; @@ -78,4 +79,24 @@ public function moveTo(string $directory): void $this->filesystem->rename($file->getPathname(), $directory.\DIRECTORY_SEPARATOR.$file->getRelativePathname()); } } + + public function add(StreamInterface $stream, string $filename): void + { + $explodedPath = \explode(\DIRECTORY_SEPARATOR, $this->path.\DIRECTORY_SEPARATOR.$filename); + \array_pop($explodedPath); + $this->filesystem->mkdir(\implode(\DIRECTORY_SEPARATOR, $explodedPath)); + if (!$handle = \fopen($this->path.\DIRECTORY_SEPARATOR.$filename, 'w')) { + throw new \RuntimeException(\sprintf('Can\'t open a temporary file %s', $this->path)); + } + + while (!$stream->eof()) { + if (false === \fwrite($handle, $stream->read(File::DEFAULT_CHUNK_SIZE))) { + throw new \RuntimeException(\sprintf('Can\'t write in temporary file %s', $this->path)); + } + } + + if (false === \fclose($handle)) { + throw new \RuntimeException(\sprintf('Can\'t close the temporary file %s', $this->path)); + } + } }