Merge remote-tracking branch 'origin/develop' into develop

# Conflicts:
#	Draw/Controller.js
#	Draw/Models/DrawType.enum.js
#	Draw/Models/Editor.js
#	Media/Models/UploadFile.php
This commit is contained in:
Dennis Eichhorn 2017-01-26 16:42:29 +01:00
commit 4a95f90207
33 changed files with 189 additions and 103 deletions

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -16,7 +16,7 @@
namespace Modules\Media\Admin;
use phpOMS\DataStorage\Database\Pool;
use phpOMS\DataStorage\Database\DatabasePool;
use phpOMS\Module\ActivateAbstract;
use phpOMS\Module\InfoManager;
@ -37,7 +37,7 @@ class Activate extends ActivateAbstract
/**
* {@inheritdoc}
*/
public static function activate(Pool $dbPool, InfoManager $info)
public static function activate(DatabasePool $dbPool, InfoManager $info)
{
parent::activate($dbPool, $info);
}

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -16,7 +16,7 @@
namespace Modules\Media\Admin;
use phpOMS\DataStorage\Database\Pool;
use phpOMS\DataStorage\Database\DatabasePool;
use phpOMS\Module\DeactivateAbstract;
use phpOMS\Module\InfoManager;
@ -37,7 +37,7 @@ class Deactivate extends DeactivateAbstract
/**
* {@inheritdoc}
*/
public static function deactivate(Pool $dbPool, InfoManager $info)
public static function deactivate(DatabasePool $dbPool, InfoManager $info)
{
parent::deactivate($dbPool, $info);
}

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -14,7 +14,7 @@
* @link http://orange-management.com
*/
namespace Modules\Media\Admin\Install;
use phpOMS\DataStorage\Database\Pool;
use phpOMS\DataStorage\Database\DatabasePool;
/**
* Navigation class.
@ -29,7 +29,7 @@ use phpOMS\DataStorage\Database\Pool;
*/
class Navigation
{
public static function install(Pool $dbPool)
public static function install(string $path, DatabasePool $dbPool)
{
$navData = json_decode(file_get_contents(__DIR__ . '/Navigation.install.json'), true);

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -16,7 +16,7 @@
namespace Modules\Media\Admin;
use phpOMS\DataStorage\Database\DatabaseType;
use phpOMS\DataStorage\Database\Pool;
use phpOMS\DataStorage\Database\DatabasePool;
use phpOMS\Module\InfoManager;
use phpOMS\Module\InstallerAbstract;
@ -37,9 +37,9 @@ class Installer extends InstallerAbstract
/**
* {@inheritdoc}
*/
public static function install(Pool $dbPool, InfoManager $info)
public static function install(string $path, DatabasePool $dbPool, InfoManager $info)
{
parent::install($dbPool, $info);
parent::install($path, $dbPool, $info);
switch ($dbPool->get('core')->getType()) {
case DatabaseType::MYSQL:

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

@ -0,0 +1,24 @@
<?php
use phpOMS\Router\RouteVerb;
return [
'^.*/api/media/collection.*$' => [
[
'dest' => '\Modules\Media\Controller:apiCollectionCreate',
'verb' => RouteVerb::SET,
],
],
'^.*/api/media$' => [
[
'dest' => '\Modules\Media\Controller:apiMediaUpload',
'verb' => RouteVerb::SET,
],
],
'^.*/api/media/create.*$' => [
[
'dest' => '\Modules\Media\Controller:apiMediaCreate',
'verb' => RouteVerb::SET,
],
],
];

View File

@ -25,22 +25,4 @@ return [
'verb' => RouteVerb::GET,
],
],
'^.*/api/media/collection.*$' => [
[
'dest' => '\Modules\Media\Controller:apiCollectionCreate',
'verb' => RouteVerb::SET,
],
],
'^.*/api/media$' => [
[
'dest' => '\Modules\Media\Controller:apiMediaUpload',
'verb' => RouteVerb::SET,
],
],
'^.*/api/media/create.*$' => [
[
'dest' => '\Modules\Media\Controller:apiMediaCreate',
'verb' => RouteVerb::SET,
],
],
];

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -16,7 +16,7 @@
namespace Modules\Media\Admin;
use phpOMS\DataStorage\Database\Pool;
use phpOMS\DataStorage\Database\DatabasePool;
use phpOMS\DataStorage\Database\Schema\Builder;
use phpOMS\Module\UninstallAbstract;
@ -37,7 +37,7 @@ class Uninstall extends UninstallAbstract
/**
* {@inheritdoc}
*/
public static function uninstall(Pool $dbPool, InfoManager $info)
public static function uninstall(DatabasePool $dbPool, InfoManager $info)
{
parent::uninstall($dbPool, $info);

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -16,7 +16,7 @@
namespace Modules\Media\Admin;
use phpOMS\DataStorage\Database\Pool;
use phpOMS\DataStorage\Database\DatabasePool;
use phpOMS\Module\UpdateAbstract;
use phpOMS\System\File\Directory;
@ -37,7 +37,7 @@ class Update extends UpdateAbstract
/**
* {@inheritdoc}
*/
public static function update(Pool $dbPool, array $info)
public static function update(DatabasePool $dbPool, array $info)
{
Directory::deletePath(__DIR__ . '/Update');
mkdir('Update');

View File

@ -40,7 +40,7 @@
uploader.setSuccess(e.id, function (type, response)
{
e.querySelector('input[type=file]+input[type=hidden]').value = JSON.stringify(response.uploads);
self.app.eventManager.trigger(requestId, requestGroup);
self.app.eventManager.trigger(requestGroup, requestId);
});
uploader.setUri(Url + '{/lang}/api/media');

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -48,7 +48,7 @@ class Controller extends ModuleAbstract implements WebInterface
* @var string
* @since 1.0.0
*/
const MODULE_PATH = __DIR__;
/* public */ const MODULE_PATH = __DIR__;
/**
* Module version.
@ -56,7 +56,7 @@ class Controller extends ModuleAbstract implements WebInterface
* @var string
* @since 1.0.0
*/
const MODULE_VERSION = '1.0.0';
/* public */ const MODULE_VERSION = '1.0.0';
/**
* Module name.
@ -64,7 +64,7 @@ class Controller extends ModuleAbstract implements WebInterface
* @var string
* @since 1.0.0
*/
const MODULE_NAME = 'Media';
/* public */ const MODULE_NAME = 'Media';
/**
* Providing.
@ -117,6 +117,9 @@ class Controller extends ModuleAbstract implements WebInterface
$view->setTemplate('/Modules/Media/Theme/Backend/media-list');
$view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1000401001, $request, $response));
$media = MediaMapper::getNewest(25);
$view->addData('media', $media);
return $view;
}
@ -176,8 +179,13 @@ class Controller extends ModuleAbstract implements WebInterface
{
$uploads = $this->uploadFiles($request->getFiles(), $request->getAccount(), $request->getData('path') ?? '/Modules/Media/Files');
$ids = [];
foreach($uploads as $file) {
$ids[] = $file->getId();
}
$response->getHeader()->set('Content-Type', MimeType::M_JSON . '; charset=utf-8', true);
$response->set($request->__toString(), [['uploads' => $uploads, 'type' => 'UI']]);
$response->set($request->__toString(), [['uploads' => $ids, 'type' => 'UI']]);
}
/**
@ -207,24 +215,29 @@ class Controller extends ModuleAbstract implements WebInterface
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function uploadFiles(array $files, int $account, string $basePath = '/Modules/Media/Files') : array
public function uploadFiles(array $files, int $account, string $basePath = 'Modules/Media/Files') : array
{
$mediaCreated = [];
if (!empty($files)) {
$upload = new UploadFile();
$rndPath = str_pad(dechex(rand(0, 65535)), 4, '0', STR_PAD_LEFT);
$path = '/' . trim($basePath, '/\\.') . '/' . $rndPath[0] . $rndPath[1] . '/' . $rndPath[2] . $rndPath[3];
$path = self::createMediaPath($basePath);
$upload->setOutputDir($path);
$upload->setFileName(false);
$status = $upload->upload($files);
$mediaCreated = $this->createDbEntries($status, $account);
$mediaCreated = self::createDbEntries($status, $account);
}
return $mediaCreated;
}
public static function createMediaPath(string $basePath = 'Modules/Media/Files') : string
{
$rndPath = str_pad(dechex(rand(0, 65535)), 4, '0', STR_PAD_LEFT);
return $basePath . '/' . $rndPath[0] . $rndPath[1] . '/' . $rndPath[2] . $rndPath[3];
}
/**
* @param array $status Files
* @param int $account Uploader
@ -234,25 +247,36 @@ class Controller extends ModuleAbstract implements WebInterface
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function createDbEntries(array $status, int $account) : array
public static function createDbEntries(array $status, int $account) : array
{
$mediaCreated = [];
foreach ($status as $uFile) {
if ($uFile['status'] === UploadStatus::OK) {
$media = new Media();
$media->setPath(trim($uFile['path'], '/') . '/' . $uFile['filename']);
$media->setName($uFile['filename']);
$media->setSize($uFile['size']);
$media->setCreatedBy($account);
$media->setCreatedAt(new \DateTime('NOW'));
$media->setExtension($uFile['extension']);
$mediaCreated[] = MediaMapper::create($media);
if(!is_null($created = self::createDbEntry($uFile, $account))) {
$mediaCreated[] = $created;
}
}
return $mediaCreated;
}
public static function createDbEntry(array $status, int $account)
{
$media = null;
if ($status['status'] === UploadStatus::OK) {
$media = new Media();
$media->setPath(trim($status['path'], '/') . '/' . $status['filename']);
$media->setName($status['name']);
$media->setSize($status['size']);
$media->setCreatedBy($account);
$media->setCreatedAt(new \DateTime('NOW'));
$media->setExtension($status['extension']);
MediaMapper::create($media);
}
return $media;
}
}

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -15,7 +15,6 @@
*/
namespace Modules\Media\Models;
/**
* Media class.
*

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -15,6 +15,7 @@
*/
namespace Modules\Media\Models;
use Modules\Media\Models\MediaMapper;
use phpOMS\DataStorage\Database\Query\Builder;
use phpOMS\DataStorage\Database\Query\Column;
@ -22,8 +23,7 @@ class CollectionMapper extends MediaMapper
{
protected static $hasMany = [
'sources' => [
'mapper' => \Modules\Media\Models\MediaMapper::class, /* mapper of the related object */
'relationmapper' => null, /* if the relation itself is a more complex object that has it's own mapper */
'mapper' => MediaMapper::class, /* mapper of the related object */
'table' => 'media_relation', /* table of the related object, null if no relation table is used (many->1) */
'dst' => 'media_relation_dst',
'src' => 'media_relation_src',

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -330,4 +330,9 @@ class Media
{
$this->versioned = $versioned;
}
public function toArray()
{
return [];
}
}

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -26,7 +26,7 @@ class MediaMapper extends DataMapperAbstract
/**
* Columns.
*
* @var array<string, array>
* @var array
* @since 1.0.0
*/
protected static $columns = [

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -15,7 +15,7 @@
*/
namespace Modules\Media\Models;
use phpOMS\System\File\Directory;
use phpOMS\System\File\Local\Directory;
/**
@ -31,6 +31,15 @@ use phpOMS\System\File\Directory;
*/
class UploadFile
{
/* public */ const PATH_GENERATION_LIMIT = 1000;
/**
* Image interlaced.
*
* @var int
* @since 1.0.0
*/
private $interlaced = true;
/**
* Upload max size.
@ -54,7 +63,7 @@ class UploadFile
* @var string
* @since 1.0.0
*/
private $outputDir = '/Modules/Media/Files';
private $outputDir = 'Modules/Media/Files';
/**
* Output file name.
@ -79,6 +88,8 @@ class UploadFile
*
* @return array
*
* @throws \Exception
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
@ -130,6 +141,7 @@ class UploadFile
}
$split = explode('.', $f['name']);
$result[$key]['name'] = $split[0];
$extension = count($split) > 1 ? $split[count($split) - 1] : '';
$result[$key]['extension'] = $extension;
@ -138,9 +150,9 @@ class UploadFile
if (!$this->fileName || empty($this->fileName) || file_exists($path . '/' . $this->fileName)) {
$rnd = '';
$limit = 0;
do {
$sha = sha1_file($f['tmp_name'] . $rnd);
$sha .= '.' . $extension;
if ($sha === false) {
$result[$key]['status'] = UploadStatus::FAILED_HASHING;
@ -148,13 +160,20 @@ class UploadFile
return $result;
}
$sha .= '.' . $extension;
$this->fileName = $sha;
$rnd = mt_rand();
} while (file_exists($path . '/' . $this->fileName));
$limit++;
} while (file_exists($path . '/' . $this->fileName) && $limit < self::PATH_GENERATION_LIMIT);
if($limit >= self::PATH_GENERATION_LIMIT) {
throw new \Exception('No file path could be found. Potential attack!');
}
}
if (!is_dir($path)) {
Directory::create($path, '0655', true);
Directory::create($path, 0655, true);
}
if (!is_uploaded_file($f['tmp_name'])) {
@ -163,18 +182,46 @@ class UploadFile
return $result;
}
if (!move_uploaded_file($f['tmp_name'], $path . '/' . $this->fileName)) {
if (!move_uploaded_file($f['tmp_name'], $dest = $path . '/' . $this->fileName)) {
$result[$key]['status'] = UploadStatus::NOT_MOVABLE;
return $result;
}
if($this->interlaced && in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) {
$this->interlace($extension, $dest);
}
$result[$key]['path'] = $this->outputDir;
}
return $result;
}
private function interlace(string $extension, string $path) /* : void */
{
if($extension === 'png') {
$img = imagecreatefrompng($path);
} elseif($extension === 'jpg' || $extension === 'jpeg') {
$img = imagecreatefromjpeg($path);
} else {
$img = imagecreatefromgif($path);
}
imageinterlace($img, (int) $this->interlaced);
if($extension === 'png') {
imagepng($img, $path);
} elseif($extension === 'jpg' || $extension === 'jpeg') {
imagejpeg($img, $path);
} else {
imagegif($img, $path);
}
imagedestroy($img);
}
/**
* Find unique output path for batch of files
*
@ -231,6 +278,11 @@ class UploadFile
return $this->maxSize;
}
public function setInterlaced(bool $interlaced) /* : void */
{
$this->interlaced = $interlaced;
}
/**
* @param int $maxSize
*

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -30,14 +30,14 @@ use phpOMS\Datatypes\Enum;
*/
abstract class UploadStatus extends Enum
{
const OK = 0;
const WRONG_PARAMETERS = -1;
const NOTHING_UPLOADED = -2;
const UPLOAD_SIZE = -3;
const UNKNOWN_ERROR = -4;
const CONFIG_SIZE = -5;
const WRONG_EXTENSION = -6;
const NOT_UPLOADED = -7;
const NOT_MOVABLE = -8;
const FAILED_HASHING = -9;
/* public */ const OK = 0;
/* public */ const WRONG_PARAMETERS = -1;
/* public */ const NOTHING_UPLOADED = -2;
/* public */ const UPLOAD_SIZE = -3;
/* public */ const UNKNOWN_ERROR = -4;
/* public */ const CONFIG_SIZE = -5;
/* public */ const WRONG_EXTENSION = -6;
/* public */ const NOT_UPLOADED = -7;
/* public */ const NOT_MOVABLE = -8;
/* public */ const FAILED_HASHING = -9;
}

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -17,7 +17,7 @@
* @var \phpOMS\Views\View $this
*/
$media = \Modules\Media\Models\MediaMapper::getNewest(25);
$media = $this->getData('media');
$footerView = new \Web\Views\Lists\PaginationView($this->app, $this->request, $this->response);
$footerView->setTemplate('/Web/Templates/Lists/Footer/PaginationBig');
$footerView->setPages(count($media) / 25);

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD
@ -17,7 +17,7 @@
* @var \phpOMS\Views\View $this
*/
$media = \Modules\Media\Models\MediaMapper::getNewest(25);
$media = $this->getData('media');
$footerView = new \Web\Views\Lists\PaginationView($this->app, $this->request, $this->response);
$footerView->setTemplate('/Web/Templates/Lists/Footer/PaginationBig');
$footerView->setPages(count($media) / 25);

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD

View File

@ -2,7 +2,7 @@
/**
* Orange Management
*
* PHP Version 7.0
* PHP Version 7.1
*
* @category TBD
* @package TBD