method insert documentation in bbn\Appui\Medias
Adds a new media
function(string $file, array $content = NULL, string $title = '', string $type = 'file', bool $private = false, string $description = NULL)
{
array $content = null,
string $title = '',
string $type = 'file',
bool $private = false,
string $description = null
): ?string
{
$cf =& $this->class_cfg;
if (!empty($file)
&& ($id_type = $this->opt->fromCode($type, $this->opt_id))
&& ($ext = Str::fileExt($file))
) {
if (($type === 'file') && $this->isImage($file)) {
$id_type = $this->opt->fromCode('image', $this->opt_id);
}
$content = null;
if (!$this->fs->isFile($file)) {
throw new Exception(X::_("Impossible to find the file %s", $file));
}
if ($private) {
if (!$this->usr->check()) {
return null;
}
$root = Mvc::getUserDataPath($this->usr->getId(), 'appui-note');
}
else {
$root = Mvc::getDataPath('appui-note');
}
$root .= 'media/';
$path = X::makeStoragePath($root, '', 0, $this->fs);
$dpath = substr($path, strlen($root));
$name = X::basename($file);
$mime = mime_content_type($file) ?: null;
$content = [
'path' => $dpath,
'size' => $this->fs->filesize($file),
'extension' => $ext
];
if (empty($title)) {
$title = trim(str_replace(['-', '_', '+'], ' ', X::basename($file, ".$ext")));
}
if (!$this->db->insert(
$cf['table'],
[
$cf['arch']['medias']['id_user'] => $this->usr->getId() ?: BBN_EXTERNAL_USER_ID,
$cf['arch']['medias']['type'] => $id_type,
$cf['arch']['medias']['mimetype'] => $mime,
$cf['arch']['medias']['title'] => $title,
$cf['arch']['medias']['description'] => $description,
$cf['arch']['medias']['name'] => $name ?? null,
$cf['arch']['medias']['content'] => $content ? json_encode($content) : null,
$cf['arch']['medias']['private'] => $private ? 1 : 0,
$cf['arch']['medias']['created'] => date('Y-m-d H:i:s')
]
)) {
throw new Exception(X::_("Impossible to insert the media in the database"));
}
$id = $this->db->lastId();
if ($this->fs->createPath($path.$id)) {
$this->fs->move(
$file,
$path.$id
);
$new_file = $path.$id.'/'.X::basename($file);
chmod($new_file, 0644);
if (strpos($mime, 'image/') === 0) {
$image = new Image($new_file, $this->fs);
$tst = $this->getThumbsSizesByType($id_type);
$ts = !empty($tst) && !empty($tst['thumbs']) ? \array_map(function($t){
return [
empty($t['width']) ? false : $t['width'],
empty($t['height']) ? false : $t['height'],
X::hasProps($t, ['width', 'height', 'crop'], true) ? true : false
];
}, $tst['thumbs']) : $this->thumbs_sizes;
$image->thumbs($path.$id, $ts, '.bbn-%s');
}
}
return $id;
}
return null;
}
Adds a new media BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2023
BBN Solutions