Merge pull request #214 from Pterodactyl/analysis-zYo1bG

Apply fixes from StyleCI
This commit is contained in:
Dane Everitt 2016-12-14 16:56:41 -05:00 committed by GitHub
commit 6d59824d00
9 changed files with 98 additions and 88 deletions

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Pterodactyl - Panel * Pterodactyl - Panel
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com> * Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -21,6 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
namespace Pterodactyl\Console\Commands; namespace Pterodactyl\Console\Commands;
use Carbon; use Carbon;
@ -62,7 +63,7 @@ class CleanServiceBackup extends Command
{ {
$files = Storage::files('services/.bak'); $files = Storage::files('services/.bak');
foreach($files as $file) { foreach ($files as $file) {
$lastModified = Carbon::createFromTimestamp(Storage::lastModified($file)); $lastModified = Carbon::createFromTimestamp(Storage::lastModified($file));
if ($lastModified->diffInMinutes(Carbon::now()) > 5) { if ($lastModified->diffInMinutes(Carbon::now()) > 5) {
$this->info('Deleting ' . $file); $this->info('Deleting ' . $file);

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Pterodactyl - Panel * Pterodactyl - Panel
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com> * Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -21,20 +21,19 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
namespace Pterodactyl\Http\Controllers\Admin; namespace Pterodactyl\Http\Controllers\Admin;
use Alert;
use DB; use DB;
use Log; use Log;
use Alert;
use Storage; use Storage;
use Pterodactyl\Models; use Pterodactyl\Models;
use Pterodactyl\Repositories\ServiceRepository\Pack;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Exceptions\DisplayValidationException;
use Pterodactyl\Exceptions\DisplayException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\ServiceRepository\Pack;
use Pterodactyl\Exceptions\DisplayValidationException;
class PackController extends Controller class PackController extends Controller
{ {
@ -52,14 +51,14 @@ class PackController extends Controller
)->join('services', 'services.id', '=', 'service_options.parent_service')->get(); )->join('services', 'services.id', '=', 'service_options.parent_service')->get();
$array = []; $array = [];
foreach($options as &$option) { foreach ($options as &$option) {
if (!array_key_exists($option->p_service, $array)) { if (! array_key_exists($option->p_service, $array)) {
$array[$option->p_service] = []; $array[$option->p_service] = [];
} }
$array[$option->p_service] = array_merge($array[$option->p_service], [[ $array[$option->p_service] = array_merge($array[$option->p_service], [[
'id' => $option->id, 'id' => $option->id,
'name' => $option->name 'name' => $option->name,
]]); ]]);
} }
@ -69,17 +68,18 @@ class PackController extends Controller
public function listAll(Request $request) public function listAll(Request $request)
{ {
return view('admin.services.packs.index', [ return view('admin.services.packs.index', [
'services' => Models\Service::all() 'services' => Models\Service::all(),
]); ]);
} }
public function listByOption(Request $request, $id) public function listByOption(Request $request, $id)
{ {
$option = Models\ServiceOptions::findOrFail($id); $option = Models\ServiceOptions::findOrFail($id);
return view('admin.services.packs.byoption', [ return view('admin.services.packs.byoption', [
'packs' => Models\ServicePack::where('option', $option->id)->get(), 'packs' => Models\ServicePack::where('option', $option->id)->get(),
'service' => Models\Service::findOrFail($option->parent_service), 'service' => Models\Service::findOrFail($option->parent_service),
'option' => $option 'option' => $option,
]); ]);
} }
@ -91,7 +91,7 @@ class PackController extends Controller
'service_options.id', 'service_options.id',
'service_options.name', 'service_options.name',
DB::raw('(SELECT COUNT(id) FROM service_packs WHERE service_packs.option = service_options.id) AS p_count') DB::raw('(SELECT COUNT(id) FROM service_packs WHERE service_packs.option = service_options.id) AS p_count')
)->where('parent_service', $id)->get() )->where('parent_service', $id)->get(),
]); ]);
} }
@ -108,9 +108,10 @@ class PackController extends Controller
try { try {
$repo = new Pack; $repo = new Pack;
$id = $repo->create($request->except([ $id = $repo->create($request->except([
'_token' '_token',
])); ]));
Alert::success('Successfully created new service!')->flash(); Alert::success('Successfully created new service!')->flash();
return redirect()->route('admin.services.packs.edit', $id)->withInput(); return redirect()->route('admin.services.packs.edit', $id)->withInput();
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
return redirect()->route('admin.services.packs.new', $request->input('option'))->withErrors(json_decode($ex->getMessage()))->withInput(); return redirect()->route('admin.services.packs.new', $request->input('option'))->withErrors(json_decode($ex->getMessage()))->withInput();
@ -120,6 +121,7 @@ class PackController extends Controller
Log::error($ex); Log::error($ex);
Alert::danger('An error occured while attempting to add a new service pack.')->flash(); Alert::danger('An error occured while attempting to add a new service pack.')->flash();
} }
return redirect()->route('admin.services.packs.new', $request->input('option'))->withInput(); return redirect()->route('admin.services.packs.new', $request->input('option'))->withInput();
} }
@ -127,22 +129,24 @@ class PackController extends Controller
{ {
$pack = Models\ServicePack::findOrFail($id); $pack = Models\ServicePack::findOrFail($id);
$option = Models\ServiceOptions::select('id', 'parent_service', 'name')->where('id', $pack->option)->first(); $option = Models\ServiceOptions::select('id', 'parent_service', 'name')->where('id', $pack->option)->first();
return view('admin.services.packs.edit', [ return view('admin.services.packs.edit', [
'pack' => $pack, 'pack' => $pack,
'services' => $this->formatServices(), 'services' => $this->formatServices(),
'files' => Storage::files('packs/' . $pack->uuid), 'files' => Storage::files('packs/' . $pack->uuid),
'service' => Models\Service::findOrFail($option->parent_service), 'service' => Models\Service::findOrFail($option->parent_service),
'option' => $option 'option' => $option,
]); ]);
} }
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
if (!is_null($request->input('action_delete'))) { if (! is_null($request->input('action_delete'))) {
try { try {
$repo = new Pack; $repo = new Pack;
$repo->delete($id); $repo->delete($id);
Alert::success('The requested service pack has been deleted from the system.')->flash(); Alert::success('The requested service pack has been deleted from the system.')->flash();
return redirect()->route('admin.services.packs'); return redirect()->route('admin.services.packs');
} catch (DisplayException $ex) { } catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash(); Alert::danger($ex->getMessage())->flash();
@ -150,12 +154,13 @@ class PackController extends Controller
Log::error($ex); Log::error($ex);
Alert::danger('An error occured while attempting to delete this pack.')->flash(); Alert::danger('An error occured while attempting to delete this pack.')->flash();
} }
return redirect()->route('admin.services.packs.edit', $id); return redirect()->route('admin.services.packs.edit', $id);
} else { } else {
try { try {
$repo = new Pack; $repo = new Pack;
$repo->update($id, $request->except([ $repo->update($id, $request->except([
'_token' '_token',
])); ]));
Alert::success('Service pack has been successfully updated.')->flash(); Alert::success('Service pack has been successfully updated.')->flash();
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
@ -164,6 +169,7 @@ class PackController extends Controller
Log::error($ex); Log::error($ex);
Alert::danger('An error occured while attempting to add edit this pack.')->flash(); Alert::danger('An error occured while attempting to add edit this pack.')->flash();
} }
return redirect()->route('admin.services.packs.edit', $id); return redirect()->route('admin.services.packs.edit', $id);
} }
} }
@ -183,14 +189,14 @@ class PackController extends Controller
'cpu' => $pack->build_cpu, 'cpu' => $pack->build_cpu,
'io' => $pack->build_io, 'io' => $pack->build_io,
'container' => $pack->build_container, 'container' => $pack->build_container,
'script' => $pack->build_script 'script' => $pack->build_script,
] ],
]; ];
$filename = tempnam(sys_get_temp_dir(), 'pterodactyl_'); $filename = tempnam(sys_get_temp_dir(), 'pterodactyl_');
if ((bool) $files) { if ((bool) $files) {
$zip = new \ZipArchive; $zip = new \ZipArchive;
if (!$zip->open($filename, \ZipArchive::CREATE)) { if (! $zip->open($filename, \ZipArchive::CREATE)) {
abort(503, 'Unable to open file for writing.'); abort(503, 'Unable to open file for writing.');
} }
@ -207,16 +213,18 @@ class PackController extends Controller
$fp = fopen($filename, 'a+'); $fp = fopen($filename, 'a+');
fwrite($fp, json_encode($json, JSON_PRETTY_PRINT)); fwrite($fp, json_encode($json, JSON_PRETTY_PRINT));
fclose($fp); fclose($fp);
return response()->download($filename, 'pack-' . $pack->name . '.json', [ return response()->download($filename, 'pack-' . $pack->name . '.json', [
'Content-Type' => 'application/json' 'Content-Type' => 'application/json',
])->deleteFileAfterSend(true); ])->deleteFileAfterSend(true);
} }
} }
public function uploadForm(Request $request, $for = null) { public function uploadForm(Request $request, $for = null)
{
return view('admin.services.packs.upload', [ return view('admin.services.packs.upload', [
'services' => $this->formatServices(), 'services' => $this->formatServices(),
'for' => $for 'for' => $for,
]); ]);
} }
@ -225,9 +233,10 @@ class PackController extends Controller
try { try {
$repo = new Pack; $repo = new Pack;
$id = $repo->createWithTemplate($request->except([ $id = $repo->createWithTemplate($request->except([
'_token' '_token',
])); ]));
Alert::success('Successfully created new service!')->flash(); Alert::success('Successfully created new service!')->flash();
return redirect()->route('admin.services.packs.edit', $id)->withInput(); return redirect()->route('admin.services.packs.edit', $id)->withInput();
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
return redirect()->back()->withErrors(json_decode($ex->getMessage()))->withInput(); return redirect()->back()->withErrors(json_decode($ex->getMessage()))->withInput();
@ -237,6 +246,7 @@ class PackController extends Controller
Log::error($ex); Log::error($ex);
Alert::danger('An error occured while attempting to add a new service pack.')->flash(); Alert::danger('An error occured while attempting to add a new service pack.')->flash();
} }
return redirect()->back(); return redirect()->back();
} }
} }

