Add endpoints required to store the results of a backup

This commit is contained in:
Dane Everitt 2020-04-04 16:07:09 -07:00
parent ad9194a65c
commit 15b436d26e
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 104 additions and 0 deletions

View file

@ -0,0 +1,70 @@
<?php
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\Eloquent\BackupRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Http\Requests\Api\Remote\ReportBackupCompleteRequest;
class ServerBackupController extends Controller
{
/**
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
*/
private $repository;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/
private $serverRepository;
/**
* ServerBackupController constructor.
*
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $serverRepository
*/
public function __construct(BackupRepository $repository, ServerRepository $serverRepository)
{
$this->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);
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Remote;
use Illuminate\Foundation\Http\FormRequest;
class ReportBackupCompleteRequest extends FormRequest
{
/**
* @return string[]
*/
public function rules()
{
return [
'successful' => 'boolean',
'sha256_hash' => 'string|required_if:successful,true',
'file_size' => 'numeric|required_if:successful,true',
];
}
}

View file

@ -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.

View file

@ -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');
});