Skip to content

Commit

Permalink
feat: copy assets in archive cache
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 committed Oct 12, 2024
1 parent 3de99d5 commit 4a009f8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 1 deletion.
10 changes: 10 additions & 0 deletions EMS/common-bundle/src/Storage/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ private function parseFile(array $file): ArchiveItem
);
}

/**
* @return iterable<ArchiveItem>
*/
public function iterator(): iterable
{
foreach ($this->files as $path => $file) {
yield $path => $file;
}
}

/**
* @param mixed[] $file
* @return array{filename: string, hash: string, type: string, size: int}
Expand Down
5 changes: 5 additions & 0 deletions EMS/common-bundle/src/Storage/Service/AbstractUrlStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,9 @@ public function addFileInArchiveCache(string $hash, SplFileInfo $file, string $m
{
return false;
}

public function copyFileInArchiveCache(string $archiveHash, string $fileHash, string $path, string $mimeType): bool
{
return false;
}
}
5 changes: 5 additions & 0 deletions EMS/common-bundle/src/Storage/Service/EntityStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,9 @@ public function addFileInArchiveCache(string $hash, SplFileInfo $file, string $m
{
return false;
}

public function copyFileInArchiveCache(string $archiveHash, string $fileHash, string $path, string $mimeType): bool
{
return false;
}
}
5 changes: 5 additions & 0 deletions EMS/common-bundle/src/Storage/Service/FileSystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public function addFileInArchiveCache(string $hash, SplFileInfo $file, string $m
return \copy($file->getPathname(), $filename);
}

public function copyFileInArchiveCache(string $archiveHash, string $fileHash, string $path, string $mimeType): bool
{
return false;
}

protected function getCachePath(Config $config): string
{
return \join(DIRECTORY_SEPARATOR, [
Expand Down
19 changes: 19 additions & 0 deletions EMS/common-bundle/src/Storage/Service/S3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,25 @@ public function addFileInArchiveCache(string $hash, SplFileInfo $file, string $m
return $result->hasKey('ETag');
}

public function copyFileInArchiveCache(string $archiveHash, string $fileHash, string $path, string $mimeType): bool
{
$sourceKey = $this->key($fileHash);
$result = $this->getS3Client()->copyObject([
'Bucket' => $this->bucket,
'ContentType' => $mimeType,
'Key' => \implode('/', [
'cache',
\substr($archiveHash, 0, 3),
\substr($archiveHash, 3),
$path,
]),
'CopySource' => "$this->bucket/$sourceKey",
'MetadataDirective' => 'REPLACE',
]);

return $result->hasKey('ETag');
}

public function heads(string ...$hashes): array
{
$client = $this->getS3Client();
Expand Down
2 changes: 2 additions & 0 deletions EMS/common-bundle/src/Storage/Service/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ public function clearCache(): bool;
public function readFromArchiveInCache(string $hash, string $path): ?StreamWrapper;

public function addFileInArchiveCache(string $hash, SplFileInfo $file, string $mimeType): bool;

public function copyFileInArchiveCache(string $archiveHash, string $fileHash, string $path, string $mimeType): bool;
}
19 changes: 18 additions & 1 deletion EMS/common-bundle/src/Storage/StorageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,27 @@ private function getStreamFromZipArchive(string $hash, string $path, TempFile $z

private function getStreamFromJsonArchive(string $hash, string $path, TempFile $archiveFile): StreamWrapper
{
$file = Archive::fromStructure($archiveFile->getContents(), $this->hashAlgo)->getByPath($path);
$archive = Archive::fromStructure($archiveFile->getContents(), $this->hashAlgo);
$file = $archive->getByPath($path);
if (null === $file) {
throw new NotFoundHttpException(\sprintf('File %s not found in archive %s', $path, $hash));
}
$counter = 0;
foreach ($archive->iterator() as $item) {
foreach ($this->adapters as $adapter) {
if ($adapter->copyFileInArchiveCache($hash, $item->getHash(), $item->getFilename(), $item->getType())) {
++$counter;
break;
}
}
}
if ($archive->getSize() === $counter) {
$this->logger->debug(\sprintf('%d files have been successfully saved in cache', $counter));
} elseif (0 === $counter) {
$this->logger->warning(\sprintf('None of the %d files have been successfully saved in cache', $archive->getSize()));
} else {
$this->logger->warning(\sprintf('%d files, on a total of %d, have been successfully saved in cache', $counter, $archive->getSize()));
}

return new StreamWrapper($this->getStream($file->getHash()), $file->getType(), $file->getSize());
}
Expand Down

0 comments on commit 4a009f8

Please sign in to comment.