Skip to content

Commit

Permalink
don't try to build Media with thing that are not files fix #437
Browse files Browse the repository at this point in the history
+ some refactor around media listing
  • Loading branch information
vincent-peugnet committed Oct 13, 2024
1 parent 0244b61 commit dbbc86f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 88 deletions.
52 changes: 26 additions & 26 deletions app/class/Controllermedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,36 @@ public function desktop()
$mediaopt = new Mediaoptlist($datas);

try {
$this->mediamanager->checkdir($this->mediaopt->dir());
} catch (Folderexception $e) {
Model::sendflashmessage($e->getMessage(), Model::FLASH_WARNING);
$this->mediaopt->setpath(Model::MEDIA_DIR);
$this->redirect($this->generate("media", [], $this->mediaopt->getpathaddress()));
}

$medialist = $this->mediamanager->medialistopt($mediaopt);
Fs::dircheck($this->mediaopt->dir());
$medialist = $this->mediamanager->medialistopt($mediaopt);

$dirlist = $this->mediamanager->listdir(Model::MEDIA_DIR);
$dirlist = $this->mediamanager->listdir(Model::MEDIA_DIR);

$pathlist = [];
$this->mediamanager->listpath($dirlist, '', $pathlist);
$pathlist = [];
$this->mediamanager->listpath($dirlist, '', $pathlist);

$vars['maxuploadsize'] = readablesize(file_upload_max_size()) . 'o';
$vars['cssfont'] = Model::dirtopath(Model::FONTS_CSS_FILE);
$vars['maxuploadsize'] = readablesize(file_upload_max_size()) . 'o';
$vars['cssfont'] = Model::dirtopath(Model::FONTS_CSS_FILE);

if (isset($_GET['display'])) {
$this->workspace->setmediadisplay($_GET['display']);
$this->servicesession->setworkspace($this->workspace);
}
if (isset($_GET['display'])) {
$this->workspace->setmediadisplay($_GET['display']);
$this->servicesession->setworkspace($this->workspace);
}

$vars['filtercode'] = !empty($_POST); // indicate that filter code has been generated
$vars['medialist'] = $medialist;
$vars['dirlist'] = $dirlist;
$vars['pathlist'] = $pathlist;
$vars['mediaopt'] = $mediaopt;
$vars['optimizeimage'] = (extension_loaded('imagick') || extension_loaded('gd'));
$vars['filtercode'] = !empty($_POST); // indicate that filter code has been generated
$vars['medialist'] = $medialist;
$vars['dirlist'] = $dirlist;
$vars['pathlist'] = $pathlist;
$vars['mediaopt'] = $mediaopt;
$vars['optimizeimage'] = (extension_loaded('imagick') || extension_loaded('gd'));

$this->showtemplate('media', $vars);
$this->showtemplate('media', $vars);
} catch (Folderexception $e) {
// TODO: instead of redirecting show an error template
Model::sendflashmessage($e->getMessage(), Model::FLASH_WARNING);
$this->mediaopt->setpath(Model::MEDIA_DIR);
$this->redirect($this->generate('media', [], $this->mediaopt->getpathaddress()));
}
}

public function upload()
Expand Down Expand Up @@ -184,10 +184,10 @@ public function edit()
$this->refreshfont = $_POST['dir'] === Model::FONT_DIR;
}
if ($this->refreshfont || $_POST['path'] === Model::FONT_DIR) {
$fontfacer = new Servicefont($this->mediamanager);
try {
$fontfacer = new Servicefont($this->mediamanager);
$fontfacer->writecss();
} catch (Filesystemexception $e) {
} catch (RuntimeException $e) {
Model::sendflashmessage('Error while updating fonts CSS : ' . $e->getMessage());
}
}
Expand Down
70 changes: 36 additions & 34 deletions app/class/Mediaoptlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wcms;

use RuntimeException;

class Mediaoptlist extends Mediaopt
{
/** @var string full regex match */
Expand All @@ -24,45 +26,45 @@ public function readoptions(): void
$this->hydrate($datas);
}

