From 15b436d26e8e0219c672c741242816cbfbf85ab8 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 4 Apr 2020 16:07:09 -0700 Subject: [PATCH] Add endpoints required to store the results of a backup --- .../Remote/Servers/ServerBackupController.php | 70 +++++++++++++++++++ .../Remote/ReportBackupCompleteRequest.php | 20 ++++++ .../Eloquent/EloquentRepository.php | 12 ++++ routes/api-remote.php | 2 + 4 files changed, 104 insertions(+) create mode 100644 app/Http/Controllers/Api/Remote/Servers/ServerBackupController.php create mode 100644 app/Http/Requests/Api/Remote/ReportBackupCompleteRequest.php diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerBackupController.php b/app/Http/Controllers/Api/Remote/Servers/ServerBackupController.php new file mode 100644 index 000000000..b4599ee0a --- /dev/null +++ b/app/Http/Controllers/Api/Remote/Servers/ServerBackupController.php @@ -0,0 +1,70 @@ +repository = $repository; + $this->serverRepository = $serverRepository; + } + + /** + * Updates a server backup's state in the database depending on wether or not + * it was successful. + * + * @param \Pterodactyl\Http\Requests\Api\Remote\ReportBackupCompleteRequest $request + * @param string $uuid + * @param string $backup + * @return \Illuminate\Http\JsonResponse + * + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + */ + public function __invoke(ReportBackupCompleteRequest $request, string $uuid, string $backup) + { + $server = $this->serverRepository->getByUuid($uuid); + + $where = [ + ['uuid', '=', $backup], + ['server_id', '=', $server->id], + ]; + + if ($request->input('successful')) { + $this->repository->updateWhere($where, [ + 'sha256_hash' => $request->input('sha256_hash'), + 'bytes' => $request->input('file_size'), + 'completed_at' => Carbon::now(), + ]); + } else { + $this->repository->deleteWhere($where); + } + + return JsonResponse::create([], JsonResponse::HTTP_NO_CONTENT); + } +} diff --git a/app/Http/Requests/Api/Remote/ReportBackupCompleteRequest.php b/app/Http/Requests/Api/Remote/ReportBackupCompleteRequest.php new file mode 100644 index 000000000..b0383b963 --- /dev/null +++ b/app/Http/Requests/Api/Remote/ReportBackupCompleteRequest.php @@ -0,0 +1,20 @@ + 'boolean', + 'sha256_hash' => 'string|required_if:successful,true', + 'file_size' => 'numeric|required_if:successful,true', + ]; + } +} diff --git a/app/Repositories/Eloquent/EloquentRepository.php b/app/Repositories/Eloquent/EloquentRepository.php index 5b22c4c32..73e671cc6 100644 --- a/app/Repositories/Eloquent/EloquentRepository.php +++ b/app/Repositories/Eloquent/EloquentRepository.php @@ -177,6 +177,18 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf return ($this->withFresh) ? $instance->fresh() : $saved; } + /** + * Update a model using the attributes passed. + * + * @param array|\Closure $attributes + * @param array $values + * @return int + */ + public function updateWhere($attributes, array $values) + { + return $this->getBuilder()->where($attributes)->update($values); + } + /** * Perform a mass update where matching records are updated using whereIn. * This does not perform any model data validation. diff --git a/routes/api-remote.php b/routes/api-remote.php index 4fb77b291..91ea76b96 100644 --- a/routes/api-remote.php +++ b/routes/api-remote.php @@ -11,4 +11,6 @@ Route::group(['prefix' => '/servers/{uuid}'], function () { Route::get('/', 'Servers\ServerDetailsController'); Route::get('/install', 'Servers\ServerInstallController@index'); Route::post('/install', 'Servers\ServerInstallController@store'); + + Route::post('/backup/{backup}', 'Servers\ServerBackupController'); });