View file

@ -28,7 +28,6 @@ use DB;
use Log; use Log;
use Alert; use Alert;
use Storage; use Storage;
use Validator;
use Pterodactyl\Models; use Pterodactyl\Models;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
@ -292,12 +291,13 @@ class ServiceController extends Controller
public function getConfiguration(Request $request, $serviceId) public function getConfiguration(Request $request, $serviceId)
{ {
$service = Models\Service::findOrFail($serviceId); $service = Models\Service::findOrFail($serviceId);
return view('admin.services.config', [ return view('admin.services.config', [
'service' => $service, 'service' => $service,
'contents' => [ 'contents' => [
'json' => Storage::get('services/' . $service->file . '/main.json'), 'json' => Storage::get('services/' . $service->file . '/main.json'),
'index' => Storage::get('services/' . $service->file . '/index.js') 'index' => Storage::get('services/' . $service->file . '/index.js'),
] ],
]); ]);
} }
@ -306,17 +306,19 @@ class ServiceController extends Controller
try { try {
$repo = new ServiceRepository\Service; $repo = new ServiceRepository\Service;
$repo->updateFile($serviceId, $request->except([ $repo->updateFile($serviceId, $request->except([
'_token' '_token',
])); ]));
return response('', 204); return response('', 204);
} catch (DisplayException $ex) { } catch (DisplayException $ex) {
return response()->json([ return response()->json([
'error' => $ex->getMessage() 'error' => $ex->getMessage(),
], 503); ], 503);
} catch (\Exception $ex) { } catch (\Exception $ex) {
Log::error($ex); Log::error($ex);
return response()->json([ return response()->json([
'error' => 'An error occured while attempting to save the file.' 'error' => 'An error occured while attempting to save the file.',
], 503); ], 503);
} }
} }

