Skip to content

Commit 8955b5a

Browse files
committed
Initial attempt trying to get file downloading to work
1 parent 4e66977 commit 8955b5a

File tree

8 files changed

+153
-3
lines changed

8 files changed

+153
-3
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4+
5+
use Carbon\Carbon;
6+
use Ramsey\Uuid\Uuid;
7+
use Pterodactyl\Models\Server;
8+
use Illuminate\Http\JsonResponse;
9+
use Illuminate\Contracts\Cache\Repository;
10+
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
11+
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest;
12+
13+
class FileController extends ClientApiController
14+
{
15+
/**
16+
* @var \Illuminate\Contracts\Cache\Factory
17+
*/
18+
private $cache;
19+
20+
/**
21+
* FileController constructor.
22+
*
23+
* @param \Illuminate\Contracts\Cache\Repository $cache
24+
*/
25+
public function __construct(Repository $cache)
26+
{
27+
parent::__construct();
28+
29+
$this->cache = $cache;
30+
}
31+
32+
/**
33+
* Configure a reference to a file to download in the cache so that when the
34+
* user hits the Daemon and it verifies with the Panel they'll actually be able
35+
* to download that file.
36+
*
37+
* Returns the token that needs to be used when downloading the file.
38+
*
39+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest $request
40+
* @return \Illuminate\Http\JsonResponse
41+
* @throws \Exception
42+
*/
43+
public function download(DownloadFileRequest $request): JsonResponse
44+
{
45+
/** @var \Pterodactyl\Models\Server $server */
46+
$server = $request->getModel(Server::class);
47+
$token = Uuid::uuid4()->toString();
48+
49+
$this->cache->put(
50+
'Server:Downloads:' . $token, ['server' => $server->uuid, 'path' => $request->route()->parameter('file')], Carbon::now()->addMinutes(5)
51+
);
52+
53+
return JsonResponse::create(['token' => $token]);
54+
}
55+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Files;
4+
5+
use Pterodactyl\Models\Server;
6+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7+
8+
class DownloadFileRequest extends ClientApiRequest
9+
{
10+
/**
11+
* Ensure that the user making this request has permission to download files
12+
* from this server.
13+
*
14+
* @return bool
15+
*/
16+
public function authorize(): bool
17+
{
18+
return $this->user()->can('download-files', $this->getModel(Server::class));
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import http from "@/api/http";
2+
// @ts-ignore
3+
import route from '../../../../../../vendor/tightenco/ziggy/src/js/route';
4+
5+
/**
6+
* Gets a download token for a file on the server.
7+
*/
8+
export function getDownloadToken(server: string, file: string): Promise<string | null> {
9+
return new Promise((resolve, reject) => {
10+
http.post(route('api.client.servers.files.download', { server, file }))
11+
.then(response => resolve(response.data ? response.data.token || null : null))
12+
.catch(reject);
13+
});
14+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</div>
2020
<div class="action">Copy</div>
2121
</div>
22-
<div class="context-row">
22+
<div class="context-row" v-on:click="triggerAction('download')">
2323
<div class="icon">
2424
<Icon name="download" class="h-4"/>
2525
</div>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
v-on:action:rename="showModal('rename')"
3636
v-on:action:copy="showModal('copy')"
3737
v-on:action:move="showModal('move')"
38+
v-on:action:download="showModal('download')"
3839
ref="contextMenu"
3940
/>
4041
<CopyFileModal :file="file" v-if="modals.copy" v-on:close="$emit('list')"/>
42+
<DownloadFileModal :file="file" v-if="modals.download" v-on:close="modals.download = false"/>
4143
<DeleteFileModal :visible.sync="modals.delete" :object="file" v-on:deleted="$emit('deleted')" v-on:close="modal.delete = false"/>
4244
<RenameModal :visible.sync="modals.rename" :object="file" v-on:renamed="$emit('list')" v-on:close="modal.rename = false"/>
4345
<MoveFileModal :visible.sync="modals.move" :file="file" v-on:moved="$emit('list')" v-on:close="modal.move = false"/>
@@ -54,6 +56,7 @@
5456
import DeleteFileModal from "@/components/server/components/filemanager/modals/DeleteFileModal.vue";
5557
import RenameModal from "@/components/server/components/filemanager/modals/RenameModal.vue";
5658
import CopyFileModal from "@/components/server/components/filemanager/modals/CopyFileModal.vue";
59+
import DownloadFileModal from "@/components/server/components/filemanager/modals/DownloadFileModal.vue";
5760
import MoveFileModal from "@/components/server/components/filemanager/modals/MoveFileModal.vue";
5861
5962
type DataStructure = {
@@ -64,7 +67,7 @@
6467
6568
export default Vue.extend({
6669
name: 'FileRow',
67-
components: {CopyFileModal, DeleteFileModal, MoveFileModal, Icon, FileContextMenu, RenameModal},
70+
components: {CopyFileModal, DownloadFileModal, DeleteFileModal, MoveFileModal, Icon, FileContextMenu, RenameModal},
6871
6972
props: {
7073
file: {
@@ -87,6 +90,7 @@
8790
delete: false,
8891
copy: false,
8992
move: false,
93+
download: false,
9094
},
9195
};
9296
},
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<template>
2+
<SpinnerModal :visible="true">
3+
Downloading {{ file.name }}...
4+
</SpinnerModal>
5+
</template>
6+
7+
<script lang="ts">
8+
import Vue from 'vue';
9+
import SpinnerModal from "../../../../core/SpinnerModal.vue";
10+
import {DirectoryContentObject} from '@/api/server/types';
11+
import {mapState} from "vuex";
12+
import {ServerState} from '@/store/types';
13+
import { join } from 'path';
14+
import {AxiosError} from "axios";
15+
import {getDownloadToken} from '@/api/server/files/getDownloadToken';
16+
17+
export default Vue.extend({
18+
components: { SpinnerModal },
19+
20+
computed: mapState('server', {
21+
server: (state: ServerState) => state.server,
22+
credentials: (state: ServerState) => state.credentials,
23+
fm: (state: ServerState) => state.fm,
24+
}),
25+
26+
props: {
27+
file: { type: Object as () => DirectoryContentObject, required: true },
28+
},
29+
30+
/**
31+
* This modal works differently than the other modals that exist for the file manager.
32+
* When it is mounted we will immediately show the spinner, and then begin the operation
33+
* to get the download token and redirect the user to that new URL.
34+
*/
35+
mounted: function () {
36+
const path = join(this.fm.currentDirectory, this.file.name);
37+
38+
getDownloadToken(this.server.uuid, path)
39+
.then((token) => {
40+
if (token) {
41+
window.location.href = `${this.credentials.node}/v1/server/file/download/${token}`;
42+
}
43+
})
44+
.catch((error: AxiosError) => {
45+
alert(`There was an error trying to download this ${this.file.directory ? 'folder' : 'file'}: ${error.message}`);
46+
console.error('Error at Server::Files::Download', {error});
47+
})
48+
.then(() => this.$emit('close'));
49+
},
50+
})
51+
</script>

0 commit comments

Comments
 (0)