Begin moving packs to new service mechanisms, refactor exceptions for services

This commit is contained in:
Dane Everitt 2017-08-18 22:19:06 -05:00
parent 46cb71e69d
commit 9d3dca87f2
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
62 changed files with 492 additions and 303 deletions

View file

@ -22,20 +22,22 @@
* SOFTWARE.
*/
namespace Pterodactyl\Repositories\Eloquent\Attributes;
namespace Pterodactyl\Repositories\Concerns;
use Pterodactyl\Repositories\Eloquent\EloquentRepository;
use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
abstract class SearchableRepository extends EloquentRepository implements SearchableInterface
trait Searchable
{
/**
* The term to search.
*
* @var bool|string
*/
protected $searchTerm = false;
/**
* {@inheritdoc}
* Perform a search of the model using the given term.
*
* @param string $term
* @return $this
*/
public function search($term)
{

View file

@ -28,10 +28,12 @@ use Pterodactyl\Models\Location;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
use Pterodactyl\Repositories\Concerns\Searchable;
class LocationRepository extends SearchableRepository implements LocationRepositoryInterface
class LocationRepository extends EloquentRepository implements LocationRepositoryInterface
{
use Searchable;
/**
* @var string
*/

View file

@ -27,10 +27,12 @@ namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Node;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
use Pterodactyl\Repositories\Concerns\Searchable;
class NodeRepository extends SearchableRepository implements NodeRepositoryInterface
class NodeRepository extends EloquentRepository implements NodeRepositoryInterface
{
use Searchable;
/**
* {@inheritdoc}
*/

View file

@ -1,5 +1,5 @@
<?php
/**
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
@ -22,52 +22,44 @@
* SOFTWARE.
*/
namespace Pterodactyl\Repositories;
namespace Pterodactyl\Repositories\Eloquent;
class HelperRepository
use Pterodactyl\Models\Pack;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Pterodactyl\Repositories\Concerns\Searchable;
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
class PackRepository extends EloquentRepository implements PackRepositoryInterface
{
/**
* Listing of editable files in the control panel.
*
* @var array
*/
protected static $editable = [
'application/json',
'application/javascript',
'application/xml',
'application/xhtml+xml',
'text/xml',
'text/css',
'text/html',
'text/plain',
'text/x-perl',
'text/x-shellscript',
'inode/x-empty',
];
use Searchable;
/**
* Converts from bytes to the largest possible size that is still readable.
*
* @param int $bytes
* @param int $decimals
* @return string
* @deprecated
* {@inheritdoc}
*/
public static function bytesToHuman($bytes, $decimals = 2)
public function model()
{
$sz = explode(',', 'B,KB,MB,GB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . $sz[$factor];
return Pack::class;
}
/**
* Returns array of editable files.
*
* @return array
* {@inheritdoc}
*/
public static function editableFiles()
public function getFileArchives($id, $collection = false)
{
return self::$editable;
$pack = $this->getBuilder()->find($id, ['id', 'uuid']);
$storage = $this->app->make(FilesystemFactory::class);
$files = collect($storage->disk('default')->files('packs/' . $pack->uuid));
$files = $files->map(function ($file) {
$path = storage_path('app/' . $file);
return (object) [
'name' => basename($file),
'hash' => sha1_file($path),
'size' => human_readable($path),
];
});
return ($collection) ? $files : (object) $files->all();
}
}

View file

@ -25,12 +25,14 @@
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Server;
use Pterodactyl\Repositories\Concerns\Searchable;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
class ServerRepository extends SearchableRepository implements ServerRepositoryInterface
class ServerRepository extends EloquentRepository implements ServerRepositoryInterface
{
use Searchable;
/**
* {@inheritdoc}
*/

View file

@ -26,12 +26,14 @@ namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\User;
use Illuminate\Foundation\Application;
use Pterodactyl\Repositories\Concerns\Searchable;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
class UserRepository extends SearchableRepository implements UserRepositoryInterface
class UserRepository extends EloquentRepository implements UserRepositoryInterface
{
use Searchable;
/**
* @var \Illuminate\Contracts\Config\Repository
*/

View file

@ -1,163 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Repositories;
use Cron;
use Validator;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
class TaskRepository
{
/**
* The default values to use for new tasks.
*
* @var array
*/
protected $defaults = [
'year' => '*',
'day_of_week' => '*',
'month' => '*',
'day_of_month' => '*',
'hour' => '*',
'minute' => '*/30',
];
/**
* Task action types.
*
* @var array
*/
protected $actions = [
'command',
'power',
];
/**
* Deletes a given task.
*
* @param int $id
* @return bool
*/
public function delete($id)
{
$task = Task::findOrFail($id);
$task->delete();
}
/**
* Toggles a task active or inactive.
*
* @param int $id
* @return bool
*/
public function toggle($id)
{
$task = Task::findOrFail($id);
$task->active = ! $task->active;
$task->queued = false;
$task->save();
return $task->active;
}
/**
* Create a new scheduled task for a given server.
*
* @param int $server
* @param int $user
* @param array $data
* @return \Pterodactyl\Models\Task
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function create($server, $user, $data)
{
$server = Server::findOrFail($server);
$user = User::findOrFail($user);
$validator = Validator::make($data, [
'action' => 'string|required',
'data' => 'string|required',
'year' => 'string|sometimes',
'day_of_week' => 'string|sometimes',
'month' => 'string|sometimes',
'day_of_month' => 'string|sometimes',
'hour' => 'string|sometimes',
'minute' => 'string|sometimes',
]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}
if (! in_array($data['action'], $this->actions)) {
throw new DisplayException('The action provided is not valid.');
}
$cron = $this->defaults;
foreach ($this->defaults as $setting => $value) {
if (array_key_exists($setting, $data) && ! is_null($data[$setting]) && $data[$setting] !== '') {
$cron[$setting] = $data[$setting];
}
}
// Check that is this a valid Cron Entry
try {
$buildCron = Cron::factory(sprintf('%s %s %s %s %s %s',
$cron['minute'],
$cron['hour'],
$cron['day_of_month'],
$cron['month'],
$cron['day_of_week'],
$cron['year']
));
} catch (\Exception $ex) {
throw $ex;
}
return Task::create([
'user_id' => $user->id,
'server_id' => $server->id,
'active' => 1,
'action' => $data['action'],
'data' => $data['data'],
'queued' => 0,
'year' => $cron['year'],
'day_of_week' => $cron['day_of_week'],
'month' => $cron['month'],
'day_of_month' => $cron['day_of_month'],
'hour' => $cron['hour'],
'minute' => $cron['minute'],
'last_run' => null,
'next_run' => $buildCron->getNextRunDate(),
]);
}
}