From 35e428c077dac7e57e6f6833562c88ae4a9867da Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Wed, 29 Apr 2020 01:28:38 +0200 Subject: feature : rename media fix #82 --- app/class/Controller.php | 1 + app/class/Controllermedia.php | 30 +++++++++++++++++++++++++++++- app/class/Media.php | 6 +++--- app/class/Modelmedia.php | 17 +++++++++++++++++ app/class/Routes.php | 1 + app/fn/fn.php | 3 +++ app/view/templates/media.php | 19 +++++++++++++++++-- app/view/templates/mediamenu.php | 1 + assets/css/home.css | 2 +- 9 files changed, 73 insertions(+), 7 deletions(-) diff --git a/app/class/Controller.php b/app/class/Controller.php index 56d4dfe..11a2c12 100644 --- a/app/class/Controller.php +++ b/app/class/Controller.php @@ -111,6 +111,7 @@ class Controller public function redirect($url) { header('Location: ' . $url); + exit; } public function routedirect(string $route, array $vars = []) diff --git a/app/class/Controllermedia.php b/app/class/Controllermedia.php index c6e5d6c..0d75de7 100644 --- a/app/class/Controllermedia.php +++ b/app/class/Controllermedia.php @@ -3,6 +3,7 @@ namespace Wcms; use Exception; +use InvalidArgumentException; use LogicException; class Controllermedia extends Controller @@ -116,6 +117,33 @@ class Controllermedia extends Controller $this->mediamanager->multimovefile($_POST['id'], $_POST['dir']); } } - $this->redirect($this->generate('media') . '?path=/' . $_POST['path']); + $this->redirect($this->generate('media') . $_POST['route']); + } + + public function rename() + { + if ( + $this->user->issupereditor() + && isset($_POST['oldid']) + && isset($_POST['newid']) + && isset($_POST['oldextension']) + && isset($_POST['newextension']) + && isset($_POST['path']) + ) { + $newid = idclean($_POST['newid']); + $newextension = idclean($_POST['newextension']); + if (!empty($newid) && !empty($newextension)) { + $oldname = $_POST['path'] . $_POST['oldid'] . '.' . $_POST['oldextension']; + $newname = $_POST['path'] . $newid . '.' . $newextension; + try { + $this->mediamanager->rename($oldname, $newname); + } catch (InvalidArgumentException $e) { + Model::sendflashmessage($e->getMessage(), 'error'); + } + } else { + Model::sendflashmessage('Invalid name or extension', 'warning'); + } + } + $this->redirect($this->generate('media') . $_POST['route']); } } diff --git a/app/class/Media.php b/app/class/Media.php index 7105830..099d4f8 100644 --- a/app/class/Media.php +++ b/app/class/Media.php @@ -97,14 +97,14 @@ class Media extends Item break; case 'sound': - $code = '<audio controls src="' . $this->getincludepath() . '"></audio>'; + $code = '<audio controls src="' . $this->getincludepath() . '"></audio>'; break; case 'video': $src = $this->getincludepath(); $ext = $this->extension; - $code = '<video controls="">'; - $code .= '<source src="' . $src . '" type="video/' . $ext . '"></video>'; + $code = '<video controls="">'; + $code .= '<source src="' . $src . '" type="video/' . $ext . '"></video>'; break; default: diff --git a/app/class/Modelmedia.php b/app/class/Modelmedia.php index 0b1d700..4da7707 100644 --- a/app/class/Modelmedia.php +++ b/app/class/Modelmedia.php @@ -2,6 +2,8 @@ namespace Wcms; +use Exception; +use InvalidArgumentException; use phpDocumentor\Reflection\Types\Mixed_; class Modelmedia extends Model @@ -364,4 +366,19 @@ class Modelmedia extends Model return true; } } + + /** + * @param string $oldname + * @param string $newname + * @throws InvalidArgumentException if cant access file + */ + public function rename(string $oldname, string $newname) + { + try { + accessfile($oldname); + } catch (InvalidArgumentException $e) { + throw new InvalidArgumentException($e->getMessage()); + } + return rename($oldname, $newname); + } } diff --git a/app/class/Routes.php b/app/class/Routes.php index 59659ea..13bd2ac 100644 --- a/app/class/Routes.php +++ b/app/class/Routes.php @@ -35,6 +35,7 @@ class Routes ['POST', '/!media/folderadd', 'Controllermedia#folderadd', 'mediafolderadd'], ['POST', '/!media/folderdelete', 'Controllermedia#folderdelete', 'mediafolderdelete'], ['POST', '/!media/edit', 'Controllermedia#edit', 'mediaedit'], + ['POST', '/!media/rename', 'Controllermedia#rename', 'mediarename'], ['GET', '/!font', 'Controllerfont#desktop', 'font'], ['GET', '/!font/render', 'Controllerfont#render', 'fontrender'], ['POST', '/!font/add', 'Controllerfont#add', 'fontadd'], diff --git a/app/fn/fn.php b/app/fn/fn.php index 103062a..461f129 100644 --- a/app/fn/fn.php +++ b/app/fn/fn.php @@ -425,6 +425,9 @@ function accessfile(string $path, bool $createdir = false): bool if (!is_writable($dir)) { throw new \InvalidArgumentException("Directory '$dir' is not writable."); } + if (!file_exists($path)) { + throw new \InvalidArgumentException("The file '$path' does not exist."); + } if (is_file($path) && !is_writable($path)) { throw new \InvalidArgumentException("The file '$path' is not writable."); } diff --git a/app/view/templates/media.php b/app/view/templates/media.php index 960ff33..d0caaf2 100644 --- a/app/view/templates/media.php +++ b/app/view/templates/media.php @@ -95,7 +95,7 @@ $this->layout('layout', ['title' => 'media', 'stylesheets' => [$css . 'home.css' - getcode() ?> + @@ -130,7 +130,22 @@ $this->layout('layout', ['title' => 'media', 'stylesheets' => [$css . 'home.css' ?> - + +
+ + + +
+ + + + + + + +
+
+ extension() ?> type() == 'image' ? '' . $media->getsymbol() . '' : $media->getsymbol() ?> size('hr') ?> diff --git a/app/view/templates/mediamenu.php b/app/view/templates/mediamenu.php index b64bd25..f79b2bc 100644 --- a/app/view/templates/mediamenu.php +++ b/app/view/templates/mediamenu.php @@ -39,6 +39,7 @@

Move

+
diff --git a/assets/css/home.css b/assets/css/home.css index 38de17b..f3c93eb 100644 --- a/assets/css/home.css +++ b/assets/css/home.css @@ -96,7 +96,7 @@ aside .submenu { font-size: medium; } -aside details > summary::-webkit-details-marker { +details > summary::-webkit-details-marker { display: none; } -- cgit v1.2.3