Skip to content

Commit 176b7bf

Browse files
authored
Merge pull request #14 from throwexceptions/master
New command for purging exports.
2 parents e9a0b1c + c361b7a commit 176b7bf

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Storage;
7+
8+
class DataTablesPurgeExportCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'datatables:purge-export';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Remove exported files that datatables-export generate.';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return void
28+
*/
29+
public function handle()
30+
{
31+
collect(Storage::listContents('exports'))
32+
->each(function ($file) {
33+
if ($file['timestamp'] < now()->subDay(config('datatables-export.purge.days'))->getTimestamp()) {
34+
Storage::delete($file['path']);
35+
}
36+
});
37+
38+
$this->info('The command was successful. Export files are cleared!');
39+
}
40+
}

src/ExportServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\ServiceProvider;
66
use Livewire\Livewire;
77
use Maatwebsite\Excel\ExcelServiceProvider;
8+
use Yajra\DataTables\Commands\DataTablesPurgeExportCommand;
89
use Yajra\DataTables\Generators\DataTablesHtmlCommand;
910
use Yajra\DataTables\Generators\DataTablesMakeCommand;
1011
use Yajra\DataTables\Generators\DataTablesScopeCommand;
@@ -48,5 +49,7 @@ protected function publishAssets()
4849
public function register()
4950
{
5051
$this->mergeConfigFrom(__DIR__ . '/config/datatables-export.php', 'datatables-export');
52+
53+
$this->commands([DataTablesPurgeExportCommand::class]);
5154
}
5255
}

src/config/datatables-export.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,12 @@
3535
NumberFormat::FORMAT_DATE_XLSX17,
3636
NumberFormat::FORMAT_DATE_YYYYMMDD2,
3737
NumberFormat::FORMAT_DATE_YYYYMMDDSLASH,
38-
]
38+
],
39+
40+
/**
41+
* Purge all exported by purge.days old files.
42+
*/
43+
'purge' => [
44+
'days' => 1,
45+
],
3946
];

0 commit comments

Comments
 (0)