Skip to content

Commit

Permalink
Merge pull request #38 from mirco-dlab/master
Browse files Browse the repository at this point in the history
Added soft deletes
  • Loading branch information
gabsource authored Dec 13, 2024
2 parents 6735785 + f2d7042 commit e6e0dac
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 6 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions config/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlterAttachmentsTableAddDeletedAtColumn extends Migration
{

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasTable('attachments')) {
Schema::table('attachments', function (Blueprint $table) {
$table->softDeletes();
});
}
}


/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if (Schema::hasTable('attachments')) {
Schema::table('attachments', function (Blueprint $table) {
$table->dropColumn('deleted_at');
});
}
}
}
14 changes: 12 additions & 2 deletions src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,6 +41,7 @@
*/
class Attachment extends Model implements AttachmentContract
{
use SoftDeletes;

protected $table = 'attachments';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Commands/CleanupAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
Expand All @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/MigrateAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/DropzoneController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e6e0dac

Please sign in to comment.