Finish pack management in Admin CP

This commit is contained in:
Dane Everitt 2017-03-15 20:52:37 -04:00
parent 4730309589
commit 1c47b2ed55
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
13 changed files with 352 additions and 455 deletions

View file

@ -80,6 +80,12 @@ class PackController extends Controller
]);
}
/**
* Handle create pack request and route user to location.
*
* @param Request $request
* @return \Illuminate\View\View
*/
public function create(Request $request)
{
$repo = new PackRepository;
@ -108,43 +114,102 @@ class PackController extends Controller
return redirect()->route('admin.packs.new')->withInput();
}
/**
* Display pack view template to user.
*
* @param Request $request
* @param int $id
* @return \Illuminate\View\View
*/
public function view(Request $request, $id)
{
return view('admin.packs.view', [
'pack' => Pack::with('servers.node', 'servers.user')->findOrFail($id),
'services' => Service::with('options')->get(),
]);
}
// public function export(Request $request, $id, $files = false)
// {
// $pack = Models\Pack::findOrFail($id);
// $json = [
// 'name' => $pack->name,
// 'version' => $pack->version,
// 'description' => $pack->dscription,
// 'selectable' => (bool) $pack->selectable,
// 'visible' => (bool) $pack->visible,
// ];
/**
* Handle updating or deleting pack information.
*
* @param Request $request
* @param int $id
* @return \Illuminate\Response\RedirectResponse
*/
public function update(Request $request, $id)
{
$repo = new PackRepository;
// $filename = tempnam(sys_get_temp_dir(), 'pterodactyl_');
// if ((bool) $files) {
// $zip = new \ZipArchive;
// if (! $zip->open($filename, \ZipArchive::CREATE)) {
// abort(503, 'Unable to open file for writing.');
// }
try {
if ($request->input('action') !== 'delete') {
$pack = $repo->update($id, $request->intersect([
'name', 'description', 'version',
'option_id', 'selectable', 'visible', 'locked'
]));
Alert::success('Pack successfully updated.')->flash();
} else {
$repo->delete($id);
Alert::success('Pack was successfully deleted from the system.')->flash();
// $files = Storage::files('packs/' . $pack->uuid);
// foreach ($files as $file) {
// $zip->addFile(storage_path('app/' . $file), basename(storage_path('app/' . $file)));
// }
return redirect()->route('admin.packs');
}
} catch(DisplayValidationException $ex) {
return redirect()->route('admin.packs.view', $id)->withErrors(json_decode($ex->getMessage()));
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An error occured while attempting to edit this service pack. This error has been logged.')->flash();
}
// $zip->addFromString('import.json', json_encode($json, JSON_PRETTY_PRINT));
// $zip->close();
return redirect()->route('admin.packs.view', $id);
}
// return response()->download($filename, 'pack-' . $pack->name . '.zip')->deleteFileAfterSend(true);
// } else {
// $fp = fopen($filename, 'a+');
// fwrite($fp, json_encode($json, JSON_PRETTY_PRINT));
// fclose($fp);
/**
* Creates an archive of the pack and downloads it to the browser.
*
* @param Request $request
* @param int $id
* @param bool $files
* @return \Illuminate\Response\BinaryFileResponse
*/
public function export(Request $request, $id, $files = false)
{
$pack = Pack::findOrFail($id);
$json = [
'name' => $pack->name,
'version' => $pack->version,
'description' => $pack->description,
'selectable' => $pack->selectable,
'visible' => $pack->visible,
'locked' => $pack->locked,
];
// return response()->download($filename, 'pack-' . $pack->name . '.json', [
// 'Content-Type' => 'application/json',
// ])->deleteFileAfterSend(true);
// }
// }
$filename = tempnam(sys_get_temp_dir(), 'pterodactyl_');
if ($files === 'with-files') {
$zip = new \ZipArchive;
if (! $zip->open($filename, \ZipArchive::CREATE)) {
abort(503, 'Unable to open file for writing.');
}
$files = Storage::files('packs/' . $pack->uuid);
foreach ($files as $file) {
$zip->addFile(storage_path('app/' . $file), basename(storage_path('app/' . $file)));
}
$zip->addFromString('import.json', json_encode($json, JSON_PRETTY_PRINT));
$zip->close();
return response()->download($filename, 'pack-' . $pack->name . '.zip')->deleteFileAfterSend(true);
} else {
$fp = fopen($filename, 'a+');
fwrite($fp, json_encode($json, JSON_PRETTY_PRINT));
fclose($fp);
return response()->download($filename, 'pack-' . $pack->name . '.json', [
'Content-Type' => 'application/json',
])->deleteFileAfterSend(true);
}
}
}

