Skip to content

Commit 5a4f2f7

Browse files
committed
Save export files as a stream
1 parent 2e7215b commit 5a4f2f7

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

app/External/Export/ToDataPack.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,19 @@ public function process(int $barId, ?string $filename = null, ForceUnitConvertEn
8080
$fullPath = $barId . '/' . $filename;
8181
Log::debug(sprintf('Moving datapack temporary file from "%s" to exports disk at "%s"', $tempFilePath, $fullPath));
8282
$this->file->makeDirectory((string) $barId);
83-
$contents = file_get_contents($tempFilePath);
84-
if ($contents === false) {
83+
$stream = fopen($tempFilePath, 'r');
84+
if ($stream === false) {
8585
throw new ExportFileNotCreatedException('Could not read temporary export file contents');
8686
}
8787

88-
$this->file->put($fullPath, $contents);
88+
try {
89+
$this->file->put($fullPath, $stream);
90+
} finally {
91+
if (is_resource($stream)) {
92+
fclose($stream);
93+
}
94+
unlink($tempFilePath);
95+
}
8996

9097
return $fullPath;
9198
}

app/External/Export/ToRecipeType.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,19 @@ public function process(int $barId, ?string $filename = null, ExportTypeEnum $ty
7070
$fullPath = $barId . '/' . $filename;
7171
Log::debug(sprintf('Moving temporary file from "%s" to exports disk at "%s"', $tempFilePath, $fullPath));
7272
$this->file->makeDirectory((string) $barId);
73-
$contents = file_get_contents($tempFilePath);
74-
if ($contents === false) {
73+
$stream = fopen($tempFilePath, 'r');
74+
if ($stream === false) {
7575
throw new ExportFileNotCreatedException('Could not read temporary export file contents');
7676
}
7777

78-
$this->file->put($fullPath, $contents);
78+
try {
79+
$this->file->put($fullPath, $stream);
80+
} finally {
81+
if (is_resource($stream)) {
82+
fclose($stream);
83+
}
84+
unlink($tempFilePath);
85+
}
7986

8087
return $fullPath;
8188
}

app/External/Model/CocktailIngredient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function fromModel(CocktailIngredientModel $model, ?Units $toUnits
3939
}
4040

4141
return new self(
42-
Ingredient::fromModel($model->ingredient),
42+
Ingredient::fromModelBasic($model->ingredient),
4343
$amount,
4444
(bool) $model->optional,
4545
(bool) $model->is_specified,

app/External/Model/CocktailIngredientSubstitute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function fromModel(CocktailIngredientSubstituteModel $model, ?Unit
2929
}
3030

3131
return new self(
32-
Ingredient::fromModel($model->ingredient),
32+
Ingredient::fromModelBasic($model->ingredient),
3333
$amount,
3434
);
3535
}

app/External/Model/Ingredient.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static function fromModel(IngredientModel $model, bool $useFileURI = fals
4747
{
4848
$images = $model->images->map(fn (ImageModel $image) => Image::fromModel($image, $useFileURI))->toArray();
4949

50-
$ingredientParts = $model->ingredientParts->map(fn (ComplexIngredient $part) => Ingredient::fromModel($part->ingredient))->toArray();
50+
$ingredientParts = $model->ingredientParts->map(fn (ComplexIngredient $part) => Ingredient::fromModelBasic($part->ingredient))->toArray();
5151

5252
$ingredientPrices = $model->prices->map(fn (IngredientPriceModel $price) => IngredientPrice::fromModel($price))->toArray();
5353

@@ -78,6 +78,18 @@ public static function fromModel(IngredientModel $model, bool $useFileURI = fals
7878
);
7979
}
8080

81+
public static function fromModelBasic(IngredientModel $model): self
82+
{
83+
return new self(
84+
id: $model->getExternalId(),
85+
name: $model->name,
86+
strength: $model->strength,
87+
description: $model->description,
88+
origin: $model->origin,
89+
color: $model->color,
90+
);
91+
}
92+
8193
public static function fromDataPackArray(array $sourceArray): self
8294
{
8395
$images = [];

0 commit comments

Comments
 (0)