Skip to content

Commit 50558db

Browse files
committed
Add initial pack creation and overview pages
1 parent 2d90187 commit 50558db

File tree

21 files changed

+943
-486
lines changed

21 files changed

+943
-486
lines changed

app/Http/Controllers/Admin/PackController.php

Lines changed: 87 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -27,179 +27,124 @@
2727
use Log;
2828
use Alert;
2929
use Storage;
30-
use Pterodactyl\Models;
3130
use Illuminate\Http\Request;
31+
use Pterodactyl\Models\Pack;
32+
use Pterodactyl\Models\Service;
3233
use Pterodactyl\Exceptions\DisplayException;
3334
use Pterodactyl\Http\Controllers\Controller;
34-
use Pterodactyl\Repositories\ServiceRepository\Pack;
35+
use Pterodactyl\Repositories\PackRepository;
3536
use Pterodactyl\Exceptions\DisplayValidationException;
3637

3738
class PackController extends Controller
3839
{
39-
public function __construct()
40+
/**
41+
* Display listing of all packs on the system.
42+
*
43+
* @param Request $request
44+
* @return \Illuminate\View\View
45+
*/
46+
public function index(Request $request)
4047
{
41-
//
42-
}
48+
$packs = Pack::with('option')->withCount('servers');
4349

44-
public function listAll(Request $request)
45-
{
46-
return view('admin.services.packs.index', ['services' => Models\Service::all()]);
47-
}
50+
if (! is_null($request->input('query'))) {
51+
$packs->search($request->input('query'));
52+
}
4853

49-
public function listByOption(Request $request, $id)
50-
{
51-
return view('admin.services.packs.byoption', [
52-
'option' => Models\ServiceOption::with('service', 'packs')->findOrFail($id),
53-
]);
54+
return view('admin.packs.index', ['packs' => $packs->paginate(50)]);
5455
}
5556

56-
public function listByService(Request $request, $id)
57+
/**
58+
* Display new pack creation form.
59+
*
60+
* @param Request $request
61+
* @return \Illuminate\View\View
62+
*/
63+
public function new(Request $request)
5764
{
58-
return view('admin.services.packs.byservice', [
59-
'service' => Models\Service::with('options', 'options.packs')->findOrFail($id),
65+
return view('admin.packs.new', [
66+
'services' => Service::with('options')->get(),
6067
]);
6168
}
6269

63-
public function new(Request $request, $opt = null)
70+
/**
71+
* Display new pack creation modal for use with template upload.
72+
*
73+
* @param Request $request
74+
* @return \Illuminate\View\View
75+
*/
76+
public function newTemplate(Request $request)
6477
{
65-
return view('admin.services.packs.new', [
66-
'services' => Models\Service::with('options')->get(),
78+
return view('admin.packs.modal', [
79+
'services' => Service::with('options')->get(),
6780
]);
6881
}
6982

7083
public function create(Request $request)
7184
{
72-
try {
73-
$repo = new Pack;
74-
$pack = $repo->create($request->only([
75-
'name', 'version', 'description',
76-
'option', 'selectable', 'visible',
77-
'file_upload',
78-
]));
79-
Alert::success('Successfully created new service!')->flash();
80-
81-
return redirect()->route('admin.services.packs.edit', $pack->id)->withInput();
82-
} catch (DisplayValidationException $ex) {
83-
return redirect()->route('admin.services.packs.new', $request->input('option'))->withErrors(json_decode($ex->getMessage()))->withInput();
84-
} catch (DisplayException $ex) {
85-
Alert::danger($ex->getMessage())->flash();
86-
} catch (\Exception $ex) {
87-
Log::error($ex);
88-
Alert::danger('An error occured while attempting to add a new service pack.')->flash();
89-
}
85+
$repo = new PackRepository;
9086

91-
return redirect()->route('admin.services.packs.new', $request->input('option'))->withInput();
92-
}
93-
94-
public function edit(Request $request, $id)
95-
{
96-
$pack = Models\ServicePack::with('option.service')->findOrFail($id);
97-
98-
return view('admin.services.packs.edit', [
99-
'pack' => $pack,
100-
'services' => Models\Service::all()->load('options'),
101-
'files' => Storage::files('packs/' . $pack->uuid),
102-
]);
103-
}
104-
105-
public function update(Request $request, $id)
106-
{
107-
if (! is_null($request->input('action_delete'))) {
108-
try {
109-
$repo = new Pack;
110-
$repo->delete($id);
111-
Alert::success('The requested service pack has been deleted from the system.')->flash();
112-
113-
return redirect()->route('admin.services.packs');
114-
} catch (DisplayException $ex) {
115-
Alert::danger($ex->getMessage())->flash();
116-
} catch (\Exception $ex) {
117-
Log::error($ex);
118-
Alert::danger('An error occured while attempting to delete this pack.')->flash();
119-
}
120-
121-
return redirect()->route('admin.services.packs.edit', $id);
122-
} else {
123-
try {
124-
$repo = new Pack;
125-
$repo->update($id, $request->only([
126-
'name', 'version', 'description',
127-
'option', 'selectable', 'visible',
87+
try {
88+
if ($request->input('action') === 'from_template') {
89+
$pack = $repo->createWithTemplate($request->intersect(['option_id', 'file_upload']));
90+
} else {
91+
$pack = $repo->create($request->intersect([
92+
'name', 'description', 'version', 'option_id',
93+
'selectable', 'visible', 'locked', 'file_upload',
12894
]));
129-
Alert::success('Service pack has been successfully updated.')->flash();
130-
} catch (DisplayValidationException $ex) {
131-
return redirect()->route('admin.services.packs.edit', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
132-
} catch (\Exception $ex) {
133-
Log::error($ex);
134-
Alert::danger('An error occured while attempting to add edit this pack.')->flash();
13595
}
96+
Alert::success('Pack successfully created on the system.')->flash();
13697

137-
return redirect()->route('admin.services.packs.edit', $id);
138-
}
139-
}
140-
141-
public function export(Request $request, $id, $files = false)
142-
{
143-
$pack = Models\ServicePack::findOrFail($id);
144-
$json = [
145-
'name' => $pack->name,
146-
'version' => $pack->version,
147-
'description' => $pack->dscription,
148-
'selectable' => (bool) $pack->selectable,
149-
'visible' => (bool) $pack->visible,
150-
];
151-
152-
$filename = tempnam(sys_get_temp_dir(), 'pterodactyl_');
153-
if ((bool) $files) {
154-
$zip = new \ZipArchive;
155-
if (! $zip->open($filename, \ZipArchive::CREATE)) {
156-
abort(503, 'Unable to open file for writing.');
157-
}
158-
159-
$files = Storage::files('packs/' . $pack->uuid);
160-
foreach ($files as $file) {
161-
$zip->addFile(storage_path('app/' . $file), basename(storage_path('app/' . $file)));
162-
}
163-
164-
$zip->addFromString('import.json', json_encode($json, JSON_PRETTY_PRINT));
165-
$zip->close();
166-
167-
return response()->download($filename, 'pack-' . $pack->name . '.zip')->deleteFileAfterSend(true);
168-
} else {
169-
$fp = fopen($filename, 'a+');
170-
fwrite($fp, json_encode($json, JSON_PRETTY_PRINT));
171-
fclose($fp);
172-
173-
return response()->download($filename, 'pack-' . $pack->name . '.json', [
174-
'Content-Type' => 'application/json',
175-
])->deleteFileAfterSend(true);
176-
}
177-
}
178-
179-
public function uploadForm(Request $request, $for = null)
180-
{
181-
return view('admin.services.packs.upload', [
182-
'services' => Models\Service::all()->load('options'),
183-
]);
184-
}
185-
186-
public function postUpload(Request $request)
187-
{
188-
try {
189-
$repo = new Pack;
190-
$pack = $repo->createWithTemplate($request->only(['option', 'file_upload']));
191-
Alert::success('Successfully created new service!')->flash();
192-
193-
return redirect()->route('admin.services.packs.edit', $pack->id)->withInput();
194-
} catch (DisplayValidationException $ex) {
195-
return redirect()->back()->withErrors(json_decode($ex->getMessage()))->withInput();
98+
return redirect()->route('admin.packs.view', $pack->id);
99+
} catch(DisplayValidationException $ex) {
100+
return redirect()->route('admin.packs.new')->withErrors(json_decode($ex->getMessage()))->withInput();
196101
} catch (DisplayException $ex) {
197102
Alert::danger($ex->getMessage())->flash();
198103
} catch (\Exception $ex) {
199104
Log::error($ex);
200-
Alert::danger('An error occured while attempting to add a new service pack.')->flash();
105+
Alert::danger('An error occured while attempting to add a new service pack. This error has been logged.')->flash();
201106
}
202107

203-
return redirect()->back();
108+
return redirect()->route('admin.packs.new')->withInput();
204109
}
110+
111+
112+
// public function export(Request $request, $id, $files = false)
113+
// {
114+
// $pack = Models\Pack::findOrFail($id);
115+
// $json = [
116+
// 'name' => $pack->name,
117+
// 'version' => $pack->version,
118+
// 'description' => $pack->dscription,
119+
// 'selectable' => (bool) $pack->selectable,
120+
// 'visible' => (bool) $pack->visible,
121+
// ];
122+
123+
// $filename = tempnam(sys_get_temp_dir(), 'pterodactyl_');
124+
// if ((bool) $files) {
125+
// $zip = new \ZipArchive;
126+
// if (! $zip->open($filename, \ZipArchive::CREATE)) {
127+
// abort(503, 'Unable to open file for writing.');
128+
// }
129+
130+
// $files = Storage::files('packs/' . $pack->uuid);
131+
// foreach ($files as $file) {
132+
// $zip->addFile(storage_path('app/' . $file), basename(storage_path('app/' . $file)));
133+
// }
134+
135+
// $zip->addFromString('import.json', json_encode($json, JSON_PRETTY_PRINT));
136+
// $zip->close();
137+
138+
// return response()->download($filename, 'pack-' . $pack->name . '.zip')->deleteFileAfterSend(true);
139+
// } else {
140+
// $fp = fopen($filename, 'a+');
141+
// fwrite($fp, json_encode($json, JSON_PRETTY_PRINT));
142+
// fclose($fp);
143+
144+
// return response()->download($filename, 'pack-' . $pack->name . '.json', [
145+
// 'Content-Type' => 'application/json',
146+
// ])->deleteFileAfterSend(true);
147+
// }
148+
// }
149+
205150
}

app/Http/Controllers/Daemon/PackController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct()
4747
*/
4848
public function pull(Request $request, $uuid)
4949
{
50-
$pack = Models\ServicePack::where('uuid', $uuid)->first();
50+
$pack = Models\Pack::where('uuid', $uuid)->first();
5151

5252
if (! $pack) {
5353
return response()->json(['error' => 'No such pack.'], 404);
@@ -68,7 +68,7 @@ public function pull(Request $request, $uuid)
6868
*/
6969
public function hash(Request $request, $uuid)
7070
{
71-
$pack = Models\ServicePack::where('uuid', $uuid)->first();
71+
$pack = Models\Pack::where('uuid', $uuid)->first();
7272

7373
if (! $pack) {
7474
return response()->json(['error' => 'No such pack.'], 404);

app/Http/Routes/AdminRoutes.php

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -453,43 +453,27 @@ public function map(Router $router)
453453
'csrf',
454454
],
455455
], function () use ($router) {
456-
// $router->get('/new/{option?}', [
457-
// 'as' => 'admin.packs.new',
458-
// 'uses' => 'Admin\PackController@new',
459-
// ]);
460-
// $router->post('/new', [
461-
// 'uses' => 'Admin\PackController@create',
462-
// ]);
463-
// $router->get('/upload/{option?}', [
464-
// 'as' => 'admin.packs.uploadForm',
465-
// 'uses' => 'Admin\PackController@uploadForm',
466-
// ]);
467-
// $router->post('/upload', [
468-
// 'uses' => 'Admin\PackController@postUpload',
469-
// ]);
470456
$router->get('/', [
471457
'as' => 'admin.packs',
472-
'uses' => 'Admin\PackController@listAll',
473-
]);
474-
// $router->get('/for/option/{option}', [
475-
// 'as' => 'admin.packs.option',
476-
// 'uses' => 'Admin\PackController@listByOption',
477-
// ]);
478-
// $router->get('/for/service/{service}', [
479-
// 'as' => 'admin.packs.service',
480-
// 'uses' => 'Admin\PackController@listByService',
481-
// ]);
482-
// $router->get('/edit/{pack}', [
483-
// 'as' => 'admin.packs.edit',
484-
// 'uses' => 'Admin\PackController@edit',
485-
// ]);
486-
// $router->post('/edit/{pack}', [
487-
// 'uses' => 'Admin\PackController@update',
488-
// ]);
489-
// $router->get('/edit/{pack}/export/{archive?}', [
490-
// 'as' => 'admin.packs.export',
491-
// 'uses' => 'Admin\PackController@export',
492-
// ]);
458+
'uses' => 'Admin\PackController@index',
459+
]);
460+
461+
$router->get('/new', [
462+
'as' => 'admin.packs.new',
463+
'uses' => 'Admin\PackController@new',
464+
]);
465+
466+
$router->post('/new', 'Admin\PackController@create');
467+
468+
$router->get('/new/template', [
469+
'as' => 'admin.packs.new.template',
470+
'uses' => 'Admin\PackController@newTemplate',
471+
]);
472+
473+
$router->get('/view/{id}', [
474+
'as' => 'admin.packs.view',
475+
'uses' => 'Admin\PackController@view',
476+
]);
493477
});
494478
}
495479
}

0 commit comments

Comments
 (0)