Files
2026-08-27 12:45:30 +02:00

71 lines
2.1 KiB
PHP

<?php
namespace Franzz\Livetrail;
use Franzz\Objects\UploadHandler;
class Uploader extends UploadHandler {
private Media $oMedia;
public string $sBody;
public function __construct(Media &$oMedia) {
$this->oMedia = &$oMedia;
$this->sBody = '';
parent::__construct([
'upload_dir' => Media::MEDIA_FOLDER.'/',
'image_versions' => [],
'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 = [$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 = [];
$file->error = true;
}
return $file;
}
protected function body($sBodyPart) {
$this->sBody .= $sBodyPart;
}
protected function get_error_message($sLangId, $asParams=[]) {
return array_key_exists($sLangId, $this->error_messages)?'upload.error':$sLangId;
}
}