Skip to content

Commit 46117af

Browse files
committed
Code cleanup and fixes filemanager
1 parent a391a2d commit 46117af

File tree

9 files changed

+246
-23
lines changed

9 files changed

+246
-23
lines changed

app/Http/Controllers/Scales/FileController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Pterodactyl\Http\Controllers\Scales;
3+
namespace Pterodactyl\Http\Controllers\Daemon;
44

55
use \Exception;
66
use Log;
@@ -9,7 +9,7 @@
99
use Pterodactyl\Models\Node;
1010

1111
use Pterodactyl\Exceptions\DisplayException;
12-
use Pterodactyl\Http\Helpers;
12+
use Pterodactyl\Repositories\HelperRepository;
1313
use Pterodactyl\Http\Controllers\Controller;
1414

1515
use GuzzleHttp\Client;
@@ -76,7 +76,7 @@ public function returnFileContents($file)
7676
}
7777

7878
$file = (object) pathinfo($file);
79-
if (!in_array($file->extension, Helpers::editableFiles())) {
79+
if (!in_array($file->extension, HelperRepository::editableFiles())) {
8080
throw new DisplayException('You do not have permission to edit this type of file.');
8181
}
8282

@@ -111,7 +111,7 @@ public function saveFileContents($file, $content)
111111

112112
$file = (object) pathinfo($file);
113113

114-
if(!in_array($file->extension, Helpers::editableFiles())) {
114+
if(!in_array($file->extension, HelperRepository::editableFiles())) {
115115
throw new DisplayException('You do not have permission to edit this type of file.');
116116
}
117117

@@ -175,7 +175,7 @@ public function returnDirectoryListing($directory)
175175
'entry' => $value->name,
176176
'directory' => trim($directory, '/'),
177177
'extension' => pathinfo($value->name, PATHINFO_EXTENSION),
178-
'size' => Helpers::bytesToHuman($value->size),
178+
'size' => HelperRepository::bytesToHuman($value->size),
179179
'date' => strtotime($value->modified)
180180
]]);
181181

app/Http/Controllers/Server/AjaxController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
use Debugbar;
77
use Pterodactyl\Models\Server;
88
use Pterodactyl\Models\Node;
9-
use Pterodactyl\Http\Helpers;
109

1110
use Pterodactyl\Exceptions\DisplayException;
12-
use Pterodactyl\Http\Controllers\Scales\FileController;
11+
use Pterodactyl\Repositories;
1312
use Pterodactyl\Http\Controllers\Controller;
1413
use Illuminate\Http\Request;
1514

@@ -119,7 +118,7 @@ public function postDirectoryList(Request $request, $uuid)
119118
$prevDir['link_show'] = trim($prevDir['link'], '/');
120119
}
121120

122-
$controller = new FileController($uuid);
121+
$controller = new Repositories\Daemon\FileRepository($uuid);
123122

124123
try {
125124
$directoryContents = $controller->returnDirectoryListing($this->directory);
@@ -140,7 +139,7 @@ public function postDirectoryList(Request $request, $uuid)
140139
'server' => $server,
141140
'files' => $directoryContents->files,
142141
'folders' => $directoryContents->folders,
143-
'extensions' => Helpers::editableFiles(),
142+
'extensions' => Repositories\HelperRepository::editableFiles(),
144143
'directory' => $prevDir
145144
]);
146145

@@ -159,7 +158,7 @@ public function postSaveFile(Request $request, $uuid)
159158
$server = Server::getByUUID($uuid);
160159
$this->authorize('save-files', $server);
161160

162-
$controller = new FileController($uuid);
161+
$controller = new Repositories\Daemon\FileRepository($uuid);
163162

