mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-27 11:41:09 -04:00
build version 400
This commit is contained in:
23
app/Http/Controllers/V1/Backup/ApiController.php
Normal file
23
app/Http/Controllers/V1/Backup/ApiController.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
// Implementation taken from nova-backup-tool - https://github.com/spatie/nova-backup-tool/
|
||||
|
||||
namespace Crater\Http\Controllers\V1\Backup;
|
||||
|
||||
use Crater\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class ApiController extends Controller
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
|
||||
public function respondSuccess(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'success' => true
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
93
app/Http/Controllers/V1/Backup/BackupsController.php
Normal file
93
app/Http/Controllers/V1/Backup/BackupsController.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
// Implementation taken from nova-backup-tool - https://github.com/spatie/nova-backup-tool/
|
||||
|
||||
namespace Crater\Http\Controllers\V1\Backup;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Spatie\Backup\BackupDestination\Backup;
|
||||
use Spatie\Backup\BackupDestination\BackupDestination;
|
||||
use Spatie\Backup\Helpers\Format;
|
||||
use Crater\Jobs\CreateBackupJob;
|
||||
use Crater\Rules\Backup\BackupDisk;
|
||||
use Crater\Rules\Backup\PathToZip;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class BackupsController extends ApiController
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$configuredBackupDisks = config('backup.backup.destination.disks');
|
||||
|
||||
try {
|
||||
$backupDestination = BackupDestination::create(config('filesystems.default'), config('backup.backup.name'));
|
||||
|
||||
$backups = Cache::remember("backups-{$request->file_disk_id}", now()->addSeconds(4), function () use ($backupDestination) {
|
||||
return $backupDestination
|
||||
->backups()
|
||||
->map(function (Backup $backup) {
|
||||
return [
|
||||
'path' => $backup->path(),
|
||||
'created_at' => $backup->date()->format('Y-m-d H:i:s'),
|
||||
'size' => Format::humanReadableSize($backup->size()),
|
||||
];
|
||||
})
|
||||
->toArray();
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'backups' => $backups,
|
||||
'disks' => $configuredBackupDisks
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'backups' => [],
|
||||
'error' => 'invalid_disk_credentials',
|
||||
'error_message' => $e->getMessage(),
|
||||
'disks' => $configuredBackupDisks
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
dispatch(new CreateBackupJob($request->all()))->onQueue(config('backup.queue.name'));
|
||||
|
||||
return $this->respondSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function destroy($disk, Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'path' => ['required', new PathToZip()],
|
||||
]);
|
||||
|
||||
$backupDestination = BackupDestination::create(config('filesystems.default'), config('backup.backup.name'));
|
||||
|
||||
$backupDestination
|
||||
->backups()
|
||||
->first(function (Backup $backup) use ($validated) {
|
||||
return $backup->path() === $validated['path'];
|
||||
})
|
||||
->delete();
|
||||
|
||||
return $this->respondSuccess();
|
||||
}
|
||||
}
|
||||
58
app/Http/Controllers/V1/Backup/DownloadBackupController.php
Normal file
58
app/Http/Controllers/V1/Backup/DownloadBackupController.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
// Implementation taken from nova-backup-tool - https://github.com/spatie/nova-backup-tool/
|
||||
|
||||
namespace Crater\Http\Controllers\V1\Backup;
|
||||
|
||||
use Crater\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Spatie\Backup\BackupDestination\Backup;
|
||||
use Spatie\Backup\BackupDestination\BackupDestination;
|
||||
use Crater\Rules\Backup\BackupDisk;
|
||||
use Crater\Rules\Backup\PathToZip;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class DownloadBackupController extends ApiController
|
||||
{
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
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->size(),
|
||||
'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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user