misc_pterodactyl-panel/app/Http/Controllers/Api/Remote/Backups/BackupStatusController.php

99 lines
3.5 KiB
PHP
Raw Normal View History

2020-04-17 17:21:51 +00:00
<?php
namespace Pterodactyl\Http\Controllers\Api\Remote\Backups;
use Carbon\CarbonImmutable;
use Pterodactyl\Models\Backup;
2020-04-17 17:21:51 +00:00
use Illuminate\Http\JsonResponse;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
2020-04-17 17:21:51 +00:00
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Extensions\Backups\BackupManager;
2020-04-17 17:21:51 +00:00
use Pterodactyl\Repositories\Eloquent\BackupRepository;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2020-04-17 17:21:51 +00:00
use Pterodactyl\Http\Requests\Api\Remote\ReportBackupCompleteRequest;
class BackupStatusController extends Controller
{
/**
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
*/
private $repository;
/**
* @var \Pterodactyl\Extensions\Backups\BackupManager
*/
private $backupManager;
2020-04-17 17:21:51 +00:00
/**
* BackupStatusController constructor.
*
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
* @param \Pterodactyl\Extensions\Backups\BackupManager $backupManager
2020-04-17 17:21:51 +00:00
*/
public function __construct(BackupRepository $repository, BackupManager $backupManager)
2020-04-17 17:21:51 +00:00
{
$this->repository = $repository;
$this->backupManager = $backupManager;
2020-04-17 17:21:51 +00:00
}
/**
* Handles updating the state of a backup.
*
* @param \Pterodactyl\Http\Requests\Api\Remote\ReportBackupCompleteRequest $request
* @param string $backup
*
2020-04-17 17:21:51 +00:00
* @return \Illuminate\Http\JsonResponse
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Exception
2020-04-17 17:21:51 +00:00
*/
public function __invoke(ReportBackupCompleteRequest $request, string $backup)
{
/** @var \Pterodactyl\Models\Backup $model */
$model = $this->repository->findFirstWhere([[ 'uuid', '=', $backup ]]);
if (! is_null($model->completed_at)) {
throw new BadRequestHttpException(
'Cannot update the status of a backup that is already marked as completed.'
);
}
2020-08-28 02:59:01 +00:00
$successful = $request->input('successful') ? true : false;
2020-08-28 02:59:01 +00:00
$model->forceFill([
'is_successful' => $successful,
'checksum' => $successful ? ($request->input('checksum_type') . ':' . $request->input('checksum')) : null,
'bytes' => $successful ? $request->input('size') : 0,
'completed_at' => CarbonImmutable::now(),
2020-08-28 02:59:01 +00:00
])->save();
2020-04-17 17:21:51 +00:00
// Check if we are using the s3 backup adapter.
$adapter = $this->backupManager->adapter();
if ($adapter instanceof AwsS3Adapter) {
/** @var \Pterodactyl\Models\Backup $backup */
$backup = Backup::query()->where('uuid', $backup)->firstOrFail();
$client = $adapter->getClient();
$params = [
'Bucket' => $adapter->getBucket(),
'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),
'UploadId' => $request->input('upload_id'),
];
// If the backup was not successful, send an AbortMultipartUpload request.
if (! $successful) {
$client->execute($client->getCommand('AbortMultipartUpload', $params));
2020-12-06 22:19:54 +00:00
} else {
// Otherwise send a CompleteMultipartUpload request.
$params['MultipartUpload'] = [
'Parts' => $client->execute($client->getCommand('ListParts', $params))['Parts'],
];
$client->execute($client->getCommand('CompleteMultipartUpload', $params));
}
}
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
2020-04-17 17:21:51 +00:00
}
}