Skip to content

Commit d79fe69

Browse files
committed
Add support for file copy and deletion
1 parent 8110268 commit d79fe69

File tree

12 files changed

+173
-53
lines changed

12 files changed

+173
-53
lines changed

app/Contracts/Repository/Daemon/FileRepositoryInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,20 @@ public function createDirectory(string $name, string $path): ResponseInterface;
6565
* @return \Psr\Http\Message\ResponseInterface
6666
*/
6767
public function renameFile(string $from, string $to): ResponseInterface;
68+
69+
/**
70+
* Copy a given file and give it a unique name.
71+
*
72+
* @param string $location
73+
* @return \Psr\Http\Message\ResponseInterface
74+
*/
75+
public function copyFile(string $location): ResponseInterface;
76+
77+
/**
78+
* Delete a file or folder for the server.
79+
*
80+
* @param string $location
81+
* @return \Psr\Http\Message\ResponseInterface
82+
*/
83+
public function deleteFile(string $location): ResponseInterface;
6884
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use Illuminate\Contracts\Cache\Repository as CacheRepository;
1111
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
1212
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
13+
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
1314
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
15+
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest;
1416
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest;
1517
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest;
1618
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest;
@@ -86,6 +88,36 @@ public function renameFile(RenameFileRequest $request): Response
8688
return Response::create('', Response::HTTP_NO_CONTENT);
8789
}
8890

91+
/**
92+
* Copies a file on the server.
93+
*
94+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest $request
95+
* @return \Illuminate\Http\Response
96+
*/
97+
public function copyFile(CopyFileRequest $request): Response
98+
{
99+
$this->fileRepository
100+
->setServer($request->getModel(Server::class))
101+
->copyFile($request->input('location'));
102+
103+
return Response::create('', Response::HTTP_NO_CONTENT);
104+
}
105+
106+
/**
107+
* Deletes a file or folder from the server.
108+
*
109+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest $request
110+
* @return \Illuminate\Http\Response
111+
*/
112+
public function delete(DeleteFileRequest $request): Response
113+
{
114+
$this->fileRepository
115+
->setServer($request->getModel(Server::class))
116+
->deleteFile($request->input('location'));
117+
118+
return Response::create('', Response::HTTP_NO_CONTENT);
119+
}
120+
89121
/**
90122
* Configure a reference to a file to download in the cache so that when the
91123
* user hits the Daemon and it verifies with the Panel they'll actually be able
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 CopyFileRequest extends ClientApiRequest implements ClientPermissionsRequest
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function permission(): string
14+
{
15+
return 'copy-files';
16+
}
17+
18+
/**
19+
* @return array
20+
*/
21+
public function rules(): array
22+
{
23+
return [
24+
'location' => 'required|string',
25+
];
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 DeleteFileRequest extends ClientApiRequest implements ClientPermissionsRequest
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function permission(): string
14+
{
15+
return 'delete-files';
16+
}
17+
18+
/**
19+
* @return array
20+
*/
21+
public function rules(): array
22+
{
23+
return [
24+
'location' => 'required|string',
25+
];
26+
}
27+
}

app/Repositories/Wings/FileRepository.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,40 @@ public function renameFile(string $from, string $to): ResponseInterface
106106
]
107107
);
108108
}
109+
110+
/**
111+
* Copy a given file and give it a unique name.
112+
*
113+
* @param string $location
114+
* @return \Psr\Http\Message\ResponseInterface
115+
*/
116+
public function copyFile(string $location): ResponseInterface
117+
{
118+
return $this->getHttpClient()->post(
119+
sprintf('/api/servers/%s/files/copy', $this->getServer()->uuid),
120+
[
121+
'json' => [
122+
'location' => $location,
123+
],
124+
]
125+
);
126+
}
127+
128+
/**
129+
* Delete a file or folder for the server.
130+
*
131+
* @param string $location
132+
* @return \Psr\Http\Message\ResponseInterface
133+
*/
134+
public function deleteFile(string $location): ResponseInterface
135+
{
136+
return $this->getHttpClient()->post(
137+
sprintf('/api/servers/%s/files/delete', $this->getServer()->uuid),
138+
[
139+
'json' => [
140+
'location' => $location,
141+
],
142+
]
143+
);
144+
}
109145
}

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

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import http from "@/api/http";
2+
3+
/**
4+
* Creates a copy of the given file or directory on the Daemon. Expects a fully resolved path
5+
* to be passed through for both data arguments.
6+
*/
7+
export function copyFile(server: string, location: string): Promise<void> {
8+
return new Promise((resolve, reject) => {
9+
http.post(`/api/client/servers/${server}/files/copy`, {location})
10+
.then(() => resolve())
11+
.catch(reject);
12+
});
13+
}

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

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import http from "@/api/http";
2+
3+
/**
4+
* Deletes files and/or folders from the server. You should pass through an array of
5+
* file or folder paths to be deleted.
6+
*/
7+
export function deleteFile(server: string, location: string): Promise<void> {
8+
return new Promise((resolve, reject) => {
9+
http.post(`/api/client/servers/${server}/files/delete`, {location})
10+
.then(() => resolve())
11+
.catch(reject);
12+
})
13+
}

resources/assets/scripts/components/server/components/filemanager/modals/CopyFileModal.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import {DirectoryContentObject} from '@/api/server/types';
1111
import {mapState} from "vuex";
1212
import {ServerState} from '@/store/types';
13-
import { join } from 'path';
14-
import {copyElement} from '@/api/server/files/copyElement';
13+
import {join} from 'path';
14+
import {copyFile} from '@/api/server/files/copyFile';
1515
import {AxiosError} from "axios";
1616
1717
export default Vue.extend({
18-
components: { SpinnerModal },
18+
components: {SpinnerModal},
1919
2020
computed: mapState('server', {
2121
server: (state: ServerState) => state.server,
@@ -24,7 +24,7 @@
2424
}),
2525
2626
props: {
27-
file: { type: Object as () => DirectoryContentObject, required: true },
27+
file: {type: Object as () => DirectoryContentObject, required: true},
2828
},
2929
3030
/**
@@ -46,7 +46,7 @@
4646
}
4747
}
4848
49-
copyElement(this.server.uuid, this.credentials, {currentPath: join(this.fm.currentDirectory, this.file.name), newPath})
49+
copyFile(this.server.uuid, join(this.fm.currentDirectory, this.file.name))
5050
.then(() => this.$emit('close'))
5151
.catch((error: AxiosError) => {
5252
alert(`There was an error creating a copy of this item: ${error.message}`);

0 commit comments

Comments
 (0)