From 7757ab6b77a5aa2398a4763eb703720cac1d1479 Mon Sep 17 00:00:00 2001 From: tonyyb Date: Wed, 27 Sep 2017 17:45:42 +0200 Subject: [PATCH] Call imagePathDeleted in some forgotten cases (#22) * Call after delete image in several cases * Code refacto * Rename variable + return bool * Php7 cast function --- README.md | 2 ++ src/Services/UploadImageService.php | 29 ++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 34c1f58..7ddd82e 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,8 @@ class Article extends \Backpack\NewsCRUD\app\Models\Article */ public function imagePathDeleted(string $imagePath, string $imageAttributeName = null, string $diskName = null) { + $this->clearMediaCollection(); + return true; } } diff --git a/src/Services/UploadImageService.php b/src/Services/UploadImageService.php index 8b43e08..b27c59f 100644 --- a/src/Services/UploadImageService.php +++ b/src/Services/UploadImageService.php @@ -101,16 +101,31 @@ public function deleteImages(Model $model) { $this->initModel($model); foreach ($this->filesAttributes($this->model->uploadableImages()) as $imageAttribute) { - \Storage::disk(self::STORAGE_DISK_NAME)->delete($this->model->{$imageAttribute}); - - // Performs custom actions after deleting - $imagePath = \Storage::disk(self::STORAGE_DISK_NAME)->getDriver()->getAdapter()->getPathPrefix().$this->model->{$imageAttribute}; - $this->model->imagePathDeleted($imagePath, $imageAttribute, self::STORAGE_DISK_NAME); + $this->deleteImage($imageAttribute, $this->model->{$imageAttribute}); } return true; } + /** + * Delete image file + * + * @param string $imageAttributeName + * @param string $imageRelativePath + * @return bool + */ + protected function deleteImage(string $imageAttributeName, string $imageRelativePath): bool + { + // Delete image on disk + \Storage::disk(self::STORAGE_DISK_NAME)->delete($imageRelativePath); + + // Performs custom actions after image deletion + $imagePath = \Storage::disk(self::STORAGE_DISK_NAME)->getDriver()->getAdapter()->getPathPrefix().$imageRelativePath; + $this->model->imagePathDeleted($imagePath, $imageAttributeName, self::STORAGE_DISK_NAME); + + return true; + } + /** * Delete old images * @@ -122,7 +137,7 @@ protected function deleteOldImage($originalValue, string $imageAttributeName) { // Delete old image : if (!empty($originalValue)) { - \Storage::disk(self::STORAGE_DISK_NAME)->delete($originalValue); + $this->deleteImage($imageAttributeName, $originalValue); } // Set path to '' as there is no image in the input : $this->model->fillUploadedImageAttributeValue($imageAttributeName, ''); @@ -144,7 +159,7 @@ protected function uploadNewImage(string $imageAttributeName, string $value, $or $this->tmpImages[$imageAttributeName] = \Image::make($value); // Delete the old one : if (!empty($originalValue)) { - \Storage::disk(self::STORAGE_DISK_NAME)->delete($originalValue); + $this->deleteImage($imageAttributeName, $originalValue); } $this->model->fillUploadedImageAttributeValue($imageAttributeName, '');