-
-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathDeleteTemporaryFileUploads.php
More file actions
41 lines (31 loc) · 967 Bytes
/
DeleteTemporaryFileUploads.php
File metadata and controls
41 lines (31 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace Statamic\Http\Middleware;
use Closure;
use Statamic\Facades\File;
class DeleteTemporaryFileUploads
{
public function handle($request, Closure $next)
{
if (! $request->inertia() && $request->ajax()) {
return $next($request);
}
$lottery = [2, 100];
if (random_int(1, $lottery[1]) <= $lottery[0]) {
$this->deleteFilesOverAnHourOld();
}
return $next($request);
}
private function deleteFilesOverAnHourOld()
{
$disk = File::disk('local');
$disk
->getFilesRecursively($dir = 'statamic/file-uploads')
->filter(function ($path) {
$bits = explode('/', $path);
$timestamp = $bits[count($bits) - 2];
return $timestamp < now()->subHour()->timestamp;
})
->each(fn ($path) => $disk->delete($path));
$disk->deleteEmptySubfolders($dir);
}
}