Skip to content

Commit ff93bd2

Browse files
authored
Merge pull request #544 from karlomikus/develop
Bugfix release
2 parents 0fb1d3f + 434e656 commit ff93bd2

File tree

17 files changed

+855
-443
lines changed

17 files changed

+855
-443
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# v5.9.3
2+
## Fixes
3+
- Fixed shopping list access check
4+
5+
# v5.9.2
6+
## Fixes
7+
- Fixed CSV import errors
8+
19
# v5.9.1
210
## Fixes
311
- Public menu can be enabled independently of `is_public` bar setting

app/Console/Commands/BarImportRecipes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function handle(): int
4747
'root' => storage_path('bar-assistant/temp/' . Str::random(8)),
4848
]);
4949

50-
$zipFileDisk = Storage::disk('bar-assistant');
50+
$zipFileDisk = Storage::disk('local');
5151

5252
$filename = $this->argument('filename');
5353

app/External/Export/ToDataPack.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Illuminate\Support\Facades\DB;
1010
use Kami\Cocktail\Models\Cocktail;
1111
use Illuminate\Support\Facades\Log;
12-
use Illuminate\Support\Facades\File;
1312
use Kami\Cocktail\Models\Calculator;
1413
use Kami\Cocktail\Models\Ingredient;
1514
use Kami\Cocktail\Models\BarIngredient;
@@ -51,31 +50,41 @@ public function process(int $barId, ?string $filename = null, ForceUnitConvertEn
5150
'called_from' => self::class,
5251
];
5352

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));
5655

57-
Log::debug(sprintf('Exporting datapack to "%s"', $filename));
56+
try {
57+
$zip = new ZipArchive();
5858

59-
$zip = new ZipArchive();
59+
if ($zip->open($tempFilePath, ZipArchive::CREATE) !== true) {
60+
$message = sprintf('Error creating zip archive with filepath "%s"', $tempFilePath);
6061

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+
}
6364

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);
6669

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+
}
7176

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');
7483
}
7584

76-
$zip->close();
85+
$this->file->disk('exports')->put($fullPath, $contents);
7786

78-
return $filename;
87+
return $fullPath;
7988
}
8089

8190
private function dumpCocktails(int $barId, ZipArchive &$zip, ?Units $toUnits = null): void

app/External/Export/ToRecipeType.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Carbon\Carbon;
99
use Kami\Cocktail\Models\Cocktail;
1010
use Illuminate\Support\Facades\Log;
11-
use Illuminate\Support\Facades\File;
1211
use Kami\RecipeUtils\UnitConverter\Units;
1312
use Kami\Cocktail\External\ExportTypeEnum;
1413
use Kami\Cocktail\External\ForceUnitConvertEnum;
@@ -44,26 +43,38 @@ public function process(int $barId, ?string $filename = null, ExportTypeEnum $ty
4443
'schema' => $type === ExportTypeEnum::Schema ? 'https://barassistant.app/cocktail-02.schema.json' : null,
4544
];
4645

47-
File::ensureDirectoryExists($this->file->disk('exports')->path((string) $barId));
48-
$filename = $this->file->disk('exports')->path($barId . '/' . $filename);
46+
$tempFilePath = tempnam(sys_get_temp_dir(), 'cocktail_export');
47+
Log::debug(sprintf('Exporting recipe type "%s" to temporary file "%s"', $type->value, $tempFilePath));
4948

50-
$zip = new ZipArchive();
49+
try {
50+
$zip = new ZipArchive();
5151

52-
if ($zip->open($filename, ZipArchive::CREATE) !== true) {
53-
$message = sprintf('Error creating zip archive with filepath "%s"', $filename);
52+
if ($zip->open($tempFilePath, ZipArchive::CREATE) !== true) {
53+
$message = sprintf('Error creating zip archive with filepath "%s"', $tempFilePath);
5454

55-
throw new ExportFileNotCreatedException($message);
56-
}
55+
throw new ExportFileNotCreatedException($message);
56+
}
57+
58+
$this->dumpCocktails($barId, $zip, $type, $toUnits);
5759

58-
$this->dumpCocktails($barId, $zip, $type, $toUnits);
60+
if ($metaContent = json_encode($meta)) {
61+
$zip->addFromString('_meta.json', $metaContent);
62+
}
63+
} finally {
64+
$zip->close();
65+
}
5966

