Skip to content

Commit a8462bf

Browse files
committed
Add initial support for opening a file in the file manager, still needs more work
1 parent 6606eb1 commit a8462bf

File tree

10 files changed

+159
-24
lines changed

10 files changed

+159
-24
lines changed

app/Contracts/Repository/Daemon/FileRepositoryInterface.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ public function getFileStat(string $path): stdClass;
2020
/**
2121
* Return the contents of a given file if it can be edited in the Panel.
2222
*
23-
* @param string $path
23+
* @param string $path
24+
* @param int|null $notLargerThan
2425
* @return string
25-
*
26-
* @throws \GuzzleHttp\Exception\TransferException
2726
*/
28-
public function getContent(string $path): string;
27+
public function getContent(string $path, int $notLargerThan = null): string;
2928

3029
/**
3130
* Save new contents to a given file.

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Pterodactyl\Models\Server;
99
use Illuminate\Http\JsonResponse;
1010
use Illuminate\Contracts\Cache\Repository as CacheRepository;
11+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
1112
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
1213
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
1314
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
@@ -16,6 +17,7 @@
1617
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest;
1718
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest;
1819
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest;
20+
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest;
1921

2022
class FileController extends ClientApiController
2123
{
@@ -24,6 +26,11 @@ class FileController extends ClientApiController
2426
*/
2527
private $cache;
2628

29+
/**
30+
* @var \Illuminate\Contracts\Config\Repository
31+
*/
32+
private $config;
33+
2734
/**
2835
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface
2936
*/
@@ -32,14 +39,16 @@ class FileController extends ClientApiController
3239
/**
3340
* FileController constructor.
3441
*
42+
* @param \Illuminate\Contracts\Config\Repository $config
3543
* @param \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface $fileRepository
3644
* @param \Illuminate\Contracts\Cache\Repository $cache
3745
*/
38-
public function __construct(FileRepositoryInterface $fileRepository, CacheRepository $cache)
46+
public function __construct(ConfigRepository $config, FileRepositoryInterface $fileRepository, CacheRepository $cache)
3947
{
4048
parent::__construct();
4149

4250
$this->cache = $cache;
51+
$this->config = $config;
4352
$this->fileRepository = $fileRepository;
4453
}
4554

@@ -55,9 +64,25 @@ public function listDirectory(ListFilesRequest $request): JsonResponse
5564
'contents' => $this->fileRepository->setServer($request->getModel(Server::class))->getDirectory(
5665
$request->get('directory') ?? '/'
5766
),
67+
'editable' => $this->config->get('pterodactyl.files.editable', []),
5868
]);
5969
}
6070

71+
/**
72+
* Return the contents of a specified file for the user.
73+
*
74+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest $request
75+
* @return \Illuminate\Http\Response
76+
*/
77+
public function getFileContents(GetFileContentsRequest $request): Response
78+
{
79+
return Response::create(
80+
$this->fileRepository->setServer($request->getModel(Server::class))->getContent(
81+
$request->get('file'), $this->config->get('pterodactyl.files.max_edit_size')
82+
)
83+
);
84+
}
85+
6186
/**
6287
* Creates a new folder on the server.
6388
*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Files;
4+
5+
use Pterodactyl\Contracts\Http\ClientPermissionsRequest;
6+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7+
8+
class GetFileContentsRequest extends ClientApiRequest implements ClientPermissionsRequest
9+
{
10+
/**
11+
* Returns the permissions string indicating which permission should be used to
12+
* validate that the authenticated user has permission to perform this action aganist
13+
* the given resource (server).
14+
*
15+
* @return string
16+
*/
17+
public function permission(): string
18+
{
19+
return 'edit-files';
20+
}
21+
22+
/**
23+
* @return array
24+
*/
25+
public function rules(): array
26+
{
27+
return [
28+
'file' => 'required|string',
29+
];
30+
}
31+
}

app/Repositories/Wings/FileRepository.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Pterodactyl\Repositories\Wings;
44

55
use stdClass;
6+
use Exception;
67
use Psr\Http\Message\ResponseInterface;
8+
use Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException;
79
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
810

911
class FileRepository extends BaseWingsRepository implements FileRepositoryInterface
@@ -14,28 +16,47 @@ class FileRepository extends BaseWingsRepository implements FileRepositoryInterf
1416
* @param string $path
1517
* @return \stdClass
1618
*
19+
* @throws \Exception
1720
* @throws \GuzzleHttp\Exception\TransferException
1821
*/
1922
public function getFileStat(string $path): stdClass
2023
{
21-
// TODO: Implement getFileStat() method.
24+
throw new Exception('Function not implemented.');
2225
}
2326

