Skip to content

Commit 95d19bf

Browse files
committed
Update logic that handles creation of folders for a server
1 parent ec87330 commit 95d19bf

File tree

13 files changed

+116
-9
lines changed

13 files changed

+116
-9
lines changed

app/Contracts/Repository/Daemon/FileRepositoryInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,13 @@ public function putContent(string $path, string $content): ResponseInterface;
4747
* @throws \GuzzleHttp\Exception\TransferException
4848
*/
4949
public function getDirectory(string $path): array;
50+
51+
/**
52+
* Creates a new directory for the server in the given $path.
53+
*
54+
* @param string $name
55+
* @param string $path
56+
* @return \Psr\Http\Message\ResponseInterface
57+
*/
58+
public function createDirectory(string $name, string $path): ResponseInterface;
5059
}

app/Http/Controllers/Api/Client/Servers/FileController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
use Carbon\Carbon;
66
use Ramsey\Uuid\Uuid;
7+
use Illuminate\Http\Response;
78
use Pterodactyl\Models\Server;
89
use Illuminate\Http\JsonResponse;
910
use Illuminate\Contracts\Cache\Repository as CacheRepository;
1011
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
1112
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
1213
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
14+
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest;
1315
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest;
1416

1517
class FileController extends ClientApiController
@@ -53,6 +55,21 @@ public function listDirectory(ListFilesRequest $request): JsonResponse
5355
]);
5456
}
5557

58+
/**
59+
* Creates a new folder on the server.
60+
*
61+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest $request
62+
* @return \Illuminate\Http\Response
63+
*/
64+
public function createFolder(CreateFolderRequest $request): Response
65+
{
66+
$this->fileRepository
67+
->setServer($request->getModel(Server::class))
68+
->createDirectory($request->input('name'), $request->input('directory', '/'));
69+
70+
return Response::create('s');
71+
}
72+
5673
/**
5774
* Configure a reference to a file to download in the cache so that when the
5875
* user hits the Daemon and it verifies with the Panel they'll actually be able

app/Http/Middleware/Api/Client/Server/AuthenticateServerAccess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function handle(Request $request, Closure $next)
3939
$server = $request->route()->parameter('server');
4040

4141
if (! $server instanceof Server) {
42-
throw new NotFoundHttpException;
42+
throw new NotFoundHttpException(trans('exceptions.api.resource_not_found'));
4343
}
4444

4545
if ($server->suspended) {

app/Http/Middleware/Api/Client/SubstituteClientApiBindings.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ public function handle($request, Closure $next)
2626
// column rather than the default 'id'.
2727
$this->router->bind('server', function ($value) use ($request) {
2828
try {
29+
$column = 'uuidShort';
30+
if (preg_match('/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', $value)) {
31+
$column = 'uuid';
32+
}
33+
2934
return Container::getInstance()->make(ServerRepositoryInterface::class)->findFirstWhere([
30-
['uuidShort', '=', $value],
35+
[$column, '=', $value],
3136
]);
3237
} catch (RecordNotFoundException $ex) {
3338
$request->attributes->set('is_missing_model', true);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Files;
4+
5+
use Pterodactyl\Models\Server;
6+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7+
8+
class CreateFolderRequest extends ClientApiRequest
9+
{
10+
/**
11+
* Checks that the authenticated user is allowed to create files on the server.
12+
*
13+
* @return bool
14+
*/
15+
public function authorize(): bool
16+
{
17+
return $this->user()->can('create-files', $this->getModel(Server::class));
18+
}
19+
20+
/**
21+
* @return array
22+
*/
23+
public function rules(): array
24+
{
25+
return [
26+
'root' => 'sometimes|nullable|string',
27+
'name' => 'required|string',
28+
];
29+
}
30+
}

app/Http/Requests/Api/Client/Servers/Files/ListFilesRequest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ public function authorize(): bool
1717
{
1818
return $this->user()->can('list-files', $this->getModel(Server::class));
1919
}
20+
21+
/**
22+
* @return array
23+
*/
24+
public function rules(): array
25+
{
26+
return [
27+
'directory' => 'sometimes|nullable|string',
28+
];
29+
}
2030
}

app/Repositories/Daemon/FileRepository.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Repositories\Daemon;
44

55
use stdClass;
6+
use RuntimeException;
67
use Psr\Http\Message\ResponseInterface;
78
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
89

@@ -86,4 +87,18 @@ public function getDirectory(string $path): array
8687

8788
return json_decode($response->getBody());
8889
}
90+
91+
/**
92+
* Creates a new directory for the server in the given $path.
93+
*
94+
* @param string $name
95+
* @param string $path
96+
* @return \Psr\Http\Message\ResponseInterface
97+
*
98+
* @throws \RuntimeException
99+
*/
100+
public function createDirectory(string $name, string $path): ResponseInterface
101+
{
102+
throw new RuntimeException('Not implemented.');
103+
}
89104
}

app/Repositories/Wings/FileRepository.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,22 @@ public function getDirectory(string $path): array
6666

6767
return json_decode($response->getBody(), true);
6868
}
69+
70+
/**
71+
* Creates a new directory for the server in the given $path.
72+
*
73+
* @param string $name
74+
* @param string $path
75+
* @return \Psr\Http\Message\ResponseInterface
76+
*/
77+
public function createDirectory(string $name, string $path): ResponseInterface
78+
{
79+
return $this->getHttpClient()->post(
80+
sprintf('/api/servers/%s/files/create-directory', $this->getServer()->uuid),
81+
[
82+
'name' => $name,
83+
'directory' => $path,
84+
]
85+
);
86+
}
6987
}

resources/assets/scripts/api/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios, {AxiosInstance, AxiosRequestConfig} from 'axios';
1+
import axios, {AxiosInstance} from 'axios';
22
import {ServerApplicationCredentials} from "@/store/types";
33

44
// This token is set in the bootstrap.js file at the beginning of the request

resources/assets/scripts/api/server/files/createFolder.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import {ServerApplicationCredentials} from "@/store/types";
2-
import {withCredentials} from "@/api/http";
1+
import http from "@/api/http";
32

43
/**
54
* Connects to the remote daemon and creates a new folder on the server.
65
*/
7-
export function createFolder(server: string, credentials: ServerApplicationCredentials, path: string): Promise<void> {
6+
export function createFolder(server: string, directory: string, name: string): Promise<void> {
87
return new Promise((resolve, reject) => {
9-
withCredentials(server, credentials).post('/v1/server/file/folder', { path })
8+
http.post(`/api/client/servers/${server}/files/create-folder`, {
9+
directory, name,
10+
})
1011
.then(() => resolve())
1112
.catch(reject);
1213
});

0 commit comments

Comments
 (0)