public function generatecontent()
/**
* Generate HTML displaying list of medias
*
* @throws RuntimeException If something went wrong
*/
public function generatecontent(): string
{
$mediamanager = new Modelmedia();
$medialist = $mediamanager->getlistermedia($this->dir(), $this->type);
if (!$medialist) {
return false;
} else {
$mediamanager->medialistsort($medialist, $this->sortby, $this->order);

$dirid = str_replace('/', '-', $this->path);

$div = "<div class=\"medialist\" id=\"$dirid\">\n";

foreach ($medialist as $media) {
$div .= '<div class="content ' . $media->type() . '">';
$id = 'id="media_' . $media->filename() . '"';
$path = $media->getincludepath();
$ext = $media->extension();
$filename = $media->filename();
if ($media->type() == 'image') {
$div .= '<img alt="' . $media->filename() . '" ' . $id . ' src="' . $path . '" >';
} elseif ($media->type() == 'sound') {
$div .= '<audio ' . $id . ' controls src="' . $path . '" </audio>';
} elseif ($media->type() == 'video') {
$source = '<source src="' . $path . '" type="video/' . $ext . '" ' . $id . '>';
$div .= '<video controls>' . $source . '</video>';
} else {
$div .= '<a href="' . $path . '" target="_blank" class="media" ' . $id . '>' . $filename . '</a>';
}
if ($this->filename && in_array($media->type(), ['image', 'sound', 'video'])) {
$div .= "<div class=\"filename\">$filename</div>";
}
$div .= "</div>\n";
$medialist = $mediamanager->medialistopt($this);

$dirid = str_replace('/', '-', $this->path);

$div = "<div class=\"medialist\" id=\"$dirid\">\n";

foreach ($medialist as $media) {
$div .= '<div class="content ' . $media->type() . '">';
$id = 'id="media_' . $media->filename() . '"';
$path = $media->getincludepath();
$ext = $media->extension();
$filename = $media->filename();
if ($media->type() == 'image') {
$div .= '<img alt="' . $media->filename() . '" ' . $id . ' src="' . $path . '" >';
} elseif ($media->type() == 'sound') {
$div .= '<audio ' . $id . ' controls src="' . $path . '" </audio>';
} elseif ($media->type() == 'video') {
$source = '<source src="' . $path . '" type="video/' . $ext . '" ' . $id . '>';
$div .= '<video controls>' . $source . '</video>';
} else {
$div .= '<a href="' . $path . '" target="_blank" class="media" ' . $id . '>' . $filename . '</a>';
}
if ($this->filename && in_array($media->type(), ['image', 'sound', 'video'])) {
$div .= "<div class=\"filename\">$filename</div>";
}

$div .= "</div>\n";

return $div;
}

$div .= "</div>\n";

return $div;
}

public function getquery()
Expand Down
58 changes: 32 additions & 26 deletions app/class/Modelmedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RuntimeException;
use Imagick;
use ImagickException;
use RangeException;
use Wcms\Exception\Forbiddenexception;

class Modelmedia extends Model
Expand Down Expand Up @@ -36,10 +37,12 @@ class Modelmedia extends Model

/**
* @return Media[] sorted array of Media
*
* @throws Folderexception If dir is not a valid folder
*/
public function medialistopt(Mediaopt $mediaopt): array
{
$medialist = $this->getlistermedia($mediaopt->dir(), $mediaopt->type());
$medialist = $this->getlistermedia($mediaopt);
$this->medialistsort($medialist, $mediaopt->sortby(), $mediaopt->order());

return $medialist;
Expand All @@ -48,30 +51,34 @@ public function medialistopt(Mediaopt $mediaopt): array
/**
* get a list of media of selected types
*
* @param string $dir Media directory ot look at
* @param array $type
* @param Mediaopt $mediaopt Media option filter Object
*
* @return Media[] array of Media objects
*
* @return Media[] of Media objects
* @throws Folderexception When the given folder isn't a directory
*/
public function getlistermedia($dir, $type = []): array
protected function getlistermedia(Mediaopt $mediaopt): array
{
if (is_dir($dir) && $handle = opendir($dir)) {
$list = [];
while (false !== ($entry = readdir($handle))) {
if (!empty($entry) && $entry != "." && $entry != "..") {
try {
$media = new Media($dir . $entry);
if (empty($type) || in_array($media->type(), $type)) {
$list[] = $media;
}
} catch (RuntimeException $e) {
Logger::errorex($e);
$dir = $mediaopt->dir();
$types = $mediaopt->type();
if (!is_dir($dir)) {
throw new Folderexception("$dir is not a directory");
}
$list = [];
$files = scandir($dir);
foreach ($files as $file) {
if (is_file($dir . $file)) {
try {
$media = new Media($dir . $file);
if (empty($types) || in_array($media->type(), $types)) {
$list[] = $media;
}
} catch (RuntimeException $e) {
Logger::errorex($e);
}
}
return $list;
}
return [];
return $list;
}


Expand All @@ -82,22 +89,22 @@ public function getlistermedia($dir, $type = []): array
* @param string $sortby
* @param int $order Can be 1 or -1
*/
public function medialistsort(array &$medialist, string $sortby = 'id', int $order = 1): bool
protected function medialistsort(array &$medialist, string $sortby = 'id', int $order = 1): bool
{
$sortby = (in_array($sortby, self::MEDIA_SORTBY)) ? $sortby : 'id';
$order = ($order === 1 || $order === -1) ? $order : 1;
return usort($medialist, $this->buildsorter($sortby, $order));
}

public function buildsorter($sortby, $order)
protected function buildsorter($sortby, $order)
{
return function ($media1, $media2) use ($sortby, $order) {
$result = $this->mediacompare($media1, $media2, $sortby, $order);
return $result;
};
}

public function mediacompare($media1, $media2, $method = 'filename', $order = 1)
protected function mediacompare($media1, $media2, $method = 'filename', $order = 1)
{
$result = ($media1->$method() <=> $media2->$method());
return $result * $order;
Expand Down Expand Up @@ -283,16 +290,15 @@ public function multiupload(string $index, string $target, bool $idclean = false
/**
* @param string $dir directory path
*
* @throws Folderexception if target folder does not exist or is outside `/media` folder
* @return void
*
* @throws Folderexception if target folder does not exist
*/
public function checkdir(string $dir)
public function checkdir(string $dir): void
{
if (!is_dir($dir)) {
throw new Folderexception("directory `$dir` does not exists");
}
if (strpos($dir, "media/") !== 0) {
throw new Folderexception("directory `$dir`, is not inside `/media` folder");
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion app/class/Servicefont.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wcms;

use RuntimeException ;

class Servicefont
{
protected Modelmedia $mediamanager;
Expand All @@ -11,11 +13,14 @@ class Servicefont

/**
* This will import all the Media as fonts
*
* @throws RuntimeException If font folder is not working
*/
public function __construct(Modelmedia $mediamanager)
{
$this->mediamanager = $mediamanager;
$medias = $this->mediamanager->getlistermedia(Model::FONT_DIR, [Media::FONT]);
$mediaopt = new Mediaopt(['path' => Model::FONT_DIR, 'type' => [Media::FONT]]);
$medias = $this->mediamanager->medialistopt($mediaopt);
$medias = $this->filterfonts($medias);
$this->fonts = $this->groupfonts($medias);
}
Expand Down
6 changes: 5 additions & 1 deletion app/class/Servicerender.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,11 @@ protected function automedialist(string $text): string
foreach ($matches as $match) {
$medialist = new Mediaoptlist($match);
$medialist->readoptions();
$text = str_replace($medialist->fullmatch(), $medialist->generatecontent(), $text);
try {
$text = str_replace($medialist->fullmatch(), $medialist->generatecontent(), $text);
} catch (RuntimeException $e) {
Logger::errorex($e);
}
}
}
return $text;
Expand Down

0 comments on commit dbbc86f

Please sign in to comment.