ui(files): add pull file modal

This commit is contained in:
Matthew Penner 2021-07-25 13:24:52 -06:00
parent 01242a805d
commit 3c2a6e1136
14 changed files with 211 additions and 42 deletions

View File

@ -247,15 +247,19 @@ class FileController extends ClientApiController
/**
* Updates file permissions for file(s) in the given root directory.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Throwable
*/
public function chmod(ChmodFilesRequest $request, Server $server): Response
{
$this->fileRepository->setServer($server)
->chmodFiles(
$request->input('root'),
$request->input('files')
);
$server->audit(AuditLog::SERVER__FILESYSTEM_CHMOD, function (AuditLog $audit, Server $server) use ($request) {
$audit->metadata = ['directory' => $request->input('root'), 'files' => $request->input('files')];
$this->fileRepository->setServer($server)
->chmodFiles(
$request->input('root'),
$request->input('files'),
);
});
return $this->returnNoContent();
}
@ -268,9 +272,13 @@ class FileController extends ClientApiController
public function pull(PullFileRequest $request, Server $server): Response
{
$server->audit(AuditLog::SERVER__FILESYSTEM_PULL, function (AuditLog $audit, Server $server) use ($request) {
$audit->metadata = ['directory' => $request->input('directory'), 'url' => $request->input('url')];
$audit->metadata = ['directory' => $request->input('root'), 'url' => $request->input('url')];
$this->fileRepository->setServer($server)->pull($request->input('url'), $request->input('directory'));
$this->fileRepository->setServer($server)
->pull(
$request->input('root'),
$request->input('url'),
);
});
return $this->returnNoContent();

View File

@ -19,8 +19,8 @@ class PullFileRequest extends ClientApiRequest implements ClientPermissionsReque
public function rules(): array
{
return [
'root' => 'sometimes|nullable|string',
'url' => 'required|string|url',
'directory' => 'sometimes|nullable|string',
];
}
}

View File

@ -34,6 +34,9 @@ class AssetComposer
'siteKey' => config('recaptcha.website_key') ?? '',
],
'analytics' => config('app.analytics') ?? '',
'features' => [
'pullFiles' => config('features.pull_files'),
],
]);
}
}

View File

@ -30,6 +30,7 @@ class AuditLog extends Model
public const SERVER__FILESYSTEM_RENAME = 'server:filesystem.rename';
public const SERVER__FILESYSTEM_COMPRESS = 'server:filesystem.compress';
public const SERVER__FILESYSTEM_DECOMPRESS = 'server:filesystem.decompress';
public const SERVER__FILESYSTEM_CHMOD = 'server:filesystem.chmod';
public const SERVER__FILESYSTEM_PULL = 'server:filesystem.pull';
public const SERVER__BACKUP_STARTED = 'server:backup.started';
public const SERVER__BACKUP_FAILED = 'server:backup.failed';

View File

@ -3,6 +3,8 @@
namespace Pterodactyl\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
@ -55,5 +57,9 @@ class RouteServiceProvider extends ServiceProvider
Route::middleware(['daemon'])->prefix('/api/remote')
->namespace($this->namespace . '\Api\Remote')
->group(base_path('routes/api-remote.php'));
RateLimiter::for('pull', function () {
return Limit::perMinute(10);
});
}
}

View File

@ -29,7 +29,7 @@ class DaemonFileRepository extends DaemonRepository
sprintf('/api/servers/%s/files/contents', $this->server->uuid),
[
'query' => ['file' => $path],
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -60,7 +60,7 @@ class DaemonFileRepository extends DaemonRepository
[
'query' => ['file' => $path],
'body' => $content,
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -81,7 +81,7 @@ class DaemonFileRepository extends DaemonRepository
sprintf('/api/servers/%s/files/list-directory', $this->server->uuid),
[
'query' => ['directory' => $path],
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -107,7 +107,7 @@ class DaemonFileRepository extends DaemonRepository
'name' => $name,
'path' => $path,
],
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -131,7 +131,7 @@ class DaemonFileRepository extends DaemonRepository
'root' => $root ?? '/',
'files' => $files,
],
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -154,7 +154,7 @@ class DaemonFileRepository extends DaemonRepository
'json' => [
'location' => $location,
],
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -178,7 +178,7 @@ class DaemonFileRepository extends DaemonRepository
'root' => $root ?? '/',
'files' => $files,
],
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -205,7 +205,7 @@ class DaemonFileRepository extends DaemonRepository
// Wait for up to 15 minutes for the archive to be completed when calling this endpoint
// since it will likely take quite awhile for large directories.
'timeout' => 60 * 15,
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -231,7 +231,7 @@ class DaemonFileRepository extends DaemonRepository
'root' => $root ?? '/',
'file' => $file,
],
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -255,7 +255,7 @@ class DaemonFileRepository extends DaemonRepository
'root' => $root ?? '/',
'files' => $files,
],
]
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
@ -267,7 +267,7 @@ class DaemonFileRepository extends DaemonRepository
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function pull(string $url, ?string $directory): ResponseInterface
public function pull(?string $root, string $url): ResponseInterface
{
Assert::isInstanceOf($this->server, Server::class);
@ -275,8 +275,11 @@ class DaemonFileRepository extends DaemonRepository
return $this->getHttpClient()->post(
sprintf('/api/servers/%s/files/pull', $this->server->uuid),
[
'json' => ['url' => $url, 'directory' => $directory ?? '/'],
]
'json' => [
'root' => $root ?? '/',
'url' => $url,
],
],
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);

