Skip to content

Commit 8110268

Browse files
committed
Update support for moving/renaming files and folders
1 parent eed4be4 commit 8110268

File tree

9 files changed

+97
-41
lines changed

9 files changed

+97
-41
lines changed

app/Contracts/Repository/Daemon/FileRepositoryInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,13 @@ public function getDirectory(string $path): array;
5656
* @return \Psr\Http\Message\ResponseInterface
5757
*/
5858
public function createDirectory(string $name, string $path): ResponseInterface;
59+
60+
/**
61+
* Renames or moves a file on the remote machine.
62+
*
63+
* @param string $from
64+
* @param string $to
65+
* @return \Psr\Http\Message\ResponseInterface
66+
*/
67+
public function renameFile(string $from, string $to): ResponseInterface;
5968
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
1212
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
1313
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
14+
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest;
1415
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest;
1516
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest;
1617

@@ -67,7 +68,22 @@ public function createFolder(CreateFolderRequest $request): Response
6768
->setServer($request->getModel(Server::class))
6869
->createDirectory($request->input('name'), $request->input('directory', '/'));
6970

70-
return Response::create('s');
71+
return Response::create('', Response::HTTP_NO_CONTENT);
72+
}
73+
74+
/**
75+
* Renames a file on the remote machine.
76+
*
77+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest $request
78+
* @return \Illuminate\Http\Response
79+
*/
80+
public function renameFile(RenameFileRequest $request): Response
81+
{
82+
$this->fileRepository
83+
->setServer($request->getModel(Server::class))
84+
->renameFile($request->input('rename_from'), $request->input('rename_to'));
85+
86+
return Response::create('', Response::HTTP_NO_CONTENT);
7187
}
7288

7389
/**
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 RenameFileRequest extends ClientApiRequest implements ClientPermissionsRequest
9+
{
10+
/**
11+
* The permission the user is required to have in order to perform this
12+
* request action.
13+
*
14+
* @return string
15+
*/
16+
public function permission(): string
17+
{
18+
return 'move-files';
19+
}
20+
21+
/**
22+
* @return array
23+
*/
24+
public function rules(): array
25+
{
26+
return [
27+
'rename_from' => 'string|required',
28+
'rename_to' => 'string|required',
29+
];
30+
}
31+
}

app/Repositories/Wings/FileRepository.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function putContent(string $path, string $content): ResponseInterface
5959
public function getDirectory(string $path): array
6060
{
6161
$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.
62+
// Reason for the path check is because it is unnecessary on the Daemon but we need
63+
// to respect the interface.
6464
sprintf('/api/servers/%s/files/list/%s', $this->getServer()->uuid, $path === '/' ? '' : $path)
6565
);
6666

@@ -86,4 +86,24 @@ public function createDirectory(string $name, string $path): ResponseInterface
8686
]
8787
);
8888
}
89+
90+
/**
91+
* Renames or moves a file on the remote machine.
92+
*
93+
* @param string $from
94+
* @param string $to
95+
* @return \Psr\Http\Message\ResponseInterface
96+
*/
97+
public function renameFile(string $from, string $to): ResponseInterface
98+
{
99+
return $this->getHttpClient()->put(
100+
sprintf('/api/servers/%s/files/rename', $this->getServer()->uuid),
101+
[
102+
'json' => [
103+
'rename_from' => $from,
104+
'rename_to' => $to,
105+
],
106+
]
107+
);
108+
}
89109
}

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

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import http from "@/api/http";
2+
3+
export function renameFile(server: string, renameFrom: string, renameTo: string): Promise<void> {
4+
return new Promise((resolve, reject) => {
5+
http.put(`/api/client/servers/${server}/files/rename`, {
6+
rename_from: renameFrom,
7+
rename_to: renameTo,
8+
})
9+
.then(() => resolve())
10+
.catch(reject);
11+
});
12+
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import Modal from "@/components/core/Modal.vue";
4343
import MessageBox from "@/components/MessageBox.vue";
4444
import {DirectoryContentObject} from "@/api/server/types";
45-
import {moveElement} from '@/api/server/files/copyElement';
45+
import {renameFile} from '@/api/server/files/renameFile';
4646
import {mapState} from "vuex";
4747
import {ApplicationState} from "@/store/types";
4848
import {join} from 'path';
@@ -106,12 +106,7 @@
106106
this.isLoading = true;
107107
108108
// @ts-ignore
109-
moveElement(this.server.uuid, this.credentials, {
110-
// @ts-ignore
111-
currentPath: join(this.fm.currentDirectory, this.file.name),
112-
// @ts-ignore
113-
newPath: join(this.fm.currentDirectory, this.moveTo),
114-
})
109+
renameFile(this.server.uuid, join(this.fm.currentDirectory, this.file.name), join(this.fm.currentDirectory, this.moveTo))
115110
.then(() => this.$emit('moved'))
116111
.catch((error: AxiosError) => {
117112
this.error = `There was an error moving the requested ${(this.file.directory) ? 'folder' : 'file'}. Response was: ${error.message}`;

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
import MessageBox from '@/components/MessageBox.vue';
4848
import {DirectoryContentObject} from "@/api/server/types";
4949
import {mapState} from "vuex";
50-
import {renameElement} from "@/api/server/files/renameElement";
50+
import {renameFile} from "@/api/server/files/renameFile";
5151
import {AxiosError} from 'axios';
5252
import {ApplicationState} from "@/store/types";
53+
import {join} from "path";
5354
5455
type DataStructure = {
5556
error: null | string,
@@ -109,12 +110,7 @@
109110
this.error = null;
110111
111112
// @ts-ignore
112-
renameElement(this.server.uuid, this.credentials, {
113-
// @ts-ignore
114-
path: this.fm.currentDirectory,
115-
toName: this.newName,
116-
fromName: this.object.name
117-
})
113+
renameFile(this.server.uuid, join(this.fm.currentDirectory, this.object.name), join(this.fm.currentDirectory, this.newName))
118114
.then(() => {
119115
this.$emit('renamed', this.newName);
120116
this.closeModal();

routes/api-client.php

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

4444
Route::group(['prefix' => '/files'], function () {
4545
Route::get('/list', 'Servers\FileController@listDirectory')->name('api.client.servers.files.list');
46-
46+
Route::put('/rename', 'Servers\FileController@renameFile')->name('api.client.servers.files.rename');
4747
Route::post('/create-folder', 'Servers\FileController@createFolder')->name('api.client.servers.files.create-folder');
4848

4949
Route::post('/download/{file}', 'Servers\FileController@download')

0 commit comments

Comments
 (0)