2020-04-17 10:21:51 -07:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-04-17 10:21:51 -07:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Backups;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Contracts\Backups\CompletesBackups;
|
|
|
|
|
use Pterodactyl\Contracts\Backups\RestoresBackupStatuses;
|
|
|
|
|
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
|
2022-05-29 16:19:04 -04:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2021-01-23 12:09:16 -08:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Http\Requests\Api\Remote\RemoteRequestNode;
|
2020-04-17 10:21:51 -07:00
|
|
|
use Pterodactyl\Http\Requests\Api\Remote\ReportBackupCompleteRequest;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Http\Requests\Api\Remote\ReportBackupRestoreRequest;
|
|
|
|
|
use Pterodactyl\Models\Backup;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
|
use Throwable;
|
2020-04-17 10:21:51 -07:00
|
|
|
|
|
|
|
|
class BackupStatusController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Handles updating the state of a backup.
|
|
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @throws Throwable
|
2020-04-17 10:21:51 -07:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function index(ReportBackupCompleteRequest $request, CompletesBackups $completeBackup, string $backup): JsonResponse
|
2020-04-17 10:21:51 -07:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$node = RemoteRequestNode::get($request);
|
2024-04-10 17:38:09 -06:00
|
|
|
|
2024-04-11 10:42:18 -06:00
|
|
|
$model = Backup::query()
|
2024-04-10 17:38:09 -06:00
|
|
|
->where('uuid', $backup)
|
|
|
|
|
->firstOrFail();
|
2020-08-27 19:35:22 -07:00
|
|
|
|
2024-05-04 16:06:13 -06:00
|
|
|
// Check that the backup is "owned" by the node making the request. This avoids other nodes
|
|
|
|
|
// from messing with backups that they don't own.
|
|
|
|
|
$server = $model->server;
|
2026-10-05 18:10:18 -04:00
|
|
|
throw_if($server->node_id !== $node->id, HttpForbiddenException::class, 'Requesting node does not have permission to access this server.');
|
2024-05-04 16:06:13 -06:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
throw_if($model->is_successful, BadRequestHttpException::class, 'Cannot update the status of a backup that is already marked as completed.');
|
2020-08-27 19:35:22 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$data = $request->payload();
|
2020-04-17 10:21:51 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$completeBackup->complete($model, $data);
|
2022-05-29 16:19:04 -04:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
Activity::event($data['successful'] ? 'server:backup.complete' : 'server:backup.fail')
|
|
|
|
|
->subject($model, $model->server)
|
|
|
|
|
->property('name', $model->name)
|
|
|
|
|
->log();
|
2020-12-06 13:53:55 -07:00
|
|
|
|
2020-12-27 11:56:28 -08:00
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
|
}
|
2020-12-06 13:53:55 -07:00
|
|
|
|
2021-01-18 21:14:49 -08:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* Resets the server status to null after a restore attempt - even a failed restore is
|
|
|
|
|
* recoverable by retrying or reinstalling, and the successful flag only selects which
|
|
|
|
|
* activity log event is recorded.
|
2021-01-18 21:14:49 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @throws Throwable
|
2021-01-18 21:14:49 -08:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function restore(ReportBackupRestoreRequest $request, RestoresBackupStatuses $restoreStatus, string $backup): JsonResponse
|
2021-01-18 21:14:49 -08:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$model = $restoreStatus->restoreStatus($backup, RemoteRequestNode::get($request));
|
2026-02-11 16:04:09 -08:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$successful = $request->payload()['successful'];
|
2022-05-29 16:19:04 -04:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
Activity::event($successful ? 'server:backup.restore-complete' : 'server:backup.restore-failed')
|
2022-05-29 16:19:04 -04:00
|
|
|
->subject($model, $model->server)
|
|
|
|
|
->property('name', $model->name)
|
|
|
|
|
->log();
|
2021-01-18 21:14:49 -08:00
|
|
|
|
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
|
}
|
2020-04-17 10:21:51 -07:00
|
|
|
}
|