Fixing draw and drafting create

This commit is contained in:
Dennis Eichhorn 2016-11-09 21:14:16 +01:00
parent 3f6672eb42
commit 6bdc90c65e
7 changed files with 103 additions and 7 deletions

12
Admin/Routes/Web/Api.php Normal file
View File

@ -0,0 +1,12 @@
<?php
use phpOMS\Router\RouteVerb;
return [
'^.*/backend/draw.*$' => [
[
'dest' => '\Modules\Draw\Controller:apiDrawCreate',
'verb' => RouteVerb::SET,
],
],
];

View File

@ -1,5 +1,8 @@
(function (jsOMS) {
"use strict";
/** @namespace jsOMS.Uri.UriFactory */
jsOMS.Autoloader.defineNamespace('jsOMS.Modules.Draw');
jsOMS.Modules.Draw = function (app) {
this.app = app;

View File

@ -92,7 +92,9 @@ class Controller extends ModuleAbstract implements WebInterface
{
/** @var Head $head */
$head = $response->get('Content')->getData('head');
$head->addAsset(AssetType::JS, $request->getUri()->getBase() . 'Modules/Draw/ModuleDraw.js');
$head->addAsset(AssetType::JSLATE, $request->getUri()->getBase() . 'Modules/Draw/Controller.js');
$head->addAsset(AssetType::JSLATE, $request->getUri()->getBase() . 'Modules/Draw/Models/DrawType.enum.js');
$head->addAsset(AssetType::JSLATE, $request->getUri()->getBase() . 'Modules/Draw/Models/Editor.js');
}
/**
@ -133,4 +135,77 @@ class Controller extends ModuleAbstract implements WebInterface
return $view;
}
private function validateDrawCreate(RequestAbstract $request) : array
{
$val = [];
if (
($val['title'] = empty($request->getData('title')))
|| ($val['image'] = empty($request->getData('plain')))
|| ($val['status'] = (
$request->getData('status') === null
|| !NewsStatus::isValidValue((int) $request->getData('status'))
))
) {
return $val;
}
return [];
}
/**
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param mixed $data Generic data
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function apiDrawCreate(RequestAbstract $request, ResponseAbstract $response, $data = null)
{
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($request->getData('image') . $rnd);
$filename .= '.' . $extension;
$rnd = mt_rand();
} while (file_exists($path . '/' . $filename));
$fullPath = $path . '/' . $filename;
$this->createLocalFile($fullPath, $request->getData('image'));
$status = [
'path' => $path,
'filename' => $filename,
'size' => File::size($fullPath),
'extension' => $extension,
];
$media = MediaController::createDbEntries($status, $request->getAccount());
$draw = Draw::fromMedia(end($media));
DrawMapper::create($draw);
$response->set('image', $draw->jsonSerialize());
}
private function createLocalFile(string $outputPath, string $raw) : bool
{
$imageData = ImageUtils::decodeBase64Image($raw);
File::put($outputPath, $imageData);
return true;
}
}

View File

@ -10,6 +10,9 @@
(function (jsOMS)
{
"use strict";
/** @namespace jsOMS.Uri.UriFactory */
jsOMS.Autoloader.defineNamespace('jsOMS.Modules.Draw');
jsOMS.Modules.Draw.DrawTypeEnum = Object.freeze({
DRAW: 0,

View File

@ -1,6 +1,9 @@
(function (jsOMS)
{
"use strict";
/** @namespace jsOMS.Uri.UriFactory */
jsOMS.Autoloader.defineNamespace('jsOMS.Modules.Draw');
jsOMS.Modules.Draw.Editor = function (editor)
{

View File

@ -21,8 +21,8 @@ echo $this->getData('nav')->render(); ?>
<section class="box w-100">
<div class="inner">
<form>
<input type="text" class="wf-100">
<form id="drawForm" action="<?= \phpOMS\Uri\UriFactory::build('/{/lang}/api/draw?csrf={$CSRF}'); ?>" method="POST">
<input type="text" class="wf-100"><input type="submit" value="<?= $this->getText('Create', 0, 0); ?>">
</form>
</div>
</section>
@ -63,7 +63,7 @@ echo $this->getData('nav')->render(); ?>
<div class="m-draw">
<section class="box w-100" style="height: 30%;">
<div class="inner">
<canvas></canvas>
<canvas id="canvasImage" name="image" form="drawForm"></canvas>
</div>
</section>
</div>

View File

@ -21,8 +21,8 @@ echo $this->getData('nav')->render(); ?>
<section class="box w-100">
<div class="inner">
<form>
<input type="text" class="wf-100">
<form id="drawForm" action="<?= \phpOMS\Uri\UriFactory::build('/{/lang}/api/draw?csrf={$CSRF}'); ?>" method="POST">
<input type="text" class="wf-100"><input type="submit" value="<?= $this->getText('Create', 0, 0); ?>">
</form>
</div>
</section>
@ -63,7 +63,7 @@ echo $this->getData('nav')->render(); ?>
<div class="m-draw">
<section class="box w-100" style="height: 30%;">
<div class="inner">
<canvas></canvas>
<canvas id="canvasImage" name="image" form="drawForm"></canvas>
</div>
</section>
</div>