Added /backups/latest endpoint

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-02-19 14:35:09 +00:00
parent 10bb844087
commit 8f8edd4126
2 changed files with 47 additions and 1 deletions

View file

@ -261,7 +261,6 @@ class SettingsController extends Controller
$count++;
}
}
}
@ -271,6 +270,14 @@ class SettingsController extends Controller
}
/**
* Downloads a backup file.
* We use response()->download() here instead of Storage::download() because Storage::download()
* exhausts memory on larger files.
*
* @author [A. Gianotto]
* @return JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function downloadBackup($file) {
$path = storage_path('app/backups');
@ -283,4 +290,36 @@ class SettingsController extends Controller
}
}
/**
* Determines and downloads the latest backup
*
* @author [A. Gianotto]
* @since [v6.3.1]
* @return JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function downloadLatestBackup() {
$fileData = collect();
foreach (Storage::files('app/backups') as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) == 'zip') {
$fileData->push([
'file' => $file,
'date' => Storage::lastModified($file)
]);
}
}
$newest = $fileData->sortByDesc('date')->first();
if (Storage::exists($newest['file'])) {
$headers = ['ContentType' => 'application/zip'];
return response()->download(storage_path($newest['file']), basename($newest['file']), $headers);
} else {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.file_not_found')), 404);
}
}
}

View file

@ -828,6 +828,13 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functi
]
)->name('api.settings.backups.index');
Route::get('backups/download/latest',
[
Api\SettingsController::class,
'downloadLatestBackup'
]
)->name('api.settings.backups.latest');
Route::get('backups/download/{file}',
[
Api\SettingsController::class,