From a0dd5b4acab724f5dca4ac3b2b225e71614d2616 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 19 Sep 2018 21:34:04 +0200 Subject: [PATCH] Split controllers per application --- Admin/Routes/Web/Api.php | 6 +- Admin/Routes/Web/Backend.php | 22 +-- Controller/ApiController.php | 137 +++++++++++++++++ .../BackendController.php | 145 +----------------- Controller/Controller.php | 94 ++++++++++++ 5 files changed, 247 insertions(+), 157 deletions(-) create mode 100644 Controller/ApiController.php rename Controller.php => Controller/BackendController.php (57%) create mode 100644 Controller/Controller.php diff --git a/Admin/Routes/Web/Api.php b/Admin/Routes/Web/Api.php index d443ab0..aa47d07 100644 --- a/Admin/Routes/Web/Api.php +++ b/Admin/Routes/Web/Api.php @@ -3,15 +3,15 @@ use phpOMS\Router\RouteVerb; use phpOMS\Account\PermissionType; use Modules\Draw\Models\PermissionState; -use Modules\Draw\Controller; +use Modules\Draw\Controller\ApiController; return [ '^.*/api/draw.*$' => [ [ - 'dest' => '\Modules\Draw\Controller:apiDrawCreate', + 'dest' => '\Modules\Draw\ControllerApiController:apiDrawCreate', 'verb' => RouteVerb::SET, 'permission' => [ - 'module' => Controller::MODULE_NAME, + 'module' => ApiController::MODULE_NAME, 'type' => PermissionType::CREATE, 'state' => PermissionState::DRAW, ], diff --git a/Admin/Routes/Web/Backend.php b/Admin/Routes/Web/Backend.php index ea7529d..6ecf84a 100644 --- a/Admin/Routes/Web/Backend.php +++ b/Admin/Routes/Web/Backend.php @@ -3,24 +3,24 @@ use phpOMS\Router\RouteVerb; use phpOMS\Account\PermissionType; use Modules\Draw\Models\PermissionState; -use Modules\Draw\Controller; +use Modules\Draw\Controller\BackendController; return [ '^.*/backend/draw/create.*$' => [ [ - 'dest' => '\Modules\Draw\Controller:setUpDrawEditor', + 'dest' => '\Modules\Draw\Controller\BackendController:setUpDrawEditor', 'verb' => RouteVerb::GET, 'permission' => [ - 'module' => Controller::MODULE_NAME, + 'module' => BackendController::MODULE_NAME, 'type' => PermissionType::CREATE, 'state' => PermissionState::DRAW, ], ], [ - 'dest' => '\Modules\Draw\Controller:viewDrawCreate', + 'dest' => '\Modules\Draw\Controller\BackendController:viewDrawCreate', 'verb' => RouteVerb::GET, 'permission' => [ - 'module' => Controller::MODULE_NAME, + 'module' => BackendController::MODULE_NAME, 'type' => PermissionType::CREATE, 'state' => PermissionState::DRAW, ], @@ -28,10 +28,10 @@ return [ ], '^.*/backend/draw/list.*$' => [ [ - 'dest' => '\Modules\Draw\Controller:viewDrawList', + 'dest' => '\Modules\Draw\Controller\BackendController:viewDrawList', 'verb' => RouteVerb::GET, 'permission' => [ - 'module' => Controller::MODULE_NAME, + 'module' => BackendController::MODULE_NAME, 'type' => PermissionType::READ, 'state' => PermissionState::DRAW, ], @@ -39,19 +39,19 @@ return [ ], '^.*/backend/draw/single.*$' => [ [ - 'dest' => '\Modules\Draw\Controller:setUpDrawEditor', + 'dest' => '\Modules\Draw\Controller\BackendController:setUpDrawEditor', 'verb' => RouteVerb::GET, 'permission' => [ - 'module' => Controller::MODULE_NAME, + 'module' => BackendController::MODULE_NAME, 'type' => PermissionType::READ, 'state' => PermissionState::DRAW, ], ], [ - 'dest' => '\Modules\Draw\Controller:viewDrawSingle', + 'dest' => '\Modules\Draw\Controller\BackendController:viewDrawSingle', 'verb' => RouteVerb::GET, 'permission' => [ - 'module' => Controller::MODULE_NAME, + 'module' => BackendController::MODULE_NAME, 'type' => PermissionType::READ, 'state' => PermissionState::DRAW, ], diff --git a/Controller/ApiController.php b/Controller/ApiController.php new file mode 100644 index 0000000..b08026f --- /dev/null +++ b/Controller/ApiController.php @@ -0,0 +1,137 @@ + + * + * @since 1.0.0 + */ + private function validateDrawCreate(RequestAbstract $request) : array + { + $val = []; + if (($val['title'] = empty($request->getData('title'))) + || ($val['image'] = empty($request->getData('image'))) + ) { + return $val; + } + + return []; + } + + /** + * @param RequestAbstract $request Request + * @param ResponseAbstract $response Response + * @param mixed $data Generic data + * + * @return void + * + * @api + * + * @since 1.0.0 + */ + public function apiDrawCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void + { + if (!empty($val = $this->validateDrawCreate($request))) { + $response->set('draw_create', new FormValidation($val)); + + return; + } + + $path = MediaController::createMediaPath(); + $extension = 'png'; + $filename = ''; + $rnd = ''; + + // todo: implement limit since this could get exploited + do { + $filename = sha1(((string) $request->getData('image')) . $rnd); + $filename .= '.' . $extension; + + $rnd = mt_rand(); + } while (file_exists($path . '/' . $filename)); + + $fullPath = __DIR__ . '/../../' . $path . '/' . $filename; + + $this->createLocalFile($fullPath, (string) $request->getData('image')); + + $status = [ + 'path' => $path, + 'filename' => $filename, + 'name' => (string) $request->getData('title'), + 'size' => File::size($fullPath), + 'extension' => $extension, + 'status' => UploadStatus::OK, + ]; + + $media = MediaController::createDbEntry($status, $request->getHeader()->getAccount()); + $draw = DrawImage::fromMedia($media); + + DrawImageMapper::create($draw); + + $response->set('image', $draw->jsonSerialize()); + } + + /** + * Create local image file + * + * @param string $outputPath Output path + * @param string $raw Base64 encoded image string + * + * @return bool + * + * @since 1.0.0 + */ + private function createLocalFile(string $outputPath, string $raw) : bool + { + $imageData = ImageUtils::decodeBase64Image($raw); + File::put($outputPath, $imageData); + + return true; + } +} diff --git a/Controller.php b/Controller/BackendController.php similarity index 57% rename from Controller.php rename to Controller/BackendController.php index 4b2e0d2..08fd722 100644 --- a/Controller.php +++ b/Controller/BackendController.php @@ -12,7 +12,7 @@ */ declare(strict_types=1); -namespace Modules\Draw; +namespace Modules\Draw\Controller; use Model\Message\FormValidation; use Modules\Draw\Models\DrawImage; @@ -40,58 +40,9 @@ use phpOMS\Message\Http\RequestStatusCode; * @link http://website.orange-management.de * @since 1.0.0 */ -final class Controller extends ModuleAbstract implements WebInterface +class BackendController extends Controller { - /** - * Module path. - * - * @var string - * @since 1.0.0 - */ - public const MODULE_PATH = __DIR__; - - /** - * Module version. - * - * @var string - * @since 1.0.0 - */ - public const MODULE_VERSION = '1.0.0'; - - /** - * Module name. - * - * @var string - * @since 1.0.0 - */ - public const MODULE_NAME = 'Draw'; - - /** - * Module id. - * - * @var int - * @since 1.0.0 - */ - public const MODULE_ID = 1005200000; - - /** - * Providing. - * - * @var string[] - * @since 1.0.0 - */ - protected static $providing = []; - - /** - * Dependencies. - * - * @var string[] - * @since 1.0.0 - */ - protected static $dependencies = [ - ]; - /** * @param RequestAbstract $request Request * @param ResponseAbstract $response Response @@ -187,96 +138,4 @@ final class Controller extends ModuleAbstract implements WebInterface return $view; } - - /** - * Validate draw create request - * - * @param RequestAbstract $request Request - * - * @return array - * - * @since 1.0.0 - */ - private function validateDrawCreate(RequestAbstract $request) : array - { - $val = []; - if (($val['title'] = empty($request->getData('title'))) - || ($val['image'] = empty($request->getData('image'))) - ) { - return $val; - } - - return []; - } - - /** - * @param RequestAbstract $request Request - * @param ResponseAbstract $response Response - * @param mixed $data Generic data - * - * @return void - * - * @api - * - * @since 1.0.0 - */ - public function apiDrawCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void - { - if (!empty($val = $this->validateDrawCreate($request))) { - $response->set('draw_create', new FormValidation($val)); - - return; - } - - $path = MediaController::createMediaPath(); - $extension = 'png'; - $filename = ''; - $rnd = ''; - - // todo: implement limit since this could get exploited - do { - $filename = sha1(((string) $request->getData('image')) . $rnd); - $filename .= '.' . $extension; - - $rnd = mt_rand(); - } while (file_exists($path . '/' . $filename)); - - $fullPath = __DIR__ . '/../../' . $path . '/' . $filename; - - $this->createLocalFile($fullPath, (string) $request->getData('image')); - - $status = [ - 'path' => $path, - 'filename' => $filename, - 'name' => (string) $request->getData('title'), - 'size' => File::size($fullPath), - 'extension' => $extension, - 'status' => UploadStatus::OK, - ]; - - $media = MediaController::createDbEntry($status, $request->getHeader()->getAccount()); - $draw = DrawImage::fromMedia($media); - - DrawImageMapper::create($draw); - - $response->set('image', $draw->jsonSerialize()); - } - - /** - * Create local image file - * - * @param string $outputPath Output path - * @param string $raw Base64 encoded image string - * - * @return bool - * - * @since 1.0.0 - */ - private function createLocalFile(string $outputPath, string $raw) : bool - { - $imageData = ImageUtils::decodeBase64Image($raw); - File::put($outputPath, $imageData); - - return true; - } } diff --git a/Controller/Controller.php b/Controller/Controller.php new file mode 100644 index 0000000..437d7df --- /dev/null +++ b/Controller/Controller.php @@ -0,0 +1,94 @@ +