5
config/features.php Normal file
View File

@ -0,0 +1,5 @@
<?php
return [
'pull_files' => env('FEATURES_PULL_FILES', true),
];

View File

@ -0,0 +1,9 @@
import http from '@/api/http';
export default (uuid: string, directory: string, url: string): Promise<void> => {
return new Promise((resolve, reject) => {
http.post(`/api/client/servers/${uuid}/files/pull`, { root: directory, url })
.then(() => resolve())
.catch(reject);
});
};

View File

@ -15,8 +15,8 @@ interface FormikValues {
}
interface File {
file: string,
mode: string,
file: string;
mode: string;
}
type OwnProps = RequiredModalProps & { files: File[] };

View File

@ -16,10 +16,13 @@ import useFileManagerSwr from '@/plugins/useFileManagerSwr';
import MassActionsBar from '@/components/server/files/MassActionsBar';
import UploadButton from '@/components/server/files/UploadButton';
import ServerContentBlock from '@/components/elements/ServerContentBlock';
import { useStoreActions } from '@/state/hooks';
import ErrorBoundary from '@/components/elements/ErrorBoundary';
import PullFileModal from '@/components/server/files/PullFileModal';
import { FileActionCheckbox } from '@/components/server/files/SelectFileCheckbox';
import { hashToPath } from '@/helpers';
import { useStoreActions } from '@/state/hooks';
import { useStoreState } from 'easy-peasy';
import { ApplicationStore } from '@/state';
const sortFiles = (files: FileObject[]): FileObject[] => {
return files.sort((a, b) => a.name.localeCompare(b.name))
@ -27,16 +30,20 @@ const sortFiles = (files: FileObject[]): FileObject[] => {
};
export default () => {
const pullFiles = useStoreState((state: ApplicationStore) => state.settings.data!.features.pullFiles);
const id = ServerContext.useStoreState(state => state.server.data!.id);
const { hash } = useLocation();
const { data: files, error, mutate } = useFileManagerSwr();
const directory = ServerContext.useStoreState(state => state.files.directory);
const clearFlashes = useStoreActions(actions => actions.flashes.clearFlashes);
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
const selectedFilesLength = ServerContext.useStoreState(state => state.files.selectedFiles.length);
const clearFlashes = useStoreActions(actions => actions.flashes.clearFlashes);
const { hash } = useLocation();
const { data: files, error, mutate } = useFileManagerSwr();
useEffect(() => {
clearFlashes('files');
setSelectedFiles([]);
@ -75,6 +82,7 @@ export default () => {
<Can action={'file.create'}>
<ErrorBoundary>
<div css={tw`flex flex-shrink-0 flex-wrap-reverse md:flex-nowrap justify-end mb-4 md:mb-0 ml-0 md:ml-auto`}>
{pullFiles && <PullFileModal css={tw`w-full flex-none mt-4 sm:mt-0 sm:w-auto sm:mr-4`}/>}
<NewDirectoryButton css={tw`w-full flex-none mt-4 sm:mt-0 sm:w-auto sm:mr-4`}/>
<UploadButton css={tw`flex-1 mr-4 sm:flex-none sm:mt-0`}/>
<NavLink

View File

@ -68,7 +68,7 @@ export default ({ className }: WithClassname) => {
validationSchema={object().shape({
directoryName: string()
.required('A valid directory name must be provided.')
.test('unique', 'Directory with that name already exists.', v => {
.test('unique', 'File or directory with that name already exists.', v => {
return v !== undefined &&
data !== undefined &&
data.filter(f => f.name.toLowerCase() === v.toLowerCase()).length < 1;

View File

@ -0,0 +1,121 @@
import { Form, Formik, FormikHelpers } from 'formik';
import React, { useEffect, useState } from 'react';
import tw from 'twin.macro';
import { object, string } from 'yup';
import pullFile from '@/api/server/files/pullFile';
import { WithClassname } from '@/components/types';
import Button from '@/components/elements/Button';
import Field from '@/components/elements/Field';
import Modal from '@/components/elements/Modal';
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
import useFlash from '@/plugins/useFlash';
import { ServerContext } from '@/state/server';
import { FileObject } from '@/api/server/files/loadDirectory';
import FlashMessageRender from '@/components/FlashMessageRender';
import { join } from 'path';
interface Values {
url: string;
}
const generateFileData = (name: string): FileObject => ({
key: `file_${name.split('/', 1)[0] ?? name}`,
name: name,
mode: 'rw-rw-rw-',
modeBits: '0644',
size: 0,
isFile: true,
isSymlink: false,
mimetype: '',
createdAt: new Date(),
modifiedAt: new Date(),
isArchiveType: () => false,
isEditable: () => false,
});
export default ({ className }: WithClassname) => {
const [ visible, setVisible ] = useState(false);
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const directory = ServerContext.useStoreState(state => state.files.directory);
const { clearFlashes, clearAndAddHttpError } = useFlash();
const { data, mutate } = useFileManagerSwr();
useEffect(() => {
if (!visible) return;
return () => {
clearFlashes('files:pull-modal');
};
}, [ visible ]);
const submit = ({ url }: Values, { setSubmitting }: FormikHelpers<Values>) => {
pullFile(uuid, directory, url)
.then(() => mutate(data => [ ...data!, generateFileData(new URL(url).pathname.split('/').pop() || '') ], false))
.then(() => setVisible(false))
.catch(error => {
console.error(error);
setSubmitting(false);
clearAndAddHttpError({ key: 'files:pull-modal', error });
});
};
return (
<>
<Formik
onSubmit={submit}
initialValues={{ url: '' }}
validationSchema={object().shape({
url: string()
.required()
.url()
.test('unique', 'File or directory with that name already exists.', v => {
return v !== undefined &&
data !== undefined &&
data.filter(f => f.name.toLowerCase() === v.toLowerCase()).length < 1;
}),
})}
>
{({ resetForm, isSubmitting, values }) => (
<Modal
visible={visible}
dismissable={!isSubmitting}
showSpinnerOverlay={isSubmitting}
onDismissed={() => {
setVisible(false);
resetForm();
}}
>
<FlashMessageRender key={'files:pull-modal'}/>
<Form css={tw`m-0`}>
<Field
type={'text'}
id={'url'}
name={'url'}
label={'URL'}
autoFocus
/>
<p css={tw`text-xs mt-2 text-neutral-400 break-all`}>
<span css={tw`text-neutral-200`}>This file will be downloaded to</span>
&nbsp;/home/container/
<span css={tw`text-cyan-200`}>
{values.url !== '' ? join(directory, new URL(values.url).pathname.split('/').pop() || '').substr(1) : ''}
</span>
</p>
<div css={tw`flex justify-end`}>
<Button type={'submit'} css={tw`mt-8`}>
Pull File
</Button>
</div>
</Form>
</Modal>
)}
</Formik>
<Button onClick={() => setVisible(true)} className={className} isSecondary>
Pull Remote File
</Button>
</>
);
};

View File

@ -8,6 +8,9 @@ export interface SiteSettings {
siteKey: string;
};
analytics: string;
features: {
pullFiles: boolean;
};
}
export interface SettingsStore {

View File

@ -5,6 +5,8 @@ use Pterodactyl\Http\Middleware\RequireTwoFactorAuthentication;
use Pterodactyl\Http\Middleware\Api\Client\Server\ResourceBelongsToServer;
use Pterodactyl\Http\Middleware\Api\Client\Server\AuthenticateServerAccess;
use Pterodactyl\Http\Controllers\Api\Client;
/*
|--------------------------------------------------------------------------
| Client Control API
@ -63,19 +65,19 @@ Route::group(['prefix' => '/servers/{server}', 'middleware' => [AuthenticateServ
});
Route::group(['prefix' => '/files'], function () {
Route::get('/list', 'Servers\FileController@directory');
Route::get('/contents', 'Servers\FileController@contents');
Route::get('/download', 'Servers\FileController@download');
Route::put('/rename', 'Servers\FileController@rename');
Route::post('/copy', 'Servers\FileController@copy');
Route::post('/write', 'Servers\FileController@write');
Route::post('/compress', 'Servers\FileController@compress');
Route::post('/decompress', 'Servers\FileController@decompress');
Route::post('/delete', 'Servers\FileController@delete');
Route::post('/create-folder', 'Servers\FileController@create');
Route::post('/chmod', 'Servers\FileController@chmod');
Route::post('/pull', 'Servers\FileController@pull')->middleware(['throttle:10,5']);
Route::get('/upload', 'Servers\FileUploadController');
Route::get('/list', [Client\Servers\FileController::class, 'directory']);
Route::get('/contents', [Client\Servers\FileController::class, 'contents']);
Route::get('/download', [Client\Servers\FileController::class, 'download']);
Route::put('/rename', [Client\Servers\FileController::class, 'rename']);
Route::post('/copy', [Client\Servers\FileController::class, 'copy']);
Route::post('/write', [Client\Servers\FileController::class, 'write']);
Route::post('/compress', [Client\Servers\FileController::class, 'compress']);
Route::post('/decompress', [Client\Servers\FileController::class, 'decompress']);
Route::post('/delete', [Client\Servers\FileController::class, 'delete']);
Route::post('/create-folder', [Client\Servers\FileController::class, 'create']);
Route::post('/chmod', [Client\Servers\FileController::class, 'chmod']);
Route::post('/pull', [Client\Servers\FileController::class, 'pull'])->middleware(['throttle:pull']);
Route::get('/upload', [Client\Servers\FileUploadController::class]);
});
Route::group(['prefix' => '/schedules'], function () {