|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TotalCMS\Domain\Export\Service; |
| 4 | + |
| 5 | +use TotalCMS\Infrastructure\Filesystem\PathUtils; |
| 6 | +use TotalCMS\Support\Config; |
| 7 | + |
| 8 | +/** |
| 9 | + * Service for creating zip files of a single object's data and assets. |
| 10 | + */ |
| 11 | +readonly class ObjectZipper |
| 12 | +{ |
| 13 | + public function __construct( |
| 14 | + private Config $config, |
| 15 | + ) { |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Create a zip file of an object's JSON file and assets folder. |
| 20 | + * |
| 21 | + * @param string $collection The collection name |
| 22 | + * @param string $id The object ID |
| 23 | + * |
| 24 | + * @throws \RuntimeException If zip creation fails or object not found |
| 25 | + * |
| 26 | + * @return string The path to the created zip file |
| 27 | + */ |
| 28 | + public function createObjectZip(string $collection, string $id): string |
| 29 | + { |
| 30 | + $datadir = $this->config->datadir; |
| 31 | + |
| 32 | + // Paths |
| 33 | + $objectFile = PathUtils::buildPath(collection: $collection, filename: $id . '.json'); |
| 34 | + $assetsPath = PathUtils::buildPath(collection: $collection, filename: $id); |
| 35 | + |
| 36 | + $fullObjectPath = $datadir . DIRECTORY_SEPARATOR . $objectFile; |
| 37 | + |
| 38 | + // Verify object exists |
| 39 | + if (!file_exists($fullObjectPath)) { |
| 40 | + throw new \RuntimeException("Object not found: {$collection}/{$id}"); |
| 41 | + } |
| 42 | + |
| 43 | + // Create temp zip |
| 44 | + $tempZipPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . |
| 45 | + 'object-' . $collection . '-' . $id . '-' . time() . '.zip'; |
| 46 | + |
| 47 | + $zip = new \ZipArchive(); |
| 48 | + $result = $zip->open($tempZipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); |
| 49 | + |
| 50 | + if ($result !== true) { |
| 51 | + throw new \RuntimeException(sprintf('Failed to create zip file: %s (Error code: %d)', $tempZipPath, $result)); |
| 52 | + } |
| 53 | + |
| 54 | + // Add JSON file |
| 55 | + $zip->addFile($fullObjectPath, $id . '.json'); |
| 56 | + |
| 57 | + // Add assets folder if exists and has non-cache contents |
| 58 | + $fullAssetsPath = $datadir . DIRECTORY_SEPARATOR . $assetsPath; |
| 59 | + if (is_dir($fullAssetsPath) && $this->hasNonCacheContents($fullAssetsPath)) { |
| 60 | + $this->addDirectoryToZip($zip, $fullAssetsPath, $id); |
| 61 | + } |
| 62 | + |
| 63 | + $zip->close(); |
| 64 | + |
| 65 | + return $tempZipPath; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Check if a directory has any contents that are not .cache directories. |
| 70 | + */ |
| 71 | + private function hasNonCacheContents(string $path): bool |
| 72 | + { |
| 73 | + $iterator = new \DirectoryIterator($path); |
| 74 | + foreach ($iterator as $file) { |
| 75 | + if ($file->isDot()) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + // Skip .cache directories |
| 79 | + if ($file->getFilename() === '.cache') { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Recursively add directory contents to zip, excluding .cache folders. |
| 91 | + */ |
| 92 | + private function addDirectoryToZip(\ZipArchive $zip, string $realPath, string $zipPath): void |
| 93 | + { |
| 94 | + $iterator = new \RecursiveIteratorIterator( |
| 95 | + new \RecursiveDirectoryIterator($realPath, \RecursiveDirectoryIterator::SKIP_DOTS), |
| 96 | + \RecursiveIteratorIterator::SELF_FIRST |
| 97 | + ); |
| 98 | + |
| 99 | + foreach ($iterator as $file) { |
| 100 | + $filePath = $file->getRealPath(); |
| 101 | + $relativePath = substr((string)$filePath, strlen($realPath) + 1); |
| 102 | + |
| 103 | + // Skip .cache directories and their contents |
| 104 | + if (str_contains($relativePath, '.cache')) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + $zipFilePath = $zipPath . DIRECTORY_SEPARATOR . $relativePath; |
| 109 | + |
| 110 | + if ($file->isDir()) { |
| 111 | + $zip->addEmptyDir($zipFilePath); |
| 112 | + } elseif ($file->isFile()) { |
| 113 | + $zip->addFile($filePath, $zipFilePath); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get the filename for the zip download. |
| 120 | + */ |
| 121 | + public function getZipFilename(string $collection, string $id): string |
| 122 | + { |
| 123 | + return sprintf('%s--%s.zip', $collection, $id); |
| 124 | + } |
| 125 | +} |
0 commit comments