mirror of
https://github.com/Karaka-Management/oms-Media.git
synced 2026-02-15 16:58:42 +00:00
Fixing comments and parameter for orm
This commit is contained in:
parent
24a3a2fac2
commit
7dcaa5183f
|
|
@ -22,6 +22,7 @@ use Modules\Media\Models\UploadStatus;
|
||||||
use phpOMS\Asset\AssetType;
|
use phpOMS\Asset\AssetType;
|
||||||
use phpOMS\Message\RequestAbstract;
|
use phpOMS\Message\RequestAbstract;
|
||||||
use phpOMS\Message\ResponseAbstract;
|
use phpOMS\Message\ResponseAbstract;
|
||||||
|
use phpOMS\Model\Html\Head;
|
||||||
use phpOMS\Module\ModuleAbstract;
|
use phpOMS\Module\ModuleAbstract;
|
||||||
use phpOMS\Module\WebInterface;
|
use phpOMS\Module\WebInterface;
|
||||||
use phpOMS\Views\View;
|
use phpOMS\Views\View;
|
||||||
|
|
@ -93,6 +94,7 @@ class Controller extends ModuleAbstract implements WebInterface
|
||||||
*/
|
*/
|
||||||
public static function setUpFileUploader(RequestAbstract $request, ResponseAbstract $response, $data = null)
|
public static function setUpFileUploader(RequestAbstract $request, ResponseAbstract $response, $data = null)
|
||||||
{
|
{
|
||||||
|
/** @var Head $head */
|
||||||
$head = $response->get('Content')->getData('head');
|
$head = $response->get('Content')->getData('head');
|
||||||
$head->addAsset(AssetType::JS, $request->getUri()->getBase() . 'Modules/Media/Models/Upload.js');
|
$head->addAsset(AssetType::JS, $request->getUri()->getBase() . 'Modules/Media/Models/Upload.js');
|
||||||
$head->addAsset(AssetType::JS, $request->getUri()->getBase() . 'Modules/Media/Controller.js');
|
$head->addAsset(AssetType::JS, $request->getUri()->getBase() . 'Modules/Media/Controller.js');
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ namespace Modules\Media\Models;
|
||||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||||
use phpOMS\DataStorage\Database\Query\Builder;
|
use phpOMS\DataStorage\Database\Query\Builder;
|
||||||
use phpOMS\DataStorage\Database\Query\Column;
|
use phpOMS\DataStorage\Database\Query\Column;
|
||||||
|
use phpOMS\DataStorage\Database\RelationType;
|
||||||
|
|
||||||
class MediaMapper extends DataMapperAbstract
|
class MediaMapper extends DataMapperAbstract
|
||||||
{
|
{
|
||||||
|
|
@ -66,16 +67,17 @@ class MediaMapper extends DataMapperAbstract
|
||||||
protected static $primaryField = 'media_id';
|
protected static $primaryField = 'media_id';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create media.
|
* Create object.
|
||||||
*
|
*
|
||||||
* @param Media $obj Media
|
* @param mixed $obj Object
|
||||||
|
* @param int $relations Behavior for relations creation
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return mixed
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public static function create($obj, bool $relations = true)
|
public static function create($obj, int $relations = RelationType::ALL)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$objId = parent::create($obj, $relations);
|
$objId = parent::create($obj, $relations);
|
||||||
|
|
|
||||||
|
|
@ -88,44 +88,22 @@ class UploadFile
|
||||||
$files = [$files];
|
$files = [$files];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($files) > 1) {
|
$this->findOutputDir($files);
|
||||||
do {
|
|
||||||
$rndPath = str_pad(dechex(rand(0, 65535)), 4, '0', STR_PAD_LEFT);
|
|
||||||
} while (file_exists($this->outputDir . '/' . $rndPath));
|
|
||||||
|
|
||||||
$this->outputDir = $this->outputDir . '/' . $rndPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
$rpath = $this->outputDir;
|
$rpath = $this->outputDir;
|
||||||
|
|
||||||
foreach ($files as $key => $f) {
|
foreach ($files as $key => $f) {
|
||||||
$result[$key] = [];
|
$result[$key] = [];
|
||||||
|
$result[$key]['status'] = UploadStatus::OK;
|
||||||
|
|
||||||
if (!isset($f['error'])) {
|
if (!isset($f['error'])) {
|
||||||
// TODO: handle wrong parameters
|
// TODO: handle wrong parameters
|
||||||
$result[$key]['status'] = UploadStatus::WRONG_PARAMETERS;
|
$result[$key]['status'] = UploadStatus::WRONG_PARAMETERS;
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
} elseif ($f['error'] !== UPLOAD_ERR_OK) {
|
||||||
|
$result[$key]['status'] = $this->getUploadError($f['error']);
|
||||||
|
|
||||||
switch ($f['error']) {
|
return $result;
|
||||||
case UPLOAD_ERR_OK:
|
|
||||||
break;
|
|
||||||
case UPLOAD_ERR_NO_FILE:
|
|
||||||
// TODO: no file sent
|
|
||||||
$result[$key]['status'] = UploadStatus::NOTHING_UPLOADED;
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
case UPLOAD_ERR_INI_SIZE:
|
|
||||||
case UPLOAD_ERR_FORM_SIZE:
|
|
||||||
// too large
|
|
||||||
$result[$key]['status'] = UploadStatus::UPLOAD_SIZE;
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
default:
|
|
||||||
$result[$key]['status'] = UploadStatus::UNKNOWN_ERROR;
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$result[$key]['size'] = $f['size'];
|
$result[$key]['size'] = $f['size'];
|
||||||
|
|
@ -136,6 +114,7 @@ class UploadFile
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: do I need pecl fileinfo?
|
// TODO: do I need pecl fileinfo?
|
||||||
if (!empty($this->allowedTypes) && false === $ext = array_search($f['type'], $this->allowedTypes, true)) {
|
if (!empty($this->allowedTypes) && false === $ext = array_search($f['type'], $this->allowedTypes, true)) {
|
||||||
// wrong file format
|
// wrong file format
|
||||||
|
|
@ -190,21 +169,64 @@ class UploadFile
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result[$key]['path'] = $rpath;
|
$result[$key]['path'] = $rpath;
|
||||||
$result[$key]['status'] = UploadStatus::OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find unique output path for batch of files
|
||||||
|
*
|
||||||
|
* @param array $files Array of files
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
|
private function findOutputDir(array $files)
|
||||||
|
{
|
||||||
|
if (count($files) > 1) {
|
||||||
|
do {
|
||||||
|
$rndPath = str_pad(dechex(rand(0, 65535)), 4, '0', STR_PAD_LEFT);
|
||||||
|
} while (file_exists($this->outputDir . '/' . $rndPath));
|
||||||
|
|
||||||
|
$this->outputDir = $this->outputDir . '/' . $rndPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get upload error
|
||||||
|
*
|
||||||
|
* @param mixed $error Error type
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
|
private function getUploadError($error) : int
|
||||||
|
{
|
||||||
|
switch ($error) {
|
||||||
|
case UPLOAD_ERR_NO_FILE:
|
||||||
|
// TODO: no file sent
|
||||||
|
return UploadStatus::NOTHING_UPLOADED;
|
||||||
|
case UPLOAD_ERR_INI_SIZE:
|
||||||
|
case UPLOAD_ERR_FORM_SIZE:
|
||||||
|
return UploadStatus::UPLOAD_SIZE;
|
||||||
|
default:
|
||||||
|
return UploadStatus::UNKNOWN_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function getMaxSize() : int
|
||||||
function getMaxSize() : int
|
|
||||||
{
|
{
|
||||||
return $this->maxSize;
|
return $this->maxSize;
|
||||||
}
|
}
|
||||||
|
|
@ -217,8 +239,7 @@ class UploadFile
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function setMaxSize(int $maxSize)
|
||||||
function setMaxSize(int $maxSize)
|
|
||||||
{
|
{
|
||||||
$this->maxSize = $maxSize;
|
$this->maxSize = $maxSize;
|
||||||
}
|
}
|
||||||
|
|
@ -229,8 +250,7 @@ class UploadFile
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function getAllowedTypes() : array
|
||||||
function getAllowedTypes() : array
|
|
||||||
{
|
{
|
||||||
return $this->allowedTypes;
|
return $this->allowedTypes;
|
||||||
}
|
}
|
||||||
|
|
@ -243,8 +263,7 @@ class UploadFile
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function setAllowedTypes(array $allowedTypes)
|
||||||
function setAllowedTypes(array $allowedTypes)
|
|
||||||
{
|
{
|
||||||
$this->allowedTypes = $allowedTypes;
|
$this->allowedTypes = $allowedTypes;
|
||||||
}
|
}
|
||||||
|
|
@ -257,8 +276,7 @@ class UploadFile
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function addAllowedTypes($allowedTypes)
|
||||||
function addAllowedTypes($allowedTypes)
|
|
||||||
{
|
{
|
||||||
$this->allowedTypes[] = $allowedTypes;
|
$this->allowedTypes[] = $allowedTypes;
|
||||||
}
|
}
|
||||||
|
|
@ -269,8 +287,7 @@ class UploadFile
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function getOutputDir() : string
|
||||||
function getOutputDir() : string
|
|
||||||
{
|
{
|
||||||
return $this->outputDir;
|
return $this->outputDir;
|
||||||
}
|
}
|
||||||
|
|
@ -283,8 +300,7 @@ class UploadFile
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function setOutputDir(string $outputDir)
|
||||||
function setOutputDir(string $outputDir)
|
|
||||||
{
|
{
|
||||||
$this->outputDir = $outputDir;
|
$this->outputDir = $outputDir;
|
||||||
}
|
}
|
||||||
|
|
@ -295,8 +311,7 @@ class UploadFile
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function getFileName() : string
|
||||||
function getFileName() : string
|
|
||||||
{
|
{
|
||||||
return $this->fileName;
|
return $this->fileName;
|
||||||
}
|
}
|
||||||
|
|
@ -309,8 +324,7 @@ class UploadFile
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function setFileName(string $fileName)
|
||||||
function setFileName(string $fileName)
|
|
||||||
{
|
{
|
||||||
$this->fileName = $fileName;
|
$this->fileName = $fileName;
|
||||||
}
|
}
|
||||||
|
|
@ -323,8 +337,7 @@ class UploadFile
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public
|
public function setPreserveFileName(bool $preserveFileName)
|
||||||
function setPreserveFileName(bool $preserveFileName)
|
|
||||||
{
|
{
|
||||||
$this->preserveFileName = $preserveFileName;
|
$this->preserveFileName = $preserveFileName;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user