View file

@ -148,7 +148,7 @@ class AdminRoutes
]); ]);
$router->post('/new/option-details', [ $router->post('/new/option-details', [
'uses' => 'Admin\ServersController@postNewServerOptionDetails' 'uses' => 'Admin\ServersController@postNewServerOptionDetails',
]); ]);
// End Assorted Page Helpers // End Assorted Page Helpers
@ -380,11 +380,11 @@ class AdminRoutes
$router->get('/service/{id}/configuration', [ $router->get('/service/{id}/configuration', [
'as' => 'admin.services.service.config', 'as' => 'admin.services.service.config',
'uses' => 'Admin\ServiceController@getConfiguration' 'uses' => 'Admin\ServiceController@getConfiguration',
]); ]);
$router->post('/service/{id}/configuration', [ $router->post('/service/{id}/configuration', [
'uses' => 'Admin\ServiceController@postConfiguration' 'uses' => 'Admin\ServiceController@postConfiguration',
]); ]);
$router->get('/service/{service}/option/new', [ $router->get('/service/{service}/option/new', [
@ -435,45 +435,45 @@ class AdminRoutes
'middleware' => [ 'middleware' => [
'auth', 'auth',
'admin', 'admin',
'csrf' 'csrf',
] ],
], function () use ($router) { ], function () use ($router) {
$router->get('/new/{option?}', [ $router->get('/new/{option?}', [
'as' => 'admin.services.packs.new', 'as' => 'admin.services.packs.new',
'uses' => 'Admin\PackController@new' 'uses' => 'Admin\PackController@new',
]); ]);
$router->post('/new', [ $router->post('/new', [
'uses' => 'Admin\PackController@create' 'uses' => 'Admin\PackController@create',
]); ]);
$router->get('/upload/{option?}', [ $router->get('/upload/{option?}', [
'as' => 'admin.services.packs.uploadForm', 'as' => 'admin.services.packs.uploadForm',
'uses' => 'Admin\PackController@uploadForm' 'uses' => 'Admin\PackController@uploadForm',
]); ]);
$router->post('/upload', [ $router->post('/upload', [
'uses' => 'Admin\PackController@postUpload' 'uses' => 'Admin\PackController@postUpload',
]); ]);
$router->get('/', [ $router->get('/', [
'as' => 'admin.services.packs', 'as' => 'admin.services.packs',
'uses' => 'Admin\PackController@listAll' 'uses' => 'Admin\PackController@listAll',
]); ]);
$router->get('/for/option/{option}', [ $router->get('/for/option/{option}', [
'as' => 'admin.services.packs.option', 'as' => 'admin.services.packs.option',
'uses' => 'Admin\PackController@listByOption' 'uses' => 'Admin\PackController@listByOption',
]); ]);
$router->get('/for/service/{service}', [ $router->get('/for/service/{service}', [
'as' => 'admin.services.packs.service', 'as' => 'admin.services.packs.service',
'uses' => 'Admin\PackController@listByService' 'uses' => 'Admin\PackController@listByService',
]); ]);
$router->get('/edit/{pack}', [ $router->get('/edit/{pack}', [
'as' => 'admin.services.packs.edit', 'as' => 'admin.services.packs.edit',
'uses' => 'Admin\PackController@edit' 'uses' => 'Admin\PackController@edit',
]); ]);
$router->post('/edit/{pack}', [ $router->post('/edit/{pack}', [
'uses' => 'Admin\PackController@update' 'uses' => 'Admin\PackController@update',
]); ]);
$router->get('/edit/{pack}/export/{archive?}', [ $router->get('/edit/{pack}/export/{archive?}', [
'as' => 'admin.services.packs.export', 'as' => 'admin.services.packs.export',
'uses' => 'Admin\PackController@export' 'uses' => 'Admin\PackController@export',
]); ]);
}); });
} }

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Pterodactyl - Panel * Pterodactyl - Panel
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com> * Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -21,13 +21,13 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Checksum extends Model class Checksum extends Model
{ {
/** /**
* The table associated with the model. * The table associated with the model.
* *
@ -48,7 +48,6 @@ class Checksum extends Model
* @var array * @var array
*/ */
protected $casts = [ protected $casts = [
'service' => 'integer' 'service' => 'integer',
]; ];
} }

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Pterodactyl - Panel * Pterodactyl - Panel
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com> * Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -21,13 +21,13 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class ServicePack extends Model class ServicePack extends Model
{ {
/** /**
* The table associated with the model. * The table associated with the model.
* *
@ -42,11 +42,11 @@ class ServicePack extends Model
*/ */
protected $guarded = ['id', 'created_at', 'updated_at']; protected $guarded = ['id', 'created_at', 'updated_at'];
/** /**
* Cast values to correct type. * Cast values to correct type.
* *
* @var array * @var array
*/ */
protected $casts = [ protected $casts = [
'option' => 'integer', 'option' => 'integer',
'build_memory' => 'integer', 'build_memory' => 'integer',
@ -54,7 +54,6 @@ class ServicePack extends Model
'build_cpu' => 'integer', 'build_cpu' => 'integer',
'build_io' => 'integer', 'build_io' => 'integer',
'selectable' => 'boolean', 'selectable' => 'boolean',
'visible' => 'boolean' 'visible' => 'boolean',
]; ];
} }

