Skip to content

Commit

Permalink
Call imagePathDeleted in some forgotten cases (#22)
Browse files Browse the repository at this point in the history
* Call after delete image in several cases

* Code refacto

* Rename variable + return bool

* Php7 cast function
  • Loading branch information
tonyyb authored and jguyomard committed Sep 27, 2017
1 parent ccb31ef commit 7757ab6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
29 changes: 22 additions & 7 deletions src/Services/UploadImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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, '');
Expand All @@ -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, '');

Expand Down

0 comments on commit 7757ab6

Please sign in to comment.