Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #45 from murad-optimist/feature/media-delete
Browse files Browse the repository at this point in the history
added deleting observer in Media model, observer deletes files from d…
  • Loading branch information
Tarpsvo authored Dec 28, 2021
2 parents c451327 + ef07997 commit e8b31af
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ class Media extends Model

protected $appends = ['url', 'webp_url'];

protected static function boot()
{
parent::boot();
static::deleting(function ($model) {
$driver = config('nova-media-field.storage_driver');
$mediaPath = $model->path . $model->file_name;
Storage::disk($driver)->delete($mediaPath); // Delete media file in storage

// Delete other related files like thumbnails
foreach ($model->image_sizes as $imageSize) {
if (isset($imageSize)) {
$mediaThumbnailPath = $model->path . $imageSize['file_name'];
Storage::disk($driver)->delete($mediaThumbnailPath);
}
}
});
}

public function getDisk()
{
/** @var MediaHandler $instance */
Expand Down

0 comments on commit e8b31af

Please sign in to comment.