|
9 | 9 | use Illuminate\Support\Facades\DB; |
10 | 10 | use Kami\Cocktail\Models\Cocktail; |
11 | 11 | use Illuminate\Support\Facades\Log; |
12 | | -use Illuminate\Support\Facades\File; |
13 | 12 | use Kami\Cocktail\Models\Calculator; |
14 | 13 | use Kami\Cocktail\Models\Ingredient; |
15 | 14 | use Kami\Cocktail\Models\BarIngredient; |
@@ -51,31 +50,41 @@ public function process(int $barId, ?string $filename = null, ForceUnitConvertEn |
51 | 50 | 'called_from' => self::class, |
52 | 51 | ]; |
53 | 52 |
|
54 | | - File::ensureDirectoryExists($this->file->disk('exports')->path((string) $barId)); |
55 | | - $filename = $this->file->disk('exports')->path($barId . '/' . $filename); |
| 53 | + $tempFilePath = tempnam(sys_get_temp_dir(), 'cocktail_export'); |
| 54 | + Log::debug(sprintf('Exporting datapack to temporary file "%s"', $tempFilePath)); |
56 | 55 |
|
57 | | - Log::debug(sprintf('Exporting datapack to "%s"', $filename)); |
| 56 | + try { |
| 57 | + $zip = new ZipArchive(); |
58 | 58 |
|
59 | | - $zip = new ZipArchive(); |
| 59 | + if ($zip->open($tempFilePath, ZipArchive::CREATE) !== true) { |
| 60 | + $message = sprintf('Error creating zip archive with filepath "%s"', $tempFilePath); |
60 | 61 |
|
61 | | - if ($zip->open($filename, ZipArchive::CREATE) !== true) { |
62 | | - $message = sprintf('Error creating zip archive with filepath "%s"', $filename); |
| 62 | + throw new ExportFileNotCreatedException($message); |
| 63 | + } |
63 | 64 |
|
64 | | - throw new ExportFileNotCreatedException($message); |
65 | | - } |
| 65 | + $this->dumpCocktails($barId, $zip, $toUnits); |
| 66 | + $this->dumpIngredients($barId, $zip, $toUnits); |
| 67 | + $this->dumpBaseData($barId, $zip); |
| 68 | + $this->dumpCalculators($barId, $zip); |
66 | 69 |
|
67 | | - $this->dumpCocktails($barId, $zip, $toUnits); |
68 | | - $this->dumpIngredients($barId, $zip, $toUnits); |
69 | | - $this->dumpBaseData($barId, $zip); |
70 | | - $this->dumpCalculators($barId, $zip); |
| 70 | + if ($metaContent = json_encode($meta)) { |
| 71 | + $zip->addFromString('_meta.json', $metaContent); |
| 72 | + } |
| 73 | + } finally { |
| 74 | + $zip->close(); |
| 75 | + } |
71 | 76 |
|
72 | | - if ($metaContent = json_encode($meta)) { |
73 | | - $zip->addFromString('_meta.json', $metaContent); |
| 77 | + $fullPath = $barId . '/' . $filename; |
| 78 | + Log::debug(sprintf('Moving datapack temporary file from "%s" to exports disk at "%s"', $tempFilePath, $fullPath)); |
| 79 | + $this->file->disk('exports')->makeDirectory((string) $barId); |
| 80 | + $contents = file_get_contents($tempFilePath); |
| 81 | + if ($contents === false) { |
| 82 | + throw new ExportFileNotCreatedException('Could not read temporary export file contents'); |
74 | 83 | } |
75 | 84 |
|
76 | | - $zip->close(); |
| 85 | + $this->file->disk('exports')->put($fullPath, $contents); |
77 | 86 |
|
78 | | - return $filename; |
| 87 | + return $fullPath; |
79 | 88 | } |
80 | 89 |
|
81 | 90 | private function dumpCocktails(int $barId, ZipArchive &$zip, ?Units $toUnits = null): void |
|
0 commit comments