View file

@ -162,9 +162,9 @@ class ServerRepository
$data['pack'] = null; $data['pack'] = null;
} }
if (!is_null($data['pack'])) { if (! is_null($data['pack'])) {
$pack = Models\ServicePack::where('id', $data['pack'])->where('option', $data['option'])->first(); $pack = Models\ServicePack::where('id', $data['pack'])->where('option', $data['option'])->first();
if (!$pack) { if (! $pack) {
throw new DisplayException('The requested service pack does not seem to exist for this combination.'); throw new DisplayException('The requested service pack does not seem to exist for this combination.');
} }
} }

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Pterodactyl - Panel * Pterodactyl - Panel
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com> * Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -21,13 +21,13 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
namespace Pterodactyl\Repositories\ServiceRepository; namespace Pterodactyl\Repositories\ServiceRepository;
use DB; use DB;
use Storage;
use Uuid; use Uuid;
use Storage;
use Validator; use Validator;
use Pterodactyl\Models; use Pterodactyl\Models;
use Pterodactyl\Services\UuidService; use Pterodactyl\Services\UuidService;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
@ -35,7 +35,6 @@ use Pterodactyl\Exceptions\DisplayValidationException;
class Pack class Pack
{ {
public function __construct() public function __construct()
{ {
// //
@ -55,7 +54,7 @@ class Pack
'build_cpu' => 'required|integer|min:0', 'build_cpu' => 'required|integer|min:0',
'build_io' => 'required|integer|min:10|max:1000', 'build_io' => 'required|integer|min:10|max:1000',
'build_container' => 'required|string', 'build_container' => 'required|string',
'build_script' => 'sometimes|nullable|string' 'build_script' => 'sometimes|nullable|string',
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
@ -63,13 +62,13 @@ class Pack
} }
if (isset($data['file_upload'])) { if (isset($data['file_upload'])) {
if (!$data['file_upload']->isValid()) { if (! $data['file_upload']->isValid()) {
throw new DisplayException('The file provided does not appear to be valid.'); throw new DisplayException('The file provided does not appear to be valid.');
} }
if (!in_array($data['file_upload']->getMimeType(), [ if (! in_array($data['file_upload']->getMimeType(), [
'application/zip', 'application/zip',
'application/gzip' 'application/gzip',
])) { ])) {
throw new DisplayException('The file provided does not meet the required filetypes of application/zip or application/gzip.'); throw new DisplayException('The file provided does not meet the required filetypes of application/zip or application/gzip.');
} }
@ -91,7 +90,7 @@ class Pack
'version' => $data['version'], 'version' => $data['version'],
'description' => (empty($data['description'])) ? null : $data['description'], 'description' => (empty($data['description'])) ? null : $data['description'],
'selectable' => isset($data['selectable']), 'selectable' => isset($data['selectable']),
'visible' => isset($data['visible']) 'visible' => isset($data['visible']),
]); ]);
Storage::makeDirectory('packs/' . $pack->uuid); Storage::makeDirectory('packs/' . $pack->uuid);
@ -111,25 +110,25 @@ class Pack
public function createWithTemplate(array $data) public function createWithTemplate(array $data)
{ {
if (!isset($data['file_upload'])) { if (! isset($data['file_upload'])) {
throw new DisplayException('No template file was found submitted with this request.'); throw new DisplayException('No template file was found submitted with this request.');
} }
if (!$data['file_upload']->isValid()) { if (! $data['file_upload']->isValid()) {
throw new DisplayException('The file provided does not appear to be valid.'); throw new DisplayException('The file provided does not appear to be valid.');
} }
if (!in_array($data['file_upload']->getMimeType(), [ if (! in_array($data['file_upload']->getMimeType(), [
'application/zip', 'application/zip',
'text/plain', 'text/plain',
'application/json' 'application/json',
])) { ])) {
throw new DisplayException('The file provided (' . $data['file_upload']->getMimeType() . ') does not meet the required filetypes of application/zip or application/json.'); throw new DisplayException('The file provided (' . $data['file_upload']->getMimeType() . ') does not meet the required filetypes of application/zip or application/json.');
} }
if ($data['file_upload']->getMimeType() === 'application/zip') { if ($data['file_upload']->getMimeType() === 'application/zip') {
$zip = new \ZipArchive; $zip = new \ZipArchive;
if (!$zip->open($data['file_upload']->path())) { if (! $zip->open($data['file_upload']->path())) {
throw new DisplayException('The uploaded archive was unable to be opened.'); throw new DisplayException('The uploaded archive was unable to be opened.');
} }
@ -153,19 +152,21 @@ class Pack
'build_cpu' => $json->build->cpu, 'build_cpu' => $json->build->cpu,
'build_io' => $json->build->io, 'build_io' => $json->build->io,
'build_container' => $json->build->container, 'build_container' => $json->build->container,
'build_script' => $json->build->script 'build_script' => $json->build->script,
]); ]);
$pack = Models\ServicePack::findOrFail($id); $pack = Models\ServicePack::findOrFail($id);
if (!$zip->extractTo(storage_path('app/packs/' . $pack->uuid), ($isZip === false) ? 'archive.tar.gz' : 'archive.zip')) { if (! $zip->extractTo(storage_path('app/packs/' . $pack->uuid), ($isZip === false) ? 'archive.tar.gz' : 'archive.zip')) {
$pack->delete(); $pack->delete();
throw new DisplayException('Unable to extract the archive file to the correct location.'); throw new DisplayException('Unable to extract the archive file to the correct location.');
} }
$zip->close(); $zip->close();
return $pack->id; return $pack->id;
} else { } else {
$json = json_decode(file_get_contents($data['file_upload']->path())); $json = json_decode(file_get_contents($data['file_upload']->path()));
return $this->create([ return $this->create([
'name' => $json->name, 'name' => $json->name,
'version' => $json->version, 'version' => $json->version,
@ -178,10 +179,9 @@ class Pack
'build_cpu' => $json->build->cpu, 'build_cpu' => $json->build->cpu,
'build_io' => $json->build->io, 'build_io' => $json->build->io,
'build_container' => $json->build->container, 'build_container' => $json->build->container,
'build_script' => $json->build->script 'build_script' => $json->build->script,
]); ]);
} }
} }
public function update($id, array $data) public function update($id, array $data)
@ -198,7 +198,7 @@ class Pack
'build_cpu' => 'required|integer|min:0', 'build_cpu' => 'required|integer|min:0',
'build_io' => 'required|integer|min:10|max:1000', 'build_io' => 'required|integer|min:10|max:1000',
'build_container' => 'required|string', 'build_container' => 'required|string',
'build_script' => 'sometimes|string' 'build_script' => 'sometimes|string',
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
@ -218,14 +218,15 @@ class Pack
'version' => $data['version'], 'version' => $data['version'],
'description' => (empty($data['description'])) ? null : $data['description'], 'description' => (empty($data['description'])) ? null : $data['description'],
'selectable' => isset($data['selectable']), 'selectable' => isset($data['selectable']),
'visible' => isset($data['visible']) 'visible' => isset($data['visible']),
]); ]);
return true; return true;
}); });
} }
public function delete($id) { public function delete($id)
{
$pack = Models\ServicePack::findOrFail($id); $pack = Models\ServicePack::findOrFail($id);
// @TODO Check for linked servers; foreign key should block this. // @TODO Check for linked servers; foreign key should block this.
DB::transaction(function () use ($pack) { DB::transaction(function () use ($pack) {
@ -233,5 +234,4 @@ class Pack
Storage::deleteDirectory('packs/' . $pack->uuid); Storage::deleteDirectory('packs/' . $pack->uuid);
}); });
} }
} }

View file

@ -125,7 +125,7 @@ class Service
$validator = Validator::make($data, [ $validator = Validator::make($data, [
'file' => 'required|in:index,main', 'file' => 'required|in:index,main',
'contents' => 'required|string' 'contents' => 'required|string',
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
@ -139,10 +139,9 @@ class Service
try { try {
Storage::move($filepath, $backup); Storage::move($filepath, $backup);
Storage::put($filepath, $data['contents']); Storage::put($filepath, $data['contents']);
} catch(\Exception $ex) { } catch (\Exception $ex) {
Storage::move($backup, $filepath); Storage::move($backup, $filepath);
throw $ex; throw $ex;
} }
} }
} }