Skip to content

Commit 9991989

Browse files
committed
Very basic implemention of frontend logic required to display backups and create a new one
1 parent 17ec4ef commit 9991989

File tree

16 files changed

+431
-2
lines changed

16 files changed

+431
-2
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4+
5+
use Pterodactyl\Models\Server;
6+
use Pterodactyl\Transformers\Api\Client\BackupTransformer;
7+
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
8+
use Pterodactyl\Http\Requests\Api\Client\Servers\Backups\GetBackupsRequest;
9+
use Pterodactyl\Http\Requests\Api\Client\Servers\Backups\StoreBackupRequest;
10+
11+
class BackupController extends ClientApiController
12+
{
13+
public function __construct()
14+
{
15+
parent::__construct();
16+
}
17+
18+
/**
19+
* Returns all of the backups for a given server instance in a paginated
20+
* result set.
21+
*
22+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Backups\GetBackupsRequest $request
23+
* @param \Pterodactyl\Models\Server $server
24+
* @return array
25+
*/
26+
public function index(GetBackupsRequest $request, Server $server)
27+
{
28+
return $this->fractal->collection($server->backups()->paginate(20))
29+
->transformWith($this->getTransformer(BackupTransformer::class))
30+
->toArray();
31+
}
32+
33+
/**
34+
* Starts the backup process for a server.
35+
*
36+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Backups\StoreBackupRequest $request
37+
* @param \Pterodactyl\Models\Server $server
38+
*/
39+
public function store(StoreBackupRequest $request, Server $server)
40+
{
41+
}
42+
43+
public function view()
44+
{
45+
}
46+
47+
public function update()
48+
{
49+
}
50+
51+
public function delete()
52+
{
53+
}
54+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Backups;
4+
5+
use Pterodactyl\Models\Permission;
6+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7+
8+
class GetBackupsRequest extends ClientApiRequest
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function permission()
14+
{
15+
return Permission::ACTION_BACKUP_READ;
16+
}
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Backups;
4+
5+
use Pterodactyl\Models\Permission;
6+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7+
8+
class StoreBackupRequest extends ClientApiRequest
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function permission()
14+
{
15+
return Permission::ACTION_BACKUP_CREATE;
16+
}
17+
18+
/**
19+
* @return array
20+
*/
21+
public function rules(): array
22+
{
23+
return [
24+
'name' => 'nullable|string|max:255',
25+
'ignore' => 'nullable|string',
26+
];
27+
}
28+
}

app/Models/Backup.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@
66

77
/**
88
* @property int $id
9+
* @property int $server_id
910
* @property int $uuid
1011
* @property string $name
11-
* @property string $contents
12+
* @property string $ignore
1213
* @property string $disk
1314
* @property string|null $sha256_hash
1415
* @property int $bytes
1516
* @property \Carbon\CarbonImmutable|null $completed_at
1617
* @property \Carbon\CarbonImmutable $created_at
1718
* @property \Carbon\CarbonImmutable $updated_at
1819
* @property \Carbon\CarbonImmutable|null $deleted_at
20+
*
21+
* @property \Pterodactyl\Models\Server $server
1922
*/
2023
class Backup extends Model
2124
{
2225
use SoftDeletes;
2326

27+
const RESOURCE_NAME = 'backup';
28+
2429
/**
2530
* @var string
2631
*/
@@ -56,4 +61,12 @@ protected function asDateTime($value)
5661
{
5762
return $this->asImmutableDateTime($value);
5863
}
64+
65+
/**
66+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
67+
*/
68+
public function server()
69+
{
70+
return $this->belongsTo(Server::class);
71+
}
5972
}

app/Models/Permission.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class Permission extends Model
3737
const ACTION_USER_UPDATE = 'user.update';
3838
const ACTION_USER_DELETE = 'user.delete';
3939

40+
const ACTION_BACKUP_READ = 'backup.read';
41+
const ACTION_BACKUP_CREATE = 'backup.create';
42+
const ACTION_BACKUP_UPDATE = 'backup.update';
43+
const ACTION_BACKUP_DELETE = 'backup.delete';
44+
const ACTION_BACKUP_DOWNLOAD = 'backup.download';
45+
4046
const ACTION_ALLOCATION_READ = 'allocation.read';
4147
const ACTION_ALLOCIATION_UPDATE = 'allocation.update';
4248

@@ -135,6 +141,17 @@ class Permission extends Model
135141
],
136142
],
137143