164163
try {
165164
$controller->saveFileContents($request->input('file'), $request->input('contents'));

app/Http/Controllers/Server/ServerController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Alert;
1212

1313
use Pterodactyl\Exceptions\DisplayException;
14-
use Pterodactyl\Http\Controllers\Scales\FileController;
14+
use Pterodactyl\Repositories;
1515
use Pterodactyl\Http\Controllers\Controller;
1616
use Illuminate\Http\Request;
1717

@@ -102,7 +102,7 @@ public function getEditFile(Request $request, $uuid, $file)
102102
$this->authorize('edit-files', $server);
103103

104104
$fileInfo = (object) pathinfo($file);
105-
$controller = new FileController($uuid);
105+
$controller = new Repositories\Daemon\FileRepository($uuid);
106106

107107
try {
108108
$fileContent = $controller->returnFileContents($file);
@@ -124,7 +124,7 @@ public function getEditFile(Request $request, $uuid, $file)
124124
'server' => $server,
125125
'node' => Node::find($server->node),
126126
'file' => $file,
127-
'contents' => $fileContent->contents,
127+
'contents' => $fileContent->content,
128128
'directory' => (in_array($fileInfo->dirname, ['.', './', '/'])) ? '/' : trim($fileInfo->dirname, '/') . '/',
129129
'extension' => $fileInfo->extension
130130
]);
@@ -155,7 +155,7 @@ public function getDownloadFile(Request $request, $uuid, $file)
155155

156156
$download->save();
157157

158-
return redirect('https://' . $node->fqdn . ':' . $node->daemonListen . '/server/download/' . $download->token);
158+
return redirect( $node->scheme . '://' . $node->fqdn . ':' . $node->daemonListen . '/server/download/' . $download->token);
159159

160160
}
161161

app/Models/Node.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static function guzzleRequest($node)
6969