2427
/**
25-
* Return the contents of a given file if it can be edited in the Panel.
28+
* Return the contents of a given file.
2629
*
27-
* @param string $path
30+
* @param string $path
31+
* @param int|null $notLargerThan the maximum content length in bytes
2832
* @return string
2933
*
3034
* @throws \GuzzleHttp\Exception\TransferException
35+
* @throws \Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException
3136
*/
32-
public function getContent(string $path): string
37+
public function getContent(string $path, int $notLargerThan = null): string
3338
{
34-
// TODO: Implement getContent() method.
39+
$response = $this->getHttpClient()->get(
40+
sprintf('/api/servers/%s/files/contents', $this->getServer()->uuid),
41+
[
42+
'query' => ['file' => $path],
43+
]
44+
);
45+
46+
$length = (int) $response->getHeader('Content-Length')[0] ?? 0;
47+
48+
if ($notLargerThan && $length > $notLargerThan) {
49+
throw new FileSizeTooLargeException(
50+
trans('server.files.exceptions.max_size')
51+
);
52+
}
53+
54+
return $response->getBody()->__toString();
3555
}
3656

3757
/**
38-
* Save new contents to a given file.
58+
* Save new contents to a given file. This works for both creating and updating
59+
* a file.
3960
*
4061
* @param string $path
4162
* @param string $content
@@ -45,7 +66,13 @@ public function getContent(string $path): string
4566
*/
4667
public function putContent(string $path, string $content): ResponseInterface
4768
{
48-
// TODO: Implement putContent() method.
69+
return $this->getHttpClient()->post(
70+
sprintf('/api/servers/%s/files/write', $this->getServer()->uuid),
71+
[
72+
'query' => ['file' => $path],
73+
'body' => $content,
74+
]
75+
);
4976
}
5077

5178
/**
@@ -59,9 +86,10 @@ public function putContent(string $path, string $content): ResponseInterface
5986
public function getDirectory(string $path): array
6087
{
6188
$response = $this->getHttpClient()->get(
62-
// Reason for the path check is because it is unnecessary on the Daemon but we need
63-
// to respect the interface.
64-
sprintf('/api/servers/%s/files/list/%s', $this->getServer()->uuid, $path === '/' ? '' : $path)
89+
sprintf('/api/servers/%s/files/list-directory', $this->getServer()->uuid),
90+
[
91+
'query' => ['directory' => $path],
92+
]
6593
);
6694

6795
return json_decode($response->getBody(), true);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import http from "@/api/http";
2+
3+
export default (server: string, file: string): Promise<string> => {
4+
return new Promise((resolve, reject) => {
5+
http.get(`/api/client/servers/${server}/files/contents`, {
6+
params: { file }
7+
})
8+
.then(response => resolve(response.data))
9+
.catch(reject);
10+
});
11+
}

resources/assets/scripts/components/server/components/filemanager/FileContextMenu.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
},
7575
7676
openNewFileModal: function () {
77-
window.events.$emit('server:files:open-new-file-modal');
77+
window.events.$emit('server:files:open-edit-file-modal');
7878
this.$emit('close');
7979
},
8080

resources/assets/scripts/components/server/components/filemanager/FileRow.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<template>
22
<div>
33
<div v-on:contextmenu="showContextMenu">
4-
<div class="row" :class="{ 'cursor-pointer': canEdit(file), 'active-selection': contextMenuVisible }" v-if="!file.directory">
4+
<div
5+
class="row"
6+
:class="{ 'cursor-pointer': canEdit(file), 'active-selection': contextMenuVisible }"
7+
v-if="!file.directory"
8+
v-on:click="openFileEditModal(file)"
9+
>
510
<div class="flex-none icon">
611
<Icon name="file-text" v-if="!file.symlink"/>
712
<Icon name="link2" v-else/>
@@ -142,6 +147,12 @@
142147
});
143148
},
144149
150+
openFileEditModal: function (file: DirectoryContentObject) {
151+
if (!file.directory && this.canEdit(file)) {
152+
window.events.$emit('server:files:open-edit-file-modal', file);
153+
}
154+
},
155+
145156
/**
146157
* Determine if a file can be edited on the Panel.
147158
*/

