$storage->setVisibility($thumbFilePath, 'public');
$this->cache->put('images-' . $image->id . '-' . $thumbFilePath, $thumbFilePath, 60 * 60 * 72);
+
return $this->getPublicUrl($thumbFilePath);
}
/**
* Destroys an image at the given path.
- * Searches for image thumbnails in addition to main provided path..
- * @param string $path
- * @return bool
+ * Searches for image thumbnails in addition to main provided path.
*/
- protected function destroyImagesFromPath(string $path)
+ protected function destroyImagesFromPath(string $path): bool
{
$storage = $this->getStorage();
// Delete image files
$imagesToDelete = $allImages->filter(function ($imagePath) use ($imageFileName) {
- $expectedIndex = strlen($imagePath) - strlen($imageFileName);
- return strpos($imagePath, $imageFileName) === $expectedIndex;
+ return basename($imagePath) === $imageFileName;
});
$storage->delete($imagesToDelete->all());
$page = Page::first();
$this->asAdmin();
$imageName = 'first-image.png';
+ $relPath = $this->getTestImagePath('gallery', $imageName);
+ $this->deleteImage($relPath);
$this->uploadImage($imageName, $page->id);
$image = Image::first();
- $relPath = $this->getTestImagePath('gallery', $imageName);
$delete = $this->delete( '/images/' . $image->id);
$delete->assertStatus(200);
$this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
}
+ public function test_image_delete_does_not_delete_similar_images()
+ {
+ $page = Page::first();
+ $this->asAdmin();
+ $imageName = 'first-image.png';
+
+ $relPath = $this->getTestImagePath('gallery', $imageName);
+ $this->deleteImage($relPath);
+
+ $this->uploadImage($imageName, $page->id);
+ $this->uploadImage($imageName, $page->id);
+ $this->uploadImage($imageName, $page->id);
+
+ $image = Image::first();
+ $folder = public_path(dirname($relPath));
+ $imageCount = count(glob($folder . '/*'));
+
+ $delete = $this->delete( '/images/' . $image->id);
+ $delete->assertStatus(200);
+
+ $newCount = count(glob($folder . '/*'));
+ $this->assertEquals($imageCount - 1, $newCount, 'More files than expected have been deleted');
+ $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
+ }
+
protected function getTestProfileImage()
{
$imageName = 'profile.png';