|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PavelMironchik\LaravelBackupPanel\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Support\Facades\Artisan; |
|
9
|
|
|
use Illuminate\Support\Facades\Log; |
|
10
|
|
|
use Illuminate\Support\Facades\Storage; |
|
11
|
|
|
use League\Flysystem\Adapter\Local; |
|
12
|
|
|
|
|
13
|
|
|
class BackupController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
public function files() |
|
16
|
|
|
{ |
|
17
|
|
|
$diskNames = config('backup.backup.destination.disks'); |
|
18
|
|
|
|
|
19
|
|
|
if (! count($diskNames)) { |
|
20
|
|
|
return ['error' => 'No disks configured!']; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$data = []; |
|
24
|
|
|
foreach ($diskNames as $diskName) { |
|
25
|
|
|
$disk = Storage::disk($diskName); |
|
26
|
|
|
$adapter = $disk->getDriver()->getAdapter(); |
|
27
|
|
|
$files = $disk->allFiles(); |
|
28
|
|
|
|
|
29
|
|
|
foreach ($files as $file) { |
|
30
|
|
|
if (substr($file, -4) == '.zip') { |
|
31
|
|
|
$data[] = [ |
|
32
|
|
|
'path' => $file, |
|
33
|
|
|
'name' => pathinfo($file, PATHINFO_BASENAME), |
|
34
|
|
|
'size' => round((int) $disk->size($file) / 1048576, 2).' MB', |
|
35
|
|
|
'disk' => $diskName, |
|
36
|
|
|
'download' => ($adapter instanceof Local) ? true : false, |
|
37
|
|
|
'date' => Carbon::createFromTimeStamp($disk->lastModified($file)) |
|
38
|
|
|
->formatLocalized('%d %B %Y, %H:%M'), |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$data = array_reverse($data); |
|
45
|
|
|
|
|
46
|
|
|
return ['files' => $data]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function download(Request $request) |
|
50
|
|
|
{ |
|
51
|
|
|
$disk = Storage::disk($request->input('disk')); |
|
52
|
|
|
$path = $request->input('path'); |
|
53
|
|
|
|
|
54
|
|
|
$adapter = $disk->getDriver()->getAdapter(); |
|
55
|
|
|
if (! $adapter instanceof Local) { |
|
56
|
|
|
abort(404, 'Only files from local disk can be downloaded!'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (! $disk->exists($path)) { |
|
60
|
|
|
abort(404, 'There is no such file!'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$storage_path = $disk->getDriver()->getAdapter()->getPathPrefix(); |
|
64
|
|
|
|
|
65
|
|
|
return response()->download($storage_path.$path); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function delete(Request $request) |
|
69
|
|
|
{ |
|
70
|
|
|
$disk = Storage::disk($request->input('disk')); |
|
71
|
|
|
$path = $request->input('path'); |
|
72
|
|
|
|
|
73
|
|
|
if (! $disk->exists($path)) { |
|
74
|
|
|
return ['error' => 'There is no such file!']; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$disk->delete($path); |
|
78
|
|
|
|
|
79
|
|
|
return 'success'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function create() |
|
83
|
|
|
{ |
|
84
|
|
|
try { |
|
85
|
|
|
Artisan::call('backup:run'); |
|
86
|
|
|
|
|
87
|
|
|
$output = Artisan::output(); |
|
88
|
|
|
if (strpos($output, 'Backup failed because')) { |
|
89
|
|
|
preg_match('/Backup failed because(.*?)$/ms', $output, $match); |
|
90
|
|
|
Log::error($output); |
|
91
|
|
|
|
|
92
|
|
|
return response(['error' => $match[0]], 500); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return 'success'; |
|
96
|
|
|
} catch (Exception $exception) { |
|
97
|
|
|
Log::error($exception); |
|
98
|
|
|
|
|
99
|
|
|
return response(['error' => $exception->getMessage()], 500); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|