2020-04-04 17:59:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-04-04 17:59:25 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2021-01-17 19:46:08 +00:00
|
|
|
use Pterodactyl\Models\AuditLog;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Pterodactyl\Services\Backups\DeleteBackupService;
|
2020-04-10 05:35:38 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
2020-04-04 19:26:39 +00:00
|
|
|
use Pterodactyl\Services\Backups\InitiateBackupService;
|
2020-04-04 17:59:25 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\BackupTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Backups\GetBackupsRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Backups\StoreBackupRequest;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Backups\DeleteBackupRequest;
|
2020-04-04 17:59:25 +00:00
|
|
|
|
|
|
|
class BackupController extends ClientApiController
|
|
|
|
{
|
2020-04-04 19:26:39 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Backups\InitiateBackupService
|
|
|
|
*/
|
|
|
|
private $initiateBackupService;
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Backups\DeleteBackupService
|
|
|
|
*/
|
|
|
|
private $deleteBackupService;
|
|
|
|
|
2020-04-10 05:35:38 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-04-04 19:26:39 +00:00
|
|
|
/**
|
|
|
|
* BackupController constructor.
|
|
|
|
*
|
2020-04-10 05:35:38 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
|
2020-04-10 05:08:09 +00:00
|
|
|
* @param \Pterodactyl\Services\Backups\DeleteBackupService $deleteBackupService
|
2020-04-04 19:26:39 +00:00
|
|
|
* @param \Pterodactyl\Services\Backups\InitiateBackupService $initiateBackupService
|
|
|
|
*/
|
2020-04-10 05:35:38 +00:00
|
|
|
public function __construct(
|
|
|
|
BackupRepository $repository,
|
|
|
|
DeleteBackupService $deleteBackupService,
|
|
|
|
InitiateBackupService $initiateBackupService
|
|
|
|
) {
|
2020-04-04 17:59:25 +00:00
|
|
|
parent::__construct();
|
2020-04-04 19:26:39 +00:00
|
|
|
|
|
|
|
$this->initiateBackupService = $initiateBackupService;
|
2020-04-10 05:08:09 +00:00
|
|
|
$this->deleteBackupService = $deleteBackupService;
|
2020-04-10 05:35:38 +00:00
|
|
|
$this->repository = $repository;
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all of the backups for a given server instance in a paginated
|
|
|
|
* result set.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Backups\GetBackupsRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function index(GetBackupsRequest $request, Server $server)
|
|
|
|
{
|
2020-12-06 20:01:42 +00:00
|
|
|
$limit = min($request->query('per_page') ?? 20, 50);
|
2021-01-17 19:46:08 +00:00
|
|
|
|
2020-12-06 20:01:42 +00:00
|
|
|
return $this->fractal->collection($server->backups()->paginate($limit))
|
2020-04-04 17:59:25 +00:00
|
|
|
->transformWith($this->getTransformer(BackupTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the backup process for a server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Backups\StoreBackupRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2020-04-04 19:26:39 +00:00
|
|
|
* @return array
|
|
|
|
*
|
2020-04-10 05:08:09 +00:00
|
|
|
* @throws \Exception|\Throwable
|
2020-04-04 17:59:25 +00:00
|
|
|
*/
|
|
|
|
public function store(StoreBackupRequest $request, Server $server)
|
|
|
|
{
|
2021-01-17 19:46:08 +00:00
|
|
|
/** @var \Pterodactyl\Models\Backup $backup */
|
2021-01-17 23:25:49 +00:00
|
|
|
$backup = $server->audit(AuditLog::SERVER__BACKUP_STARTED, function (AuditLog $model, Server $server) use ($request) {
|
2021-01-17 19:46:08 +00:00
|
|
|
$backup = $this->initiateBackupService
|
|
|
|
->setIgnoredFiles(
|
|
|
|
explode(PHP_EOL, $request->input('ignored') ?? '')
|
|
|
|
)
|
|
|
|
->handle($server, $request->input('name'));
|
|
|
|
|
|
|
|
$model->metadata = ['backup_uuid' => $backup->uuid];
|
|
|
|
|
|
|
|
return $backup;
|
|
|
|
});
|
2020-04-04 19:26:39 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($backup)
|
|
|
|
->transformWith($this->getTransformer(BackupTransformer::class))
|
|
|
|
->toArray();
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
/**
|
|
|
|
* Returns information about a single backup.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Backups\GetBackupsRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param \Pterodactyl\Models\Backup $backup
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function view(GetBackupsRequest $request, Server $server, Backup $backup)
|
2020-04-04 17:59:25 +00:00
|
|
|
{
|
2020-04-10 05:08:09 +00:00
|
|
|
return $this->fractal->item($backup)
|
|
|
|
->transformWith($this->getTransformer(BackupTransformer::class))
|
|
|
|
->toArray();
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
/**
|
|
|
|
* Deletes a backup from the panel as well as the remote source where it is currently
|
|
|
|
* being stored.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Backups\DeleteBackupRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param \Pterodactyl\Models\Backup $backup
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function delete(DeleteBackupRequest $request, Server $server, Backup $backup)
|
2020-04-04 17:59:25 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$server->audit(AuditLog::SERVER__BACKUP_DELETED, function (AuditLog $audit) use ($backup) {
|
|
|
|
$audit->metadata = ['backup_uuid' => $backup->uuid];
|
|
|
|
|
2021-01-17 19:46:08 +00:00
|
|
|
$this->deleteBackupService->handle($backup);
|
|
|
|
});
|
2020-04-04 17:59:25 +00:00
|
|
|
|
2021-01-17 19:46:08 +00:00
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
|
|
|
}
|