diff --git a/app/Http/Controllers/Admin/Backup/ApiController.php b/app/Http/Controllers/Admin/Backup/ApiController.php deleted file mode 100644 index 049a69e8..00000000 --- a/app/Http/Controllers/Admin/Backup/ApiController.php +++ /dev/null @@ -1,21 +0,0 @@ -json([ - 'success' => true, - ]); - } -} diff --git a/app/Http/Controllers/Admin/Backup/BackupsController.php b/app/Http/Controllers/Admin/Backup/BackupsController.php index 3b9ef7c5..91f5772a 100644 --- a/app/Http/Controllers/Admin/Backup/BackupsController.php +++ b/app/Http/Controllers/Admin/Backup/BackupsController.php @@ -1,28 +1,22 @@ authorize('manage backups'); @@ -69,12 +63,6 @@ class BackupsController extends ApiController } } - /** - * Store a newly created resource in storage. - * - * - * @throws AuthorizationException - */ public function store(Request $request): JsonResponse { $this->authorize('manage backups'); @@ -84,15 +72,9 @@ class BackupsController extends ApiController dispatch(new CreateBackupJob($data))->onQueue(config('backup.queue.name')); - return $this->respondSuccess(); + return response()->json(['success' => true]); } - /** - * Remove the specified resource from storage. - * - * - * @throws AuthorizationException - */ public function destroy($disk, Request $request): JsonResponse { $this->authorize('manage backups'); @@ -110,6 +92,41 @@ class BackupsController extends ApiController }) ->delete(); - return $this->respondSuccess(); + return response()->json(['success' => true]); + } + + public function download(Request $request): Response|StreamedResponse + { + $this->authorize('manage backups'); + + $validated = $request->validate([ + 'path' => ['required', new PathToZip], + ]); + + $backupDestination = BackupDestination::create(config('filesystems.default'), config('backup.backup.name')); + + $backup = $backupDestination->backups()->first(function (Backup $backup) use ($validated) { + return $backup->path() === $validated['path']; + }); + + if (! $backup) { + return response('Backup not found', 422); + } + + $fileName = pathinfo($backup->path(), PATHINFO_BASENAME); + + return response()->stream(function () use ($backup) { + $stream = $backup->stream(); + fpassthru($stream); + if (is_resource($stream)) { + fclose($stream); + } + }, 200, [ + 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', + 'Content-Type' => 'application/zip', + 'Content-Length' => $backup->sizeInBytes(), + 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', + 'Pragma' => 'public', + ]); } } diff --git a/app/Http/Controllers/Admin/Backup/DownloadBackupController.php b/app/Http/Controllers/Admin/Backup/DownloadBackupController.php deleted file mode 100644 index 90535154..00000000 --- a/app/Http/Controllers/Admin/Backup/DownloadBackupController.php +++ /dev/null @@ -1,70 +0,0 @@ -authorize('manage backups'); - - $validated = $request->validate([ - 'path' => ['required', new PathToZip], - ]); - - $backupDestination = BackupDestination::create(config('filesystems.default'), config('backup.backup.name')); - - $backup = $backupDestination->backups()->first(function (Backup $backup) use ($validated) { - return $backup->path() === $validated['path']; - }); - - if (! $backup) { - return response('Backup not found', Response::HTTP_UNPROCESSABLE_ENTITY); - } - - return $this->respondWithBackupStream($backup); - } - - /** - * Respond with backup stream - */ - public function respondWithBackupStream(Backup $backup): StreamedResponse - { - $fileName = pathinfo($backup->path(), PATHINFO_BASENAME); - - $downloadHeaders = [ - 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', - 'Content-Type' => 'application/zip', - 'Content-Length' => $backup->sizeInBytes(), - 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', - 'Pragma' => 'public', - ]; - - return response()->stream(function () use ($backup) { - $stream = $backup->stream(); - - fpassthru($stream); - - if (is_resource($stream)) { - fclose($stream); - } - }, 200, $downloadHeaders); - } -} diff --git a/routes/api.php b/routes/api.php index 0ed9e550..0795717f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,7 +1,6 @@ group(function () { Route::apiResource('/disks', DiskController::class); - Route::get('download-backup', DownloadBackupController::class); + Route::get('download-backup', [BackupsController::class, 'download']); Route::get('/disk/drivers', [DiskController::class, 'getDiskDrivers']);