diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f6ab74b..fa78e37b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Lines marked **Upgrade step** need you to act after updating. - Webhooks send their custom headers again, such as `Authorization` or an API key, and saving a webhook in the editor keeps headers set through the API. (#218) - A webhook whose receiver is down or answers with a server error is tried up to 5 times in all, over about 1 hour 20 minutes. A webhook call stops after 10 seconds, a webhook URL must start with http:// or https://, and the webhook log shows a plain error text when the URL can't be reached. (#218) - The webhook log shows how often a webhook was really tried. Before, it stopped counting at 2. (#218) +- A logo or background whose file is already gone, and shows as a broken image, can be removed again. (#219) ### Security diff --git a/app/Http/Controllers/Api/FormImagesController.php b/app/Http/Controllers/Api/FormImagesController.php index 8ca296e3..971c7a8d 100644 --- a/app/Http/Controllers/Api/FormImagesController.php +++ b/app/Http/Controllers/Api/FormImagesController.php @@ -56,15 +56,13 @@ public function delete(Request $request, Form $form, DeleteUnusedFormImages $del ]); $fieldname = $request->input('type').'_path'; + $oldPath = $form->$fieldname; - if ($form->hasImage($request->input('type'))) { - $oldPath = $form->$fieldname; - - $form->$fieldname = null; - $form->save(); + // clear the path even when its file is already gone, so a broken image can be removed + $form->$fieldname = null; + $form->save(); - $deleteUnusedImages->delete(collect([$oldPath])); - } + $deleteUnusedImages->delete(collect([$oldPath])); return response()->json($form, 200); } diff --git a/tests/Feature/FormImageTest.php b/tests/Feature/FormImageTest.php index 3684b2cf..763b467b 100644 --- a/tests/Feature/FormImageTest.php +++ b/tests/Feature/FormImageTest.php @@ -158,6 +158,17 @@ expect($original->fresh()->hasImage($type))->toBeTrue(); })->with(['avatar', 'background']); +test('removing an image whose file is already gone clears its path', function (string $type) { + Storage::fake(); + $form = Form::factory()->create([$type.'_path' => 'form/gone.png']); + + $this->actingAs($form->user) + ->json('DELETE', route('api.forms.images.delete', $form->uuid), ['type' => $type]) + ->assertStatus(200); + + expect($form->fresh()->{$type.'_path'})->toBeNull(); +})->with(['avatar', 'background']); + test('replacing or removing an image no other form uses deletes it and its resized copies', function (string $type) { Storage::fake(); $form = Form::factory()->create([$type.'_path' => 'form/old.png']);