resources/assets/scripts/components/server/components/filemanager/modals/NewFileModal.vue renamed to resources/assets/scripts/components/server/components/filemanager/modals/EditFileModal.vue

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@
2929
import Vue from 'vue';
3030
import Icon from "@/components/core/Icon.vue";
3131
import MessageBox from "@/components/MessageBox.vue";
32-
import {ApplicationState} from '@/store/types';
32+
import {ApplicationState, FileManagerState} from '@/store/types';
3333
import {mapState} from "vuex";
3434
import * as Ace from 'brace';
35+
import { join } from 'path';
36+
import {DirectoryContentObject} from "@/api/server/types";
37+
import getFileContents from '@/api/server/files/getFileContents';
3538
3639
interface Data {
40+
file?: DirectoryContentObject,
41+
serverUuid?: string,
42+
fm?: FileManagerState,
3743
error: string | null,
3844
editor: Ace.Editor | null,
3945
isVisible: boolean,
@@ -77,17 +83,20 @@
7783
7884
computed: mapState({
7985
fm: (state: ApplicationState) => state.server.fm,
86+
serverUuid: (state: ApplicationState) => state.server.server.uuid,
8087
}),
8188
8289
mounted: function () {
83-
window.events.$on('server:files:open-new-file-modal', () => {
90+
window.events.$on('server:files:open-edit-file-modal', (file?: DirectoryContentObject) => {
91+
this.file = file;
8492
this.isVisible = true;
8593
8694
this.$nextTick(() => {
8795
this.editor = Ace.edit('editor');
8896
this.loadDependencies()
8997
.then(() => this.loadLanguages())
90-
.then(() => this.configureEditor());
98+
.then(() => this.configureEditor())
99+
.then(() => this.loadFileContent())
91100
});
92101
});
93102
},
@@ -97,6 +106,18 @@
97106
98107
},
99108
109+
loadFileContent: function () {
110+
if (!this.file || !this.editor || this.file.directory) {
111+
return;
112+
}
113+
114+
getFileContents(this.serverUuid!, join(this.fm!.currentDirectory, this.file.name))
115+
.then(contents => {
116+
this.editor!.$blockScrolling = Infinity;
117+
this.editor!.setValue(contents, 1);
118+
});
119+
},
120+
100121
updateFileLanguage: function (e: MouseEvent) {
101122
if (!this.editor) {
102123
return;
@@ -154,6 +175,14 @@
154175
<style>
155176
#editor {
156177
@apply .h-full .relative;
178+
179+
& > .ace_gutter > .ace_layer, & > .ace_scroller {
180+
@apply .py-1;
181+
}
182+
183+
& .ace_gutter-active-line {
184+
@apply .mt-1;
185+
}
157186
}
158187
159188
.ace_editor {

resources/assets/scripts/components/server/subpages/FileManager.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</div>
5353
</div>
5454
<CreateFolderModal v-on:created="directoryCreated"/>
55-
<NewFileModal/>
55+
<EditFileModal/>
5656
</div>
5757
</template>
5858

@@ -65,7 +65,7 @@
6565
import CreateFolderModal from '../components/filemanager/modals/CreateFolderModal.vue';
6666
import DeleteFileModal from '../components/filemanager/modals/DeleteFileModal.vue';
6767
import {DirectoryContentObject} from "@/api/server/types";
68-
import NewFileModal from "@/components/server/components/filemanager/modals/NewFileModal.vue";
68+
import EditFileModal from "@/components/server/components/filemanager/modals/EditFileModal.vue";
6969
7070
type DataStructure = {
7171
loading: boolean,
@@ -78,7 +78,7 @@
7878
7979
export default Vue.extend({
8080
name: 'FileManager',
81-
components: {CreateFolderModal, DeleteFileModal, FileRow, NewFileModal},
81+
components: {CreateFolderModal, DeleteFileModal, FileRow, EditFileModal},
8282
8383
computed: {
8484
/**
@@ -181,7 +181,7 @@
181181
},
182182
183183
openNewFileModal: function () {
184-
window.events.$emit('server:files:open-new-file-modal');
184+
window.events.$emit('server:files:open-edit-file-modal');
185185
},
186186
187187
fileRowDeleted: function (file: DirectoryContentObject, directory: boolean) {

routes/api-client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
Route::group(['prefix' => '/files'], function () {
4545
Route::get('/list', 'Servers\FileController@listDirectory')->name('api.client.servers.files.list');
46+
Route::get('/contents', 'Servers\FileController@getFileContents')->name('api.client.servers.files.contents');
4647
Route::put('/rename', 'Servers\FileController@renameFile')->name('api.client.servers.files.rename');
4748
Route::post('/copy', 'Servers\FileController@copyFile')->name('api.client.servers.files.copy');
4849
Route::post('/delete', 'Servers\FileController@delete')->name('api.client.servers.files.delete');

0 commit comments

Comments
 (0)