7070
// @TODO: Better solution to disabling verification. Security risk.
7171
self::$guzzle[$node] = new Client([
72-
'base_uri' => sprintf('http%s://%s:%s/', ($nodeData->https === true) ? 's' : '', $nodeData->fqdn, $nodeData->daemonListen),
72+
'base_uri' => sprintf('%s://%s:%s/', $nodeData->scheme, $nodeData->fqdn, $nodeData->daemonListen),
7373
'timeout' => 10.0,
7474
'connect_timeout' => 5.0,
7575
'verify' => false,
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
3+
namespace Pterodactyl\Repositories\Daemon;
4+
5+
use \Exception;
6+
use Log;
7+
8+
use Pterodactyl\Models\Server;
9+
use Pterodactyl\Models\Node;
10+
use Pterodactyl\Repositories\HelperRepository;
11+
use Pterodactyl\Exceptions\DisplayException;
12+
13+
use GuzzleHttp\Client;
14+
use GuzzleHttp\Exception\RequestException;
15+
16+
class FileRepository
17+
{
18+
19+
/**
20+
* The Eloquent Model associated with the requested server.
21+
*
22+
* @var \Illuminate\Database\Eloquent\Model
23+
*/
24+
protected $server;
25+
26+
/**
27+
* The Eloquent Model for the node corresponding with the requested server.
28+
*
29+
* @var \Illuminate\Database\Eloquent\Model
30+
*/
31+
protected $node;
32+
33+
/**
34+
* The Guzzle Client associated with the requested server and node.
35+
*
36+
* @var \GuzzleHttp\Client
37+
*/
38+
protected $client;
39+
40+
/**
41+
* The Guzzle Client headers associated with the requested server and node.
42+
* (non-administrative headers)
43+
*
44+
* @var array
45+
*/
46+
protected $headers;
47+
48+
/**
49+
* Constructor
50+
*
51+
* @param string $server The server Short UUID
52+
*/
53+
public function __construct($uuid)
54+
{
55+
56+
$this->server = Server::getByUUID($uuid);
57+
$this->node = Node::getByID($this->server->node);
58+
$this->client = Node::guzzleRequest($this->server->node);
59+
$this->headers = Server::getGuzzleHeaders($uuid);
60+
61+
}
62+
63+
/**
64+
* Get the contents of a requested file for the server.
65+
*
66+
* @param string $file
67+
* @return string
68+
*/
69+
public function returnFileContents($file)
70+
{
71+
72+
if (empty($file)) {
73+
throw new Exception('Not all parameters were properly passed to the function.');
74+
}
75+
76+
$file = (object) pathinfo($file);
77+
if (!in_array($file->extension, HelperRepository::editableFiles())) {
78+
throw new DisplayException('You do not have permission to edit this type of file.');
79+
}
80+
81+
$file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/';
82+
83+
$res = $this->client->request('GET', '/server/file/' . rawurlencode($file->dirname.$file->basename), [
84+
'headers' => $this->headers
85+
]);
86+
87+
$json = json_decode($res->getBody());
88+
if($res->getStatusCode() !== 200 || !isset($json->content)) {
89+
throw new DisplayException('Scales provided a non-200 error code: HTTP\\' . $res->getStatusCode());
90+
}
91+
92+
return $json;
93+
94+
}
95+
96+
/**
97+
* Save the contents of a requested file on the Scales instance.
98+
*
99+
* @param string $file
100+
* @param string $content
101+
* @return boolean
102+
*/
103+
public function saveFileContents($file, $content)
104+
{
105+
106+
if (empty($file)) {
107+
throw new Exception('A valid file and path must be specified to save a file.');
108+
}
109+
110+
$file = (object) pathinfo($file);
111+
112+
if(!in_array($file->extension, HelperRepository::editableFiles())) {
113+
throw new DisplayException('You do not have permission to edit this type of file.');
114+
}
115+
116+
$file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/';
117+
118+
$res = $this->client->request('POST', '/server/file/' . rawurlencode($file->dirname.$file->basename), [
119+
'headers' => $this->headers,
120+
'json' => [
121+
'content' => $content
122+
]
123+
]);
124+
125+
if ($res->getStatusCode() !== 204) {
126+
throw new DisplayException('An error occured while attempting to save this file. ' . $res->getBody());
127+
}
128+
129+
return true;
130+
131+
}
132+
133+
/**
134+
* Returns a listing of all files and folders within a specified Scales directory.
135+
*
136+
* @param string $directory
137+
* @return object
138+
*/
139+
public function returnDirectoryListing($directory)
140+
{
141+
142+
if (empty($directory)) {
143+
throw new Exception('A valid directory must be specified in order to list its contents.');
144+
}
145+
146+
$res = $this->client->request('GET', '/server/directory/' . $directory, [
147+
'headers' => $this->headers
148+
]);
149+
150+
$json = json_decode($res->getBody());
151+
if($res->getStatusCode() !== 200) {
152+
throw new DisplayException('An error occured while attempting to save this file. ' . $res->getBody());
153+
}
154+
155+
// Iterate through results
156+
$files = [];
157+
$folders = [];
158+
foreach($json as &$value) {
159+
160+
if ($value->directory === true) {
161+
162+
// @TODO Handle Symlinks
163+
$folders = array_merge($folders, [[
164+
'entry' => $value->name,
165+
'directory' => trim($directory, '/'),
166+
'size' => null,
167+
'date' => strtotime($value->modified)
168+
]]);
169+
170+
} else if ($value->file === true) {
171+
172+
$files = array_merge($files, [[
173+
'entry' => $value->name,
174+
'directory' => trim($directory, '/'),
175+
'extension' => pathinfo($value->name, PATHINFO_EXTENSION),
176+
'size' => HelperRepository::bytesToHuman($value->size),
177+
'date' => strtotime($value->modified)
178+
]]);
179+
180+
}
181+
182+
}
183+
184+
return (object) [
185+
'files' => $files,
186+
'folders' => $folders,
187+
];
188+
189+
}
190+
191+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Pterodactyl\Http;
3+
namespace Pterodactyl\Repositories;
44

5-
class Helpers {
5+
class HelperRepository {
66

77
/**
88
* Listing of editable files in the control panel.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class NodeHttpsToScheme extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('nodes', function (Blueprint $table) {
16+
$table->string('https', 5)->default('http')->change();
17+
$table->renameColumn('https', 'scheme');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('nodes', function (Blueprint $table) {
29+
$table->boolean('scheme')->default(false)->change();
30+
$table->renameColumn('scheme', 'https');
31+
});
32+
}
33+
}

resources/views/server/files/index.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
@section('content')
88
<div class="col-md-9">
9-
<div class="row" id="internal_alert">
10-
<div class="col-md-12">
9+
<div class="row">
10+
<div class="col-md-12" id="internal_alert">
1111
<div class="alert alert-info">
1212
<i class="fa fa-spinner fa-spin"></i> {{ trans('server.files.loading') }}
1313
</div>
@@ -98,7 +98,7 @@ function reloadActionDelete () {
9898
9999
$.ajax({
100100
type: 'DELETE',
101-
url: 'https://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/file/' + deleteItemPath,
101+
url: '{{ $node->scheme }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/file/' + deleteItemPath,
102102
headers: {
103103
'X-Access-Token': '{{ $server->daemonSecret }}',
104104
'X-Access-Server': '{{ $server->uuid }}'

0 commit comments

Comments
 (0)