Initial attempt trying to get file downloading to work

This commit is contained in:
Dane Everitt 2019-03-16 17:10:04 -07:00
parent 4e669771ca
commit 8955b5a660
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
8 changed files with 153 additions and 3 deletions

View file

@ -0,0 +1,55 @@
<?php
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Carbon\Carbon;
use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Cache\Repository;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest;
class FileController extends ClientApiController
{
/**
* @var \Illuminate\Contracts\Cache\Factory
*/
private $cache;
/**
* FileController constructor.
*
* @param \Illuminate\Contracts\Cache\Repository $cache
*/
public function __construct(Repository $cache)
{
parent::__construct();
$this->cache = $cache;
}
/**
* Configure a reference to a file to download in the cache so that when the
* user hits the Daemon and it verifies with the Panel they'll actually be able
* to download that file.
*
* Returns the token that needs to be used when downloading the file.
*
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest $request
* @return \Illuminate\Http\JsonResponse
* @throws \Exception
*/
public function download(DownloadFileRequest $request): JsonResponse
{
/** @var \Pterodactyl\Models\Server $server */
$server = $request->getModel(Server::class);
$token = Uuid::uuid4()->toString();
$this->cache->put(
'Server:Downloads:' . $token, ['server' => $server->uuid, 'path' => $request->route()->parameter('file')], Carbon::now()->addMinutes(5)
);
return JsonResponse::create(['token' => $token]);
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Files;
use Pterodactyl\Models\Server;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
class DownloadFileRequest extends ClientApiRequest
{
/**
* Ensure that the user making this request has permission to download files
* from this server.
*
* @return bool
*/
public function authorize(): bool
{
return $this->user()->can('download-files', $this->getModel(Server::class));
}
}

View file

@ -0,0 +1,14 @@
import http from "@/api/http";
// @ts-ignore
import route from '../../../../../../vendor/tightenco/ziggy/src/js/route';
/**
* Gets a download token for a file on the server.
*/
export function getDownloadToken(server: string, file: string): Promise<string | null> {
return new Promise((resolve, reject) => {
http.post(route('api.client.servers.files.download', { server, file }))
.then(response => resolve(response.data ? response.data.token || null : null))
.catch(reject);
});
}

View file

@ -19,7 +19,7 @@
</div>
<div class="action">Copy</div>
</div>
<div class="context-row">
<div class="context-row" v-on:click="triggerAction('download')">
<div class="icon">
<Icon name="download" class="h-4"/>
</div>

View file

@ -35,9 +35,11 @@
v-on:action:rename="showModal('rename')"
v-on:action:copy="showModal('copy')"
v-on:action:move="showModal('move')"
v-on:action:download="showModal('download')"
ref="contextMenu"
/>
<CopyFileModal :file="file" v-if="modals.copy" v-on:close="$emit('list')"/>
<DownloadFileModal :file="file" v-if="modals.download" v-on:close="modals.download = false"/>
<DeleteFileModal :visible.sync="modals.delete" :object="file" v-on:deleted="$emit('deleted')" v-on:close="modal.delete = false"/>
<RenameModal :visible.sync="modals.rename" :object="file" v-on:renamed="$emit('list')" v-on:close="modal.rename = false"/>
<MoveFileModal :visible.sync="modals.move" :file="file" v-on:moved="$emit('list')" v-on:close="modal.move = false"/>
@ -54,6 +56,7 @@
import DeleteFileModal from "@/components/server/components/filemanager/modals/DeleteFileModal.vue";
import RenameModal from "@/components/server/components/filemanager/modals/RenameModal.vue";
import CopyFileModal from "@/components/server/components/filemanager/modals/CopyFileModal.vue";
import DownloadFileModal from "@/components/server/components/filemanager/modals/DownloadFileModal.vue";
import MoveFileModal from "@/components/server/components/filemanager/modals/MoveFileModal.vue";
type DataStructure = {
@ -64,7 +67,7 @@
export default Vue.extend({
name: 'FileRow',
components: {CopyFileModal, DeleteFileModal, MoveFileModal, Icon, FileContextMenu, RenameModal},
components: {CopyFileModal, DownloadFileModal, DeleteFileModal, MoveFileModal, Icon, FileContextMenu, RenameModal},
props: {
file: {
@ -87,6 +90,7 @@
delete: false,
copy: false,
move: false,
download: false,
},
};
},

View file

@ -0,0 +1,51 @@
<template>
<SpinnerModal :visible="true">
Downloading {{ file.name }}...
</SpinnerModal>
</template>
<script lang="ts">
import Vue from 'vue';
import SpinnerModal from "../../../../core/SpinnerModal.vue";
import {DirectoryContentObject} from '@/api/server/types';
import {mapState} from "vuex";
import {ServerState} from '@/store/types';
import { join } from 'path';
import {AxiosError} from "axios";
import {getDownloadToken} from '@/api/server/files/getDownloadToken';
export default Vue.extend({
components: { SpinnerModal },
computed: mapState('server', {
server: (state: ServerState) => state.server,
credentials: (state: ServerState) => state.credentials,
fm: (state: ServerState) => state.fm,
}),
props: {
file: { type: Object as () => DirectoryContentObject, required: true },
},
/**
* This modal works differently than the other modals that exist for the file manager.
* When it is mounted we will immediately show the spinner, and then begin the operation
* to get the download token and redirect the user to that new URL.
*/
mounted: function () {
const path = join(this.fm.currentDirectory, this.file.name);
getDownloadToken(this.server.uuid, path)
.then((token) => {
if (token) {
window.location.href = `${this.credentials.node}/v1/server/file/download/${token}`;
}
})
.catch((error: AxiosError) => {
alert(`There was an error trying to download this ${this.file.directory ? 'folder' : 'file'}: ${error.message}`);
console.error('Error at Server::Files::Download', {error});
})
.then(() => this.$emit('close'));
},
})
</script>

File diff suppressed because one or more lines are too long

View file

@ -40,4 +40,10 @@ Route::group(['prefix' => '/servers/{server}', 'middleware' => [AuthenticateServ
Route::post('/', 'Servers\DatabaseController@store');
Route::delete('/{database}', 'Servers\DatabaseController@delete')->name('api.client.servers.databases.delete');
});
Route::group(['prefix' => '/files'], function () {
Route::post('/download/{file}', 'Servers\FileController@download')
->where('file', '.*')
->name('api.client.servers.files.download');
});
});