73 lines
2.1 KiB
PHP
73 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Franzz\Livetrail;
|
|
use Franzz\Objects\UploadHandler;
|
|
|
|
class Uploader extends UploadHandler
|
|
{
|
|
private Media $oMedia;
|
|
|
|
public string $sBody;
|
|
|
|
function __construct(Media &$oMedia)
|
|
{
|
|
$this->oMedia = &$oMedia;
|
|
$this->sBody = '';
|
|
|
|
parent::__construct(array(
|
|
'upload_dir' => Media::MEDIA_FOLDER.'/',
|
|
'image_versions' => array(),
|
|
'accept_file_types' => '/\.(gif|jpe?g|png|mov|mp4)$/i'
|
|
));
|
|
}
|
|
|
|
protected function validate($uploaded_file, $file, $error, $index, $content_range) {
|
|
$bResult = parent::validate($uploaded_file, $file, $error, $index, $content_range);
|
|
|
|
//Check project mode
|
|
if(!$this->oMedia->isProjectEditable()) {
|
|
$file->error = true;
|
|
$file->desc_lang_id = 'upload.mode_archived';
|
|
$file->desc_lang_params = array($this->oMedia->getProjectCodeName());
|
|
$bResult = false;
|
|
}
|
|
|
|
return $bResult;
|
|
}
|
|
|
|
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) {
|
|
$sExt = strtolower(pathinfo((string) $name, PATHINFO_EXTENSION));
|
|
$sStoredName = bin2hex(random_bytes(16)).($sExt !== ''?'.'.$sExt:'');
|
|
$file = parent::handle_file_upload($uploaded_file, $sStoredName, $size, $type, $error, $index, $content_range);
|
|
|
|
if(empty($file->error)) {
|
|
$asResult = $this->oMedia->addMedia($file->name);
|
|
if(!$asResult['result']) {
|
|
$file->error = true;
|
|
$file->desc_lang_id = $asResult['desc_lang_id'];
|
|
$file->desc_lang_params = $asResult['data'];
|
|
}
|
|
else {
|
|
$file->original_name = basename((string) $name);
|
|
$file->id = $this->oMedia->getMediaId();
|
|
$file->thumbnail = $asResult['data']['thumb_path'];
|
|
}
|
|
}
|
|
if(!empty($file->error)) {
|
|
if(empty($file->desc_lang_id)) $file->desc_lang_id = is_string($file->error)?$file->error:'upload.error';
|
|
if(empty($file->desc_lang_params)) $file->desc_lang_params = array();
|
|
$file->error = true;
|
|
}
|
|
|
|
return $file;
|
|
}
|
|
|
|
protected function body($sBodyPart) {
|
|
$this->sBody .= $sBodyPart;
|
|
}
|
|
|
|
protected function get_error_message($sLangId, $asParams=array()) {
|
|
return array_key_exists($sLangId, $this->error_messages)?'upload.error':$sLangId;
|
|
}
|
|
}
|