diff --git a/README.md b/README.md index 27889fe..6697d84 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ Calling the `delete()` method on an attachment model instance will be disabled by setting the `behaviors.cascade_delete` to `false` in the configuration. -> Not that calling `delete()` on a `query()` like statement will not +> Note that calling `delete()` on a `query()` like statement will not cascade to the filesystem because it will not call the `delete()` method of the `Attachment` model class. @@ -134,6 +134,15 @@ $attachmentByKey = $user->attachment('myKey'); $attachmentByKey->delete(); // Will also delete the file on the storage by default ``` +### Soft Deletes +The package also supports the Laravel's SoftDeletes trait (disabled by default). + It can be enabled by setting the `behaviors.soft_delete` to `true` in + the configuration. + +> Note that `behaviors.cascade_delete` setting will then work only with + the `forceDelete()` method of the `Attachment` model class. + All the files will be kept while the record is soft-deleted + ## Hooking the file output diff --git a/config/attachments.php b/config/attachments.php index 3e1a478..847eb4b 100644 --- a/config/attachments.php +++ b/config/attachments.php @@ -56,6 +56,7 @@ */ 'behaviors' => [ 'cascade_delete' => env('ATTACHMENTS_CASCADE_DELETE', true), + 'soft_delete' => env('ATTACHMENTS_SOFT_DELETE', false), 'dropzone_check_csrf' => env('ATTACHMENTS_DROPZONE_CHECK_CSRF', true), ], diff --git a/migrations/2024_12_23_100100_alter_attachments_table_add_deleted_at_column.php b/migrations/2024_12_23_100100_alter_attachments_table_add_deleted_at_column.php new file mode 100644 index 0000000..5f77b1b --- /dev/null +++ b/migrations/2024_12_23_100100_alter_attachments_table_add_deleted_at_column.php @@ -0,0 +1,38 @@ +softDeletes(); + }); + } + } + + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + if (Schema::hasTable('attachments')) { + Schema::table('attachments', function (Blueprint $table) { + $table->dropColumn('deleted_at'); + }); + } + } +} diff --git a/src/Attachment.php b/src/Attachment.php index 58d0f53..9c9d79c 100644 --- a/src/Attachment.php +++ b/src/Attachment.php @@ -7,6 +7,7 @@ use Crypt; use File as FileHelper; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Storage; @@ -40,6 +41,7 @@ */ class Attachment extends Model implements AttachmentContract { + use SoftDeletes; protected $table = 'attachments'; @@ -87,7 +89,7 @@ public static function attach($uuid, $model, $options = []) $attachment->fill($options); if ($found = $model->attachments()->where('key', '=', $attachment->key)->first()) { - $found->delete(); + $found->forceDelete(); } return $attachment->model()->associate($model)->save() ? $attachment : null; @@ -211,8 +213,16 @@ protected static function boot() { parent::boot(); + if (!config('attachments.behaviors.soft_delete', false)) { + static::deleted(function ($attachment) { + /** @var Attachment $attachment */ + + $attachment->forceDelete(); + }); + } + if (config('attachments.behaviors.cascade_delete')) { - static::deleting(function ($attachment) { + static::forceDeleting(function ($attachment) { /** @var Attachment $attachment */ $attachment->deleteFile(); diff --git a/src/Console/Commands/CleanupAttachments.php b/src/Console/Commands/CleanupAttachments.php index 24b68f4..0faebac 100644 --- a/src/Console/Commands/CleanupAttachments.php +++ b/src/Console/Commands/CleanupAttachments.php @@ -44,6 +44,7 @@ public function handle() { if ($this->confirm(Lang::get('attachments::messages.console.cleanup_confirm'))) { $query = $this->model + ->withTrashed() ->whereNull('model_type') ->whereNull('model_id') ->where('updated_at', '<=', Carbon::now()->addMinutes(-1 * $this->option('since'))); @@ -55,7 +56,7 @@ public function handle() /** @var Collection $attachments */ $attachments->each(function ($attachment) use ($progress) { /** @var AttachmentContract $attachment */ - $attachment->delete(); + $attachment->forceDelete(); $progress->advance(); }); diff --git a/src/Console/Commands/MigrateAttachments.php b/src/Console/Commands/MigrateAttachments.php index 4fcab9d..5fb56f0 100644 --- a/src/Console/Commands/MigrateAttachments.php +++ b/src/Console/Commands/MigrateAttachments.php @@ -78,7 +78,7 @@ public function handle() $this->error(Lang::get('attachments::messages.console.migrate_invalid_to')); } - $query = Attachment::query() + $query = Attachment::withTrashed() ->where('disk', '=', $this->argument('from')); $this diff --git a/src/Http/Controllers/DropzoneController.php b/src/Http/Controllers/DropzoneController.php index d6df8e8..2857a01 100644 --- a/src/Http/Controllers/DropzoneController.php +++ b/src/Http/Controllers/DropzoneController.php @@ -74,7 +74,7 @@ public function delete($id, Request $request) return response(Lang::get('attachments::messages.errors.delete_denied'), 403); } - $file->delete(); + $file->forceDelete(); } return response('', 204);