Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Backups/DownloadLinkService.php
10260 views
1
<?php
2
3
namespace Pterodactyl\Services\Backups;
4
5
use Carbon\CarbonImmutable;
6
use Pterodactyl\Models\User;
7
use Pterodactyl\Models\Backup;
8
use Pterodactyl\Services\Nodes\NodeJWTService;
9
use Pterodactyl\Extensions\Backups\BackupManager;
10
11
class DownloadLinkService
12
{
13
/**
14
* DownloadLinkService constructor.
15
*/
16
public function __construct(private BackupManager $backupManager, private NodeJWTService $jwtService)
17
{
18
}
19
20
/**
21
* Returns the URL that allows for a backup to be downloaded by an individual
22
* user, or by the Wings control software.
23
*/
24
public function handle(Backup $backup, User $user): string
25
{
26
if ($backup->disk === Backup::ADAPTER_AWS_S3) {
27
return $this->getS3BackupUrl($backup);
28
}
29
30
$token = $this->jwtService
31
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
32
->setUser($user)
33
->setClaims([
34
'backup_uuid' => $backup->uuid,
35
'server_uuid' => $backup->server->uuid,
36
])
37
->handle($backup->server->node, $user->id . $backup->server->uuid);
38
39
return sprintf('%s/download/backup?token=%s', $backup->server->node->getConnectionAddress(), $token->toString());
40
}
41
42
/**
43
* Returns a signed URL that allows us to download a file directly out of a non-public
44
* S3 bucket by using a signed URL.
45
*/
46
protected function getS3BackupUrl(Backup $backup): string
47
{
48
/** @var \Pterodactyl\Extensions\Filesystem\S3Filesystem $adapter */
49
$adapter = $this->backupManager->adapter(Backup::ADAPTER_AWS_S3);
50
51
$request = $adapter->getClient()->createPresignedRequest(
52
$adapter->getClient()->getCommand('GetObject', [
53
'Bucket' => $adapter->getBucket(),
54
'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),
55
'ContentType' => 'application/x-gzip',
56
]),
57
CarbonImmutable::now()->addMinutes(5)
58
);
59
60
return $request->getUri()->__toString();
61
}
62
}
63
64