60-
if ($metaContent = json_encode($meta)) {
61-
$zip->addFromString('_meta.json', $metaContent);
67+
$fullPath = $barId . '/' . $filename;
68+
Log::debug(sprintf('Moving temporary file from "%s" to exports disk at "%s"', $tempFilePath, $fullPath));
69+
$this->file->disk('exports')->makeDirectory((string) $barId);
70+
$contents = file_get_contents($tempFilePath);
71+
if ($contents === false) {
72+
throw new ExportFileNotCreatedException('Could not read temporary export file contents');
6273
}
6374

64-
$zip->close();
75+
$this->file->disk('exports')->put($fullPath, $contents);
6576

66-
return $filename;
77+
return $fullPath;
6778
}
6879

6980
private function dumpCocktails(int $barId, ZipArchive &$zip, ExportTypeEnum $type, ?Units $toUnits = null): void

app/External/Import/FromIngredientCSV.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,25 @@ public function process(
2323
DuplicateActionsEnum $duplicateAction = DuplicateActionsEnum::None,
2424
string $imageDirectoryBasePath = '',
2525
): void {
26+
$existingIngredientNames = Ingredient::where('bar_id', $this->barId)
27+
->pluck('name')
28+
->map(fn (string $name) => mb_strtolower($name))
29+
->toArray();
30+
2631
DB::beginTransaction();
2732
try {
2833
Reader::createFromPath($filepath)
2934
->setHeaderOffset(0)
35+
->filter(function (array $record) use ($existingIngredientNames, $duplicateAction) {
36+
$record = array_change_key_case($record, CASE_LOWER);
37+
38+
$ingredientNameLower = mb_strtolower($record['name'] ?? '');
39+
if ($duplicateAction === DuplicateActionsEnum::Skip && in_array($ingredientNameLower, $existingIngredientNames, true)) {
40+
return false;
41+
}
42+
43+
return true;
44+
})
3045
->each(function (array $record) {
3146
$ingredientExternal = IngredientExternal::fromCSV($record);
3247

app/Http/Controllers/ImportController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ public function ingredients(Request $request): JsonResponse
140140
'source' => 'required|file|mimes:csv|max:1048576',
141141
])->validate();
142142

143-
$file = $request->source->store('', 'temp-uploads');
143+
$file = $request->source->store('', 'temp');
144144
} else {
145145
$file = Str::random(10) . '.csv';
146146
$csv = $request->getContent();
147-
if (!Storage::disk('temp-uploads')->put($file, $csv)) {
147+
if (!Storage::disk('temp')->put($file, $csv)) {
148148
abort(500, 'Unable to store uploaded file');
149149
}
150150
}
@@ -172,7 +172,7 @@ public function ingredients(Request $request): JsonResponse
172172
// abort(403);
173173
// }
174174

175-
// $zipFile = $request->file('file')->store('/', 'temp-uploads');
175+
// $zipFile = $request->file('file')->store('/', 'temp');
176176
// $barId = $request->post('bar_id');
177177
// $duplicateAction = DuplicateActionsEnum::from($request->post('duplicate_actions', 'none'));
178178

@@ -184,7 +184,7 @@ public function ingredients(Request $request): JsonResponse
184184
// $unzippedFilesDisk = Storage::disk('temp');
185185

186186
// $zip = new ZipUtils();
187-
// $zip->unzip(Storage::disk('temp-uploads')->path($zipFile));
187+
// $zip->unzip(Storage::disk('temp')->path($zipFile));
188188

189189
// // $importer = new FromJsonSchema(
190190
// // resolve(\Kami\Cocktail\Services\CocktailService::class),
@@ -208,7 +208,7 @@ public function ingredients(Request $request): JsonResponse
208208
// // Log::error('Error importing from file: ' . $e->getMessage());
209209
// // } finally {
210210
// // $zip->cleanup();
211-
// // Storage::disk('temp-uploads')->delete($zipFile);
211+
// // Storage::disk('temp')->delete($zipFile);
212212
// // }
213213

214214
// \Illuminate\Support\Facades\DB::commit();

app/Http/Controllers/ShoppingListController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ShoppingListController extends Controller
3030
public function index(Request $request, int $id): JsonResource
3131
{
3232
$user = User::findOrFail($id);
33-
if ($request->user()->id !== $user->id || $request->user()->cannot('show', $user)) {
33+
if ($request->user()->id !== $user->id) {
3434
abort(403);
3535
}
3636

@@ -54,7 +54,7 @@ public function index(Request $request, int $id): JsonResource
5454
public function batchStore(IngredientsBatchRequest $request, int $id): Response
5555
{
5656
$user = User::findOrFail($id);
57-
if ($request->user()->id !== $user->id || $request->user()->cannot('show', $user)) {
57+
if ($request->user()->id !== $user->id) {
5858
abort(403);
5959
}
6060

@@ -111,7 +111,7 @@ public function batchStore(IngredientsBatchRequest $request, int $id): Response
111111
public function batchDelete(IngredientsBatchRequest $request, int $id): Response
112112
{
113113
$user = User::findOrFail($id);
114-
if ($request->user()->id !== $user->id || $request->user()->cannot('show', $user)) {
114+
if ($request->user()->id !== $user->id) {
115115
abort(403);
116116
}
117117

@@ -150,7 +150,7 @@ public function batchDelete(IngredientsBatchRequest $request, int $id): Response
150150
public function share(Request $request, int $id): JsonResponse
151151
{
152152
$user = User::findOrFail($id);
153-
if ($request->user()->id !== $user->id || $request->user()->cannot('show', $user)) {
153+
if ($request->user()->id !== $user->id) {
154154
abort(403);
155155
}
156156

app/Jobs/StartIngredientCSVImport.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public function handle(): void
3939
$importer = new FromIngredientCSV($this->barId, $this->userId);
4040

4141
try {
42-
$importer->process(Storage::disk('temp-uploads')->path($this->filepath));
42+
$importer->process(Storage::disk('temp')->path($this->filepath));
4343
} catch (Throwable $e) {
4444
Log::error('Error importing ingredients from CSV: ' . $e->getMessage());
4545
}
4646

47-
Storage::disk('temp-uploads')->delete($this->filepath);
47+
Storage::disk('temp')->delete($this->filepath);
4848
}
4949
}

app/Services/Image/ImageService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ private function processImageFile(string $image, ?string $filename = null): arra
161161
$filepath = 'temp/' . $filename . '.' . $fileExtension;
162162

163163
$vipsImage = ImageResizeService::resizeImageTo($image);
164-
$vipsImage->writeToFile($this->filesystemManager->disk('uploads')->path($filepath), ['Q' => 85]);
164+
$this->filesystemManager->disk('uploads')->put(
165+
$filepath,
166+
$vipsImage->writeToBuffer('.' . $fileExtension, ['Q' => 85])
167+
);
165168
} catch (Throwable $e) {
166169
$this->log->error('[IMAGE_SERVICE] ' . $e->getMessage());
167170

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"laravel/scout": "^10.4",
1818
"laravel/socialite": "^5.18",
1919
"league/csv": "^9.0",
20+
"league/flysystem-aws-s3-v3": "^3.0",
2021
"meilisearch/meilisearch-php": "^1.0",
2122
"nxp/math-executor": "^2.3",
2223
"promphp/prometheus_client_php": "^2.13",
@@ -41,7 +42,7 @@
4142
"barryvdh/laravel-debugbar": "^3.7",
4243
"driftingly/rector-laravel": "^2.1",
4344
"fakerphp/faker": "^1.9.1",
44-
"larastan/larastan": "^3.0",
45+
"larastan/larastan": "~3.7.0",
4546
"laravel/pint": "^1.0",
4647
"mockery/mockery": "^1.6",
4748
"nunomaduro/collision": "^8.6",

0 commit comments

Comments
 (0)