Skip to content

Commit

Permalink
feat: saved ems assets archive in target dir
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 committed Oct 12, 2024
1 parent 0e784ab commit a426f50
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions EMS/common-bundle/src/Storage/StorageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
21 changes: 21 additions & 0 deletions EMS/helpers/src/File/TempDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace EMS\Helpers\File;

use Psr\Http\Message\StreamInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -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));
}
}
}

0 comments on commit a426f50

Please sign in to comment.