Skip to content

Commit 9912476

Browse files
committed
Clean up command
1 parent 432ac50 commit 9912476

File tree

5 files changed

+115
-3
lines changed

5 files changed

+115
-3
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ $attachmentByKey = $user->attachment('myKey');
8484
$publicUrl = $attachmentByKey->url;
8585
```
8686

87+
## Delete an attachment
88+
89+
Calling the `delete()` method on an attachment model instance will
90+
delete the database row and the file. The deletion of the file can
91+
be disabled by setting the `behaviors.cascade_delete` to `false` in
92+
the configuration.
93+
94+
> Not that calling `delete()` on a `query()` like statement will not
95+
cascade to the filesystem because it will not call the `delete()`
96+
method of the `Attachment` model class.
97+
98+
```php
99+
$user = App\User::first();
100+
$attachmentByKey = $user->attachment('myKey');
101+
$attachmentByKey->delete(); // Will also delete the file on the storage by default
102+
```
103+
104+
87105
## Hooking the file output
88106

89107
The `Bnb\Laravel\Attachments\Attachment` model class provides
@@ -197,4 +215,15 @@ Route::post('/upload', function () {
197215

198216
return redirect('/dropzone');
199217
});
200-
```
218+
```
219+
220+
## Cleanup commands
221+
222+
A command is provided to cleanup the attachments not bound to a model
223+
(when `model_type` and `model_id` are `null`).
224+
225+
php artisan attachment:cleanup
226+
227+
The `-s` (or `--since=[timeInMinutes]`) option can be set to specify
228+
another time limit in minutes : only unbound files older than the
229+
specified age will be deleted. This value is set to **1440** by default.

src/AttachmentsServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Bnb\Laravel\Attachments;
44

5+
use Bnb\Laravel\Attachments\Console\Commands\CleanupAttachments;
56
use Illuminate\Routing\Router;
67
use Illuminate\Support\ServiceProvider;
78

@@ -29,6 +30,12 @@ public function boot(Router $router)
2930
if (config('attachments.routes.publish')) {
3031
$this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
3132
}
33+
34+
if ($this->app->runningInConsole()) {
35+
$this->commands([
36+
CleanupAttachments::class,
37+
]);
38+
}
3239
}
3340

3441

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* laravel
4+
*
5+
* @author Jérémy GAULIN <[email protected]>
6+
* @copyright 2017 - B&B Web Expertise
7+
*/
8+
9+
namespace Bnb\Laravel\Attachments\Console\Commands;
10+
11+
use Bnb\Laravel\Attachments\Attachment;
12+
use Carbon\Carbon;
13+
use Illuminate\Console\Command;
14+
use Illuminate\Support\Collection;
15+
use Lang;
16+
use Symfony\Component\Console\Input\InputOption;
17+
18+
class CleanupAttachments extends Command
19+
{
20+
21+
protected $signature = 'attachments:cleanup';
22+
23+
24+
public function __construct()
25+
{
26+
$this->description = Lang::get('attachments::messages.console.cleanup_description');
27+
28+
parent::__construct();
29+
30+
$this->getDefinition()->addOption(new InputOption('since', '-s', InputOption::VALUE_OPTIONAL,
31+
Lang::get('attachments::messages.console.cleanup_option_since'), 1440));
32+
}
33+
34+
35+
public function handle()
36+
{
37+
if ($this->confirm(Lang::get('attachments::messages.console.cleanup_confirm'))) {
38+
$query = Attachment::query()
39+
->whereNull('model_type')
40+
->whereNull('model_id')
41+
->where('updated_at', '<=', Carbon::now()->addMinutes(-1 * $this->option('since')));
42+
43+
$progress = $this->output->createProgressBar($count = $query->count());
44+
45+
if ($count) {
46+
$query->chunk(100, function ($attachements) use ($progress) {
47+
/** @var Collection $attachements */
48+
$attachements->each(function ($attachement) use ($progress) {
49+
/** @var Attachment $attachement */
50+
$attachement->delete();
51+
52+
$progress->advance();
53+
});
54+
});
55+
56+
$this->info(Lang::get('attachments::messages.console.done'));
57+
} else {
58+
$this->comment(Lang::get('attachments::messages.console.cleanup_no_data'));
59+
}
60+
}
61+
}
62+
}

translations/en/messages.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@
55
'access_denied' => 'Access denied.',
66
'not_found' => 'Not found.',
77
'upload_failed' => 'Upload has failed.'
8-
]
8+
],
9+
'console' => [
10+
'done' => 'Done!',
11+
'cleanup_description' => 'Cleanup attachments not bound to a model instance.',
12+
'cleanup_confirm' => 'Do you confirm the deletion of unbound attachments ?',
13+
'cleanup_option_since' => 'Minimum age (in minutes) of the attachment to delete (based on modification date).',
14+
'cleanup_no_data' => 'No attachment to handle.',
15+
],
916
];

translations/fr/messages.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@
55
'access_denied' => 'Vous n’êtes pas autorisé à accéder à la ressource demandée.',
66
'not_found' => 'La ressource demandé est introuvable.',
77
'upload_failed' => 'L’envoi de la ressource a échoué.'
8-
]
8+
],
9+
'console' => [
10+
'done' => 'Terminé !',
11+
'cleanup_description' => 'Nettoie les pièces jointes non liées à un modèle.',
12+
'cleanup_confirm' => 'Confirmez-vous vouloir supprimer les pièces jointes non liées à un modèle ?',
13+
'cleanup_option_since' => 'Âge minimum (en minutes) des données à supprimer (se base sur la date de modification).',
14+
'cleanup_no_data' => 'Aucune pièce jointe à traiter.',
15+
],
916
];

0 commit comments

Comments
 (0)