Skip to content

Commit a8be92d

Browse files
committed
Cleanup old image
1 parent 6f5495f commit a8be92d

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
OPENAI_ORGANIZATION= #Optional
2121
```
2222
23+
## Fixes
24+
- Correctly clean up old image when new one is uploaded
25+
2326
## Changes
2427
- Removed feeds
2528
- Improved bar delete query plan

app/Services/Image/ImageService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public function uploadAndSaveImages(array $requestImages, int $userId): array
3939

4040
if ($dtoImage->image) {
4141
try {
42+
$oldFilePath = $image->file_path;
4243
[$filepath, $fileExtension, $thumbHash] = $this->processImageFile($dtoImage->image);
44+
$this->filesystem->delete($oldFilePath);
4345

4446
$image->file_path = $filepath;
4547
$image->placeholder_hash = $thumbHash;

tests/Feature/Http/ImageControllerTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,51 @@ public function test_single_image_upload(): void
5454
Storage::disk('uploads')->assertExists($filename);
5555
}
5656

57+
public function test_single_image_upload_updates_old_image(): void
58+
{
59+
$s = Storage::fake('uploads');
60+
File::ensureDirectoryExists($s->path('temp'));
61+
62+
$response = $this->post('/api/images', [
63+
'images' => [
64+
[
65+
'image' => UploadedFile::fake()->createWithContent('image.jpg', $this->getFakeImageContent('jpg')),
66+
'copyright' => 'Made with test',
67+
'sort' => 1,
68+
]
69+
],
70+
]);
71+
72+
$response->assertJson(function (AssertableJson $json) {
73+
$json->has('data', 1, function ($json) {
74+
$json->has('id')
75+
->has('file_path')
76+
->where('copyright', 'Made with test')
77+
->etc();
78+
});
79+
});
80+
81+
$filename = $response->json('data.0.file_path');
82+
83+
Storage::disk('uploads')->assertExists($filename);
84+
85+
$oldId = $response->json('data.0.id');
86+
$response = $this->post('/api/images', [
87+
'images' => [
88+
[
89+
'id' => $oldId,
90+
'image' => UploadedFile::fake()->createWithContent('image.jpg', $this->getFakeImageContent('jpg')),
91+
'copyright' => 'New image',
92+
'sort' => 2,
93+
]
94+
],
95+
]);
96+
$newFilename = $response->json('data.0.file_path');
97+
98+
Storage::disk('uploads')->assertExists($newFilename);
99+
Storage::disk('uploads')->assertMissing($filename);
100+
}
101+
57102
public function test_multiple_image_upload(): void
58103
{
59104
$s = Storage::fake('uploads');

0 commit comments

Comments
 (0)