144+
'backup' => [
145+
'description' => 'Permissions that control a user\'s ability to generate and manage server backups.',
146+
'keys' => [
147+
'create' => 'Allows a user to create new backups for this server.',
148+
'read' => 'Allows a user to view all backups that exist for this server.',
149+
'update' => '',
150+
'delete' => 'Allows a user to remove backups from the system.',
151+
'download' => 'Allows a user to download backups.',
152+
],
153+
],
154+
138155
// Controls permissions for editing or viewing a server's allocations.
139156
'allocation' => [
140157
'description' => 'Permissions that control a user\'s ability to modify the port allocations for this server.',

app/Models/Server.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @property \Pterodactyl\Models\Location $location
5252
* @property \Pterodactyl\Models\DaemonKey $key
5353
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
54+
* @property \Pterodactyl\Models\Backup[]|\Illuminate\Database\Eloquent\Collection $backups
5455
*/
5556
class Server extends Model
5657
{
@@ -339,4 +340,12 @@ public function keys()
339340
{
340341
return $this->hasMany(DaemonKey::class);
341342
}
343+
344+
/**
345+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
346+
*/
347+
public function backups()
348+
{
349+
return $this->hasMany(Backup::class);
350+
}
342351
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Pterodactyl\Transformers\Api\Client;
4+
5+
use Pterodactyl\Models\Backup;
6+
7+
class BackupTransformer extends BaseClientTransformer
8+
{
9+
/**
10+
* @return string
11+
*/
12+
public function getResourceName(): string
13+
{
14+
return Backup::RESOURCE_NAME;
15+
}
16+
17+
/**
18+
* @param \Pterodactyl\Models\Backup $backup
19+
* @return array
20+
*/
21+
public function transform(Backup $backup)
22+
{
23+
return [
24+
'uuid' => $backup->uuid,
25+
'name' => $backup->name,
26+
'ignore' => $backup->ignore,
27+
'sha256_hash' => $backup->sha256_hash,
28+
'bytes' => $backup->bytes,
29+
'created_at' => $backup->created_at->toIso8601String(),
30+
'completed_at' => $backup->completed_at->toIso8601String(),
31+
];
32+
}
33+
}

database/migrations/2020_04_03_230614_create_backups_table.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ public function up()
1515
{
1616
Schema::create('backups', function (Blueprint $table) {
1717
$table->bigIncrements('id');
18+
$table->unsignedInteger('server_id');
1819
$table->char('uuid', 36);
1920
$table->string('name');
20-
$table->text('contents');
21+
$table->text('ignored');
2122
$table->string('disk');
2223
$table->string('sha256_hash')->nullable();
2324
$table->integer('bytes')->default(0);
2425
$table->timestamp('completed_at')->nullable();
2526
$table->timestamps();
2627
$table->softDeletes();
28+
29+
$table->unique('uuid');
30+
$table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade');
2731
});
2832
}
2933

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { rawDataToServerBackup, ServerBackup } from '@/api/server/backups/getServerBackups';
2+
import http from '@/api/http';
3+
4+
export default (uuid: string, name?: string, ignore?: string): Promise<ServerBackup> => {
5+
return new Promise((resolve, reject) => {
6+
http.post(`/api/client/servers/${uuid}/backups`, {
7+
name, ignore,
8+
})
9+
.then(({ data }) => resolve(rawDataToServerBackup(data.attributes)))
10+
.catch(reject);
11+
});
12+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import http, { FractalResponseData, getPaginationSet, PaginatedResult } from '@/api/http';
2+
3+
export interface ServerBackup {
4+
uuid: string;
5+
name: string;
6+
contents: string;
7+
sha256Hash: string;
8+
bytes: number;
9+
createdAt: Date;
10+
completedAt: Date | null;
11+
}
12+
13+
export const rawDataToServerBackup = ({ attributes }: FractalResponseData): ServerBackup => ({
14+
uuid: attributes.uuid,
15+
name: attributes.name,
16+
contents: attributes.contents,
17+
sha256Hash: attributes.sha256_hash,
18+
bytes: attributes.bytes,
19+
createdAt: new Date(attributes.created_at),
20+
completedAt: attributes.completed_at ? new Date(attributes.completed_at) : null,
21+
});
22+
23+
export default (uuid: string, page?: number | string): Promise<PaginatedResult<ServerBackup>> => {
24+
return new Promise((resolve, reject) => {
25+
http.get(`/api/client/servers/${uuid}/backups`, { params: { page } })
26+
.then(({ data }) => resolve({
27+
items: (data.data || []).map(rawDataToServerBackup),
28+
pagination: getPaginationSet(data.meta.pagination),
29+
}))
30+
.catch(reject);
31+
});
32+
};

0 commit comments

Comments
 (0)