View file

@ -98,10 +98,6 @@ class RemoteController extends Controller
], 403);
}
// Passes Validation, Setup Notifications
$notify = new NotificationService($server);
$notify->pass($request->input('notification'));
return response('', 201);
}

View file

@ -474,6 +474,13 @@ class AdminRoutes
'as' => 'admin.packs.view',
'uses' => 'Admin\PackController@view',
]);
$router->post('/view/{id}', 'Admin\PackController@update');
$router->post('/view/{id}/export/{files?}', [
'as' => 'admin.packs.view.export',
'uses' => 'Admin\PackController@export',
]);
});
}
}

View file

@ -24,6 +24,8 @@
namespace Pterodactyl\Models;
use File;
use Storage;
use Illuminate\Database\Eloquent\Model;
use Nicolaslopezj\Searchable\SearchableTrait;
@ -78,6 +80,29 @@ class Pack extends Model
],
];
/**
* Returns all of the archived files for a given pack.
*
* @param bool $collection
* @return \Illuminate\Support\Collection|object
*/
public function files($collection = false)
{
$files = collect(Storage::files('packs/' . $this->uuid));
$files = $files->map(function ($item) {
$path = storage_path('app/' . $item);
return (object) [
'name' => basename($item),
'hash' => sha1_file($path),
'size' => File::humanReadableSize($path),
];
});
return ($collection) ? $files : (object) $files->all();
}
/**
* Gets option associated with a service pack.
*

View file

@ -22,41 +22,31 @@
* SOFTWARE.
*/
namespace Pterodactyl\Services;
namespace Pterodactyl\Providers;
use Pterodactyl\Models\Server;
use Pterodactyl\Notifications\Daemon;
use File;
use Illuminate\Support\ServiceProvider;
class NotificationService
class MacroServiceProvider extends ServiceProvider
{
protected $server;
protected $user;
/**
* Daemon will pass an event name, this matches that event name with the notification to send.
* @var array
* Bootstrap the application services.
*
* @return void
*/
protected $types = [
// 'crashed' => 'CrashNotification',
// 'started' => 'StartNotification',
// 'stopped' => 'StopNotification',
// 'rebuild' => 'RebuildNotification'
];
public function __construct(Server $server)
public function boot()
{
$this->server = $server;
}
File::macro('humanReadableSize', function ($path, $precision = 2) {
$size = File::size($path);
static $units = ['B', 'kB', 'MB', 'GB', 'TB'];
public function pass(array $notification)
{
if (! $notification->type) {
return;
}
$i = 0;
while (($size / 1024) > 0.9) {
$size = $size / 1024;
$i++;
}
if (class_exists($this->types[$notification->type]::class)) {
$user->notify(new $this->types[$notification->type]($notification->payload));
}
return round($size, ($i < 2) ? 0 : $precision) . ' ' . $units[$i];
});
}
}

View file

@ -177,12 +177,14 @@ class PackRepository
* @param array $data
* @return \Pterodactyl\Models\Pack
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function update($id, array $data)
{
$validator = Validator::make($data, [
'name' => 'sometimes|required|string',
'option_id' => 'sometimes|required|exists:service_options,id',
'version' => 'sometimes|required|string',
'description' => 'sometimes|string',
'selectable' => 'sometimes|required|boolean',
@ -194,14 +196,20 @@ class PackRepository
throw new DisplayValidationException(json_encode($validator->errors()));
}
$pack = Pack::findOrFail($id);
$pack = Pack::withCount('servers')->findOrFail($id);
if ($pack->servers_count > 0 && (isset($data['option_id']) && (int) $data['option_id'] !== $pack->option_id)) {
throw new DisplayException('You cannot modify the associated option if servers are attached to a pack.');
}
$pack->fill([
'name' => isset($data['name']) ? $data['name'] : $pack->name,
'option_id' => isset($data['option_id']) ? $data['option_id'] : $pack->option_id,
'version' => isset($data['version']) ? $data['version'] : $pack->version,
'description' => (empty($data['description'])) ? null : $data['description'],
'selectable' => isset($data['selectable']) ? $data['selectable'] : $data->selectable,
'visible' => isset($data['visible']) ? $data['visible'] : $data->visible,
'locked' => isset($data['locked']) ? $data['locked'] : $data->locked,
'selectable' => isset($data['selectable']),
'visible' => isset($data['visible']),
'locked' => isset($data['locked']),
])->save();
return $pack;

View file