Skip to content

Commit 280633b

Browse files
committed
More service classes for pack management
1 parent 9d3dca8 commit 280633b

File tree

13 files changed

+568
-52
lines changed

13 files changed

+568
-52
lines changed

app/Exceptions/Service/Pack/InvalidFileMimeTypeException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
namespace Pterodactyl\Exceptions\Service\Pack;
2626

27-
class InvalidFileMimeTypeException extends \Exception
27+
use Pterodactyl\Exceptions\DisplayException;
28+
29+
class InvalidFileMimeTypeException extends DisplayException
2830
{
2931
//
3032
}

app/Exceptions/Service/Pack/InvalidFileUploadException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
namespace Pterodactyl\Exceptions\Service\Pack;
2626

27-
class InvalidFileUploadException extends \Exception
27+
use Pterodactyl\Exceptions\DisplayException;
28+
29+
class InvalidFileUploadException extends DisplayException
2830
{
2931
//
3032
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Exceptions\Service\Pack;
26+
27+
use Pterodactyl\Exceptions\DisplayException;
28+
29+
class InvalidPackArchiveFormatException extends DisplayException
30+
{
31+
//
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Exceptions\Service\Pack;
26+
27+
use Pterodactyl\Exceptions\DisplayException;
28+
29+
class UnreadableZipArchiveException extends DisplayException
30+
{
31+
//
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Exceptions\Service\Pack;
26+
27+
use Pterodactyl\Exceptions\DisplayException;
28+
29+
class ZipExtractionException extends DisplayException
30+
{
31+
}

app/Http/Controllers/Admin/PackController.php

Lines changed: 87 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626

2727
use Log;
2828
use Alert;
29+
use Prologue\Alerts\AlertsMessageBag;
30+
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
31+
use Pterodactyl\Services\Packs\PackCreationService;
32+
use Pterodactyl\Services\Packs\PackDeletionService;
33+
use Pterodactyl\Services\Packs\PackUpdateService;
34+
use Pterodactyl\Services\Packs\TemplateUploadService;
2935
use Storage;
3036
use Illuminate\Http\Request;
3137
use Pterodactyl\Models\Pack;
@@ -37,10 +43,66 @@
3743

3844
class PackController extends Controller
3945
{
46+
/**
47+
* @var \Prologue\Alerts\AlertsMessageBag
48+
*/
49+
protected $alert;
50+
51+
/**
52+
* @var \Pterodactyl\Services\Packs\PackCreationService
53+
*/
54+
protected $creationService;
55+
56+
/**
57+
* @var \Pterodactyl\Services\Packs\PackDeletionService
58+
*/
59+
protected $deletionService;
60+
61+
/**
62+
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface
63+
*/
64+
protected $repository;
65+
66+
/**
67+
* @var \Pterodactyl\Services\Packs\PackUpdateService
68+
*/
69+
protected $packUpdateService;
70+
71+
/**
72+
* @var \Pterodactyl\Services\Packs\TemplateUploadService
73+
*/
74+
protected $templateUploadService;
75+
76+
/**
77+
* PackController constructor.
78+
*
79+
* @param \Prologue\Alerts\AlertsMessageBag $alert
80+
* @param \Pterodactyl\Services\Packs\PackCreationService $creationService
81+
* @param \Pterodactyl\Services\Packs\PackDeletionService $deletionService
82+
* @param \Pterodactyl\Contracts\Repository\PackRepositoryInterface $repository
83+
* @param \Pterodactyl\Services\Packs\PackUpdateService $packUpdateService
84+
* @param \Pterodactyl\Services\Packs\TemplateUploadService $templateUploadService
85+
*/
86+
public function __construct(
87+
AlertsMessageBag $alert,
88+
PackCreationService $creationService,
89+
PackDeletionService $deletionService,
90+
PackRepositoryInterface $repository,
91+
PackUpdateService $packUpdateService,
92+
TemplateUploadService $templateUploadService
93+
) {
94+
$this->alert = $alert;
95+
$this->creationService = $creationService;
96+
$this->deletionService = $deletionService;
97+
$this->repository = $repository;
98+
$this->packUpdateService = $packUpdateService;
99+
$this->templateUploadService = $templateUploadService;
100+
}
101+
40102
/**
41103
* Display listing of all packs on the system.
42104
*
43-
* @param \Illuminate\Http\Request $request
105+
* @param \Illuminate\Http\Request $request
44106
* @return \Illuminate\View\View
45107
*/
46108
public function index(Request $request)
@@ -57,7 +119,7 @@ public function index(Request $request)
57119
/**
58120
* Display new pack creation form.
59121
*
60-
* @param \Illuminate\Http\Request $request
122+
* @param \Illuminate\Http\Request $request
61123
* @return \Illuminate\View\View
62124
*/
63125
public function create(Request $request)
@@ -70,7 +132,7 @@ public function create(Request $request)
70132
/**
71133
* Display new pack creation modal for use with template upload.
72134
*
73-
* @param \Illuminate\Http\Request $request
135+
* @param \Illuminate\Http\Request $request
74136
* @return \Illuminate\View\View
75137
*/
76138
public function newTemplate(Request $request)
@@ -83,42 +145,34 @@ public function newTemplate(Request $request)
83145
/**
84146
* Handle create pack request and route user to location.
85147
*
86-
* @param \Illuminate\Http\Request $request
148+
* @param \Illuminate\Http\Request $request
87149
* @return \Illuminate\View\View
150+
*
151+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
152+
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException
153+
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException
154+
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidPackArchiveFormatException
155+
* @throws \Pterodactyl\Exceptions\Service\Pack\UnreadableZipArchiveException
156+
* @throws \Pterodactyl\Exceptions\Service\Pack\ZipExtractionException
88157
*/
89158
public function store(Request $request)
90159
{
91-
$repo = new PackRepository;
92-
93-
try {
94-
if ($request->input('action') === 'from_template') {
95-
$pack = $repo->createWithTemplate($request->intersect(['option_id', 'file_upload']));
96-
} else {
97-
$pack = $repo->create($request->intersect([
98-
'name', 'description', 'version', 'option_id',
99-
'selectable', 'visible', 'locked', 'file_upload',
100-
]));
101-
}
102-
Alert::success('Pack successfully created on the system.')->flash();
103-
104-
return redirect()->route('admin.packs.view', $pack->id);
105-
} catch (DisplayValidationException $ex) {
106-
return redirect()->route('admin.packs.new')->withErrors(json_decode($ex->getMessage()))->withInput();
107-
} catch (DisplayException $ex) {
108-
Alert::danger($ex->getMessage())->flash();
109-
} catch (\Exception $ex) {
110-
Log::error($ex);
111-
Alert::danger('An error occured while attempting to add a new service pack. This error has been logged.')->flash();
160+
if ($request->has('from_template')) {
161+
$pack = $this->templateUploadService->handle($request->input('option_id'), $request->input('file_upload'));
162+
} else {
163+
$pack = $this->creationService->handle($request->normalize(), $request->input('file_upload'));
112164
}
113165

114-
return redirect()->route('admin.packs.new')->withInput();
166+
$this->alert->success(trans('admin/pack.notices.pack_created'))->flash();
167+
168+
return redirect()->route('admin.packs.view', $pack->id);
115169
}
116170

117171
/**
118172
* Display pack view template to user.
119173
*
120-
* @param \Illuminate\Http\Request $request
121-
* @param int $id
174+
* @param \Illuminate\Http\Request $request
175+
* @param int $id
122176
* @return \Illuminate\View\View
123177
*/
124178
public function view(Request $request, $id)
@@ -132,8 +186,8 @@ public function view(Request $request, $id)
132186
/**
133187
* Handle updating or deleting pack information.
134188
*
135-
* @param \Illuminate\Http\Request $request
136-
* @param int $id
189+
* @param \Illuminate\Http\Request $request
190+
* @param int $id
137191
* @return \Illuminate\Http\RedirectResponse
138192
*/
139193
public function update(Request $request, $id)
@@ -168,9 +222,9 @@ public function update(Request $request, $id)
168222
/**
169223
* Creates an archive of the pack and downloads it to the browser.
170224
*
171-
* @param \Illuminate\Http\Request $request
172-
* @param int $id
173-
* @param bool $files
225+
* @param \Illuminate\Http\Request $request
226+
* @param int $id
227+
* @param bool $files
174228
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
175229
*/
176230
public function export(Request $request, $id, $files = false)

app/Services/Packs/PackCreationService.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@
2424

2525
namespace Pterodactyl\Services\Packs;
2626

27+
use Illuminate\Http\UploadedFile;
2728
use Ramsey\Uuid\Uuid;
2829
use Illuminate\Database\ConnectionInterface;
2930
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
30-
use Illuminate\Contracts\Config\Repository as ConfigRepository;
3131
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
3232
use Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException;
3333
use Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException;
3434

3535
class PackCreationService
3636
{
37-
/**
38-
* @var \Illuminate\Contracts\Config\Repository
39-
*/
40-
protected $config;
37+
const VALID_UPLOAD_TYPES = [
38+
'application/gzip',
39+
'application/x-gzip',
40+
];
4141

4242
/**
4343
* @var \Illuminate\Database\ConnectionInterface
@@ -57,18 +57,15 @@ class PackCreationService
5757
/**
5858
* PackCreationService constructor.
5959
*
60-
* @param \Illuminate\Contracts\Config\Repository $config
6160
* @param \Illuminate\Database\ConnectionInterface $connection
6261
* @param \Illuminate\Contracts\Filesystem\Factory $storage
6362
* @param \Pterodactyl\Contracts\Repository\PackRepositoryInterface $repository
6463
*/
6564
public function __construct(
66-
ConfigRepository $config,
6765
ConnectionInterface $connection,
6866
FilesystemFactory $storage,
6967
PackRepositoryInterface $repository
7068
) {
71-
$this->config = $config;
7269
$this->connection = $connection;
7370
$this->repository = $repository;
7471
$this->storage = $storage;
@@ -85,15 +82,17 @@ public function __construct(
8582
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException
8683
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException
8784
*/
88-
public function handle(array $data, $file = null)
85+
public function handle(array $data, UploadedFile $file = null)
8986
{
9087
if (! is_null($file)) {
9188
if (! $file->isValid()) {
92-
throw new InvalidFileUploadException;
89+
throw new InvalidFileUploadException(trans('admin/exceptions.packs.invalid_upload'));
9390
}
9491

95-
if (! in_array($file->getMimeType(), $this->config->get('pterodactyl.files.pack_types'))) {
96-
throw new InvalidFileMimeTypeException;
92+
if (! in_array($file->getMimeType(), self::VALID_UPLOAD_TYPES)) {
93+
throw new InvalidFileMimeTypeException(trans('admin/exceptions.packs.invalid_mime', [
94+
'type' => implode(', ', self::VALID_UPLOAD_TYPES),
95+
]));
9796
}
9897
}
9998

@@ -107,7 +106,7 @@ public function handle(array $data, $file = null)
107106
['uuid' => Uuid::uuid4()], $data
108107
));
109108

110-
$this->storage->disk('default')->makeDirectory('packs/' . $pack->uuid);
109+
$this->storage->disk()->makeDirectory('packs/' . $pack->uuid);
111110
if (! is_null($file)) {
112111
$file->storeAs('packs/' . $pack->uuid, 'archive.tar.gz');
113112
}

0 commit comments

Comments
 (0)