From 85a68f9fbe567d7831ffeda1d7ebd90d656befb3 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Fri, 27 Dec 2024 16:23:10 +0100 Subject: [PATCH 01/20] fix issues DPMedia: FTP --- .../src/Service/FileManager/FileManager.php | 10 +++++----- .../src/Service/FileManager/FileManagerInterface.php | 8 ++++---- .../src/Service/Filesystem/Filesystem.php | 11 +++++++++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php index 22161ff5c..bfce80076 100644 --- a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php +++ b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php @@ -93,18 +93,18 @@ public function __construct($catid, $selection=False) /** * Creation of image types based on source file. * Source file has to be given with a full system path. - * * * @param string $source Source file with which the image types shall be created * @param string $filename Name for the files to be created * @param object|int|string $cat Object, ID or alias of the corresponding category (default: 2) - * @param bool $processing True to create imagetypes by processing source (default: True) + * @param bool $processing True to create imagetypes by processing source (defualt: True) + * @param bool $local_source True if the source is a file located in a local folder (default: True) * * @return bool True on success, false otherwise * * @since 4.0.0 */ - public function createImages($source, $filename, $cat=2, $processing=True): bool + public function createImages($source, $filename, $cat=2, $processing=True, $local_source=True): bool { if(!$filename) { @@ -170,9 +170,9 @@ public function createImages($source, $filename, $cat=2, $processing=True): bool // Grap resource if needed $isStream = false; - if(\strpos($this->component->getFilesystem()->getFilesystem(), 'local') === false) + if(!$local_source && \strpos($this->component->getFilesystem()->getFilesystem(), 'local') === false) { - // We are dealing with an external filesystem + // The source is located in the external filesystem $source = $this->component->getFilesystem()->getResource($source); $isStream = true; } diff --git a/administrator/com_joomgallery/src/Service/FileManager/FileManagerInterface.php b/administrator/com_joomgallery/src/Service/FileManager/FileManagerInterface.php index e521563b0..b5e9e1cbb 100644 --- a/administrator/com_joomgallery/src/Service/FileManager/FileManagerInterface.php +++ b/administrator/com_joomgallery/src/Service/FileManager/FileManagerInterface.php @@ -29,17 +29,17 @@ interface FileManagerInterface * Creation of image types based on source file. * Source file has to be given with a full system path. * - * * @param string $source Source file with which the image types shall be created * @param string $filename Name for the files to be created * @param object|int|string $cat Object, ID or alias of the corresponding category (default: 2) - * @param bool $processing True to create imagetypes by processing source (default: True) - * + * @param bool $processing True to create imagetypes by processing source (defualt: True) + * @param bool $local_source True if the source is a file located in a local folder (default: True) + * * @return bool True on success, false otherwise * * @since 4.0.0 */ - public function createImages($source, $filename, $cat=2, $processing=True): bool; + public function createImages($source, $filename, $cat=2, $processing=True, $local_source=True): bool; /** * Deletion of image types diff --git a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php index bab342525..2b2d0a80c 100644 --- a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php +++ b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php @@ -98,7 +98,7 @@ public function __construct(string $filesystem = '') else { // Define filesystem adapter based on configuration 'jg_filesystem' - $this->component->getConfig()->get('jg_filesystem','local-images'); + $this->filesystem = $this->component->getConfig()->get('jg_filesystem','local-images'); } // Load language of com_media @@ -310,11 +310,18 @@ public function getFile(string $path = '/', array $options = []): \stdClass catch (\Exception $e) { $msg = $e->getMessage(); - if(\strpos($e->getMessage(), 'account') !== false || \strpos($e->getMessage(), 'Account') !== false) + if(\strpos(\strtolower($e->getMessage()), 'account')) { $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND'), 'error', 'jerror'); throw new \Exception(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter)); } + elseif(\strpos(\strtolower($e->getMessage()), 'no such file') !== false) + { + $this->component->addLog('FileNotFoundException in function getFile in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'jerror'); + $this->component->addLog('$adapter: ' . $adapter, 'warning', 'jerror'); + $this->component->addLog('$path: ' . $path, 'warning', 'jerror'); + throw new FileNotFoundException(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND')); + } else { $this->component->addLog($e->getMessage(), 'error', 'jerror'); From f80a817fca29cd77299978695499b9add869bcd2 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Fri, 27 Dec 2024 16:57:07 +0100 Subject: [PATCH 02/20] fix issue with recreation --- administrator/com_joomgallery/src/Model/ImageModel.php | 2 +- .../src/Service/FileManager/FileManager.php | 10 +++++++--- .../src/Service/Filesystem/Filesystem.php | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/administrator/com_joomgallery/src/Model/ImageModel.php b/administrator/com_joomgallery/src/Model/ImageModel.php index b7e4be34b..99e4df4dd 100644 --- a/administrator/com_joomgallery/src/Model/ImageModel.php +++ b/administrator/com_joomgallery/src/Model/ImageModel.php @@ -1080,7 +1080,7 @@ public function recreate(int $pk, $type='original'): bool } // Perform the recreation - if(!$this->component->getFileManager()->createImages($source, $table->filename, $table->catid)) + if(!$this->component->getFileManager()->createImages($source, $table->filename, $table->catid, true, false)) { $this->setError($table->getError()); diff --git a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php index bfce80076..f8c7f76a7 100644 --- a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php +++ b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php @@ -170,10 +170,14 @@ public function createImages($source, $filename, $cat=2, $processing=True, $loca // Grap resource if needed $isStream = false; - if(!$local_source && \strpos($this->component->getFilesystem()->getFilesystem(), 'local') === false) + if(\is_resource($source)) { - // The source is located in the external filesystem - $source = $this->component->getFilesystem()->getResource($source); + $isStream = true; + } + elseif(\is_string($source) && !$local_source && \strpos($this->component->getFilesystem()->getFilesystem(), 'local') === false) + { + // The path is pointing to an external filesystem + list($file_info, $source) = $this->component->getFilesystem()->getResource($source); $isStream = true; } diff --git a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php index 2b2d0a80c..c04e88daf 100644 --- a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php +++ b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php @@ -343,7 +343,7 @@ public function getFile(string $path = '/', array $options = []): \stdClass if(isset($options['content']) && $options['content'] && $file->type == 'file') { - $resource = $this->getAdapter($adapter)->getResource($file->path); + list($file_info, $resource) = $this->getAdapter($adapter)->getResource($file->path); if($resource) { @@ -410,7 +410,7 @@ public function getFiles(string $path = '/', array $options = []): array if(isset($options['content']) && $options['content'] && $file->type == 'file') { - $resource = $this->getAdapter($adapter)->getResource($file->path); + list($file_info, $resource) = $this->getAdapter($adapter)->getResource($file->path); if($resource) { From 1b4246102717c511f83abfbf42c312b73e7d5567 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Sun, 29 Dec 2024 11:59:49 +0100 Subject: [PATCH 03/20] add filesystem to image record --- administrator/com_joomgallery/forms/image.xml | 7 +++++++ .../sql/install.mysql.utf8.sql | 1 + .../sql/updates/mysql/4.0.0.sql | 1 + .../src/Service/FileManager/FileManager.php | 7 ++++--- .../src/Service/Uploader/Uploader.php | 3 +++ .../src/View/Image/RawView.php | 19 ++++++++++++++----- .../com_joomgallery/tmpl/image/edit.php | 1 + 7 files changed, 31 insertions(+), 8 deletions(-) diff --git a/administrator/com_joomgallery/forms/image.xml b/administrator/com_joomgallery/forms/image.xml index 6deda8a3f..a1198ea9f 100644 --- a/administrator/com_joomgallery/forms/image.xml +++ b/administrator/com_joomgallery/forms/image.xml @@ -24,6 +24,13 @@ + + component->getFilesystem()->createFolder(\basename($folder), \dirname($folder)); + $res = $this->component->getFilesystem()->createFolder(\basename($folder), \dirname($folder), false); } - catch(\FileExistsException $e) + catch(FileExistsException $e) { - // Do nothing + // Folder already exists. + $res = true; } catch(\Exception $e) { diff --git a/administrator/com_joomgallery/src/Service/Uploader/Uploader.php b/administrator/com_joomgallery/src/Service/Uploader/Uploader.php index 1bdc3589f..70402353e 100644 --- a/administrator/com_joomgallery/src/Service/Uploader/Uploader.php +++ b/administrator/com_joomgallery/src/Service/Uploader/Uploader.php @@ -207,6 +207,9 @@ public function retrieveImage(&$data, $filename=True): bool } } + // Set filesystem + $data['filesystem'] = $this->component->getFilesystem()->get('filesystem'); + // Trigger onJoomBeforeUpload $plugins = $this->app->triggerEvent('onJoomBeforeUpload', array($data['filename'])); if(in_array(false, $plugins, true)) diff --git a/administrator/com_joomgallery/src/View/Image/RawView.php b/administrator/com_joomgallery/src/View/Image/RawView.php index bfbdeed92..2b369edb0 100644 --- a/administrator/com_joomgallery/src/View/Image/RawView.php +++ b/administrator/com_joomgallery/src/View/Image/RawView.php @@ -44,18 +44,27 @@ public function display($tpl = null) if(!$this->access($id)) { $this->app->redirect(Route::_('index.php', false), 403); - } - - // Get image path - $img_path = JoomHelper::getImg($id, $type, false, false); + } - // Create filesystem service + // Choose the filesystem adapter $adapter = ''; if($id === 0) { // Force local-images adapter to load the no-image file $adapter = 'local-images'; } + else + { + // Take the adapter from the image object + $img_obj = $this->get('Item'); + $adapter = $img_obj->filesystem; + } + + // Get image path + $img_obj ? $img = $img_obj : $img = $id; + $img_path = JoomHelper::getImg($img, $type, false, false); + + // Create filesystem service $this->component->createFilesystem($adapter); // Get image resource diff --git a/administrator/com_joomgallery/tmpl/image/edit.php b/administrator/com_joomgallery/tmpl/image/edit.php index 8409b7a32..f109f640d 100644 --- a/administrator/com_joomgallery/tmpl/image/edit.php +++ b/administrator/com_joomgallery/tmpl/image/edit.php @@ -96,6 +96,7 @@ +
form->renderField('filesystem'); ?>
From 17211fefd3780d298138eed4a6a2f32172f8182e Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Sun, 29 Dec 2024 13:29:26 +0100 Subject: [PATCH 04/20] add warning if filesystem plugin is missing --- .../language/en-GB/com_joomgallery.ini | 1 + .../com_joomgallery/src/Helper/JoomHelper.php | 51 +++++++++++++++++++ .../src/View/Images/HtmlView.php | 4 ++ 3 files changed, 56 insertions(+) diff --git a/administrator/com_joomgallery/language/en-GB/com_joomgallery.ini b/administrator/com_joomgallery/language/en-GB/com_joomgallery.ini index f390faa70..cedb9aadb 100644 --- a/administrator/com_joomgallery/language/en-GB/com_joomgallery.ini +++ b/administrator/com_joomgallery/language/en-GB/com_joomgallery.ini @@ -787,6 +787,7 @@ COM_JOOMGALLERY_SERVICE_ERROR_CREATE_FILE="File could not be created." COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_A_DIRECTORY="Requested path is not a directory." COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND="No filesystem plugin available for the specified adapter (%s)." COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_ERROR="Filesystem plugin is reporting the following error: %s" +COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_PLUGIN_NOT_ENABLED="It seems that the required filesystem plugin (%s) is not installed or not enabled. Please install and enable the plugin here." COM_JOOMGALLERY_SERVICE_MIGRATION_CHECK_TITLE="Check results" COM_JOOMGALLERY_SERVICE_MIGRATION_GENERAL_PRECHECK_DESC="General checks like log file, site state, ..." COM_JOOMGALLERY_SERVICE_MIGRATION_SOURCE_PRECHECK_DESC="Check source extension, directories and tables if they are compatible and existent." diff --git a/administrator/com_joomgallery/src/Helper/JoomHelper.php b/administrator/com_joomgallery/src/Helper/JoomHelper.php index e225ae02f..e0a9d0249 100644 --- a/administrator/com_joomgallery/src/Helper/JoomHelper.php +++ b/administrator/com_joomgallery/src/Helper/JoomHelper.php @@ -21,6 +21,7 @@ use \Joomla\CMS\Access\Access; use \Joomla\CMS\Filesystem\Path; use \Joomla\CMS\Http\HttpFactory; +use \Joomla\CMS\Plugin\PluginHelper; use \Joomla\CMS\Language\Multilanguage; use \Joomla\Database\DatabaseInterface; @@ -1051,6 +1052,56 @@ public static function fetchXML(string $uri): \SimpleXMLElement return new \SimpleXMLElement($xmlString); } + /** + * Method to check wether all needed filesystem plugins are available and enabled. + * + * @return bool + * + * @since 4.0.0 + */ + public static function checkFilesystems() + { + // Load all used filesystems from images table + $db = Factory::getContainer()->get(DatabaseInterface::class); + + $query = $db->getQuery(true) + ->select('DISTINCT ' .$db->quoteName('filesystem')) + ->from(_JOOM_TABLE_IMAGES) + ->where($db->quoteName('published') . ' = 1'); + + $db->setQuery($query); + $filesystems = $db->loadColumn(); + + // Loop throug all found filesystems + foreach ($filesystems as $key => $filesystem) + { + // Get correstonding plugin name + $plugin_name = \explode('-', $filesystem, 2)[0]; + $plugin_fullname = 'plg_filesystem_'.$plugin_name; + + if(!PluginHelper::isEnabled('filesystem', $plugin_fullname)) + { + // Plugin is not installed or not enabled. Show warning message. + $lang = Factory::getLanguage(); + + if(!$lang->getPaths($plugin_fullname)) + { + // Language file is not availavle + $langFile = JPATH_PLUGINS . '/filesystem/' . $plugin_name; + + // Try to load plugin language file + $lang->load($plugin_fullname); + $lang->load($plugin_fullname, $langFile); + } + + $plugins_url = Route::_('index.php?option=com_plugins&view=plugins&filter[folder]=filesystem'); + $plugin_title = Text::_($plugin_fullname); + + self::getComponent()->setWarning(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_PLUGIN_NOT_ENABLED', $plugin_title, $plugins_url)); + } + } + } + /** * Returns a list of all available access action names available * diff --git a/administrator/com_joomgallery/src/View/Images/HtmlView.php b/administrator/com_joomgallery/src/View/Images/HtmlView.php index 139ebe5ce..84fd8ac77 100644 --- a/administrator/com_joomgallery/src/View/Images/HtmlView.php +++ b/administrator/com_joomgallery/src/View/Images/HtmlView.php @@ -18,6 +18,7 @@ use \Joomla\CMS\HTML\Helpers\Sidebar; use \Joomla\CMS\Toolbar\ToolbarHelper; use \Joomla\Component\Content\Administrator\Extension\ContentComponent; +use \Joomgallery\Component\Joomgallery\Administrator\Helper\JoomHelper; use \Joomgallery\Component\Joomgallery\Administrator\View\JoomGalleryView; /** @@ -48,6 +49,9 @@ public function display($tpl = null) $this->pagination = $this->get('Pagination'); $this->filterForm = $this->get('FilterForm'); $this->activeFilters = $this->get('ActiveFilters'); + + // Check if filesystem plugins are available + JoomHelper::checkFilesystems(); // Check for errors. if(\count($errors = $this->get('Errors'))) From 00618d691f59e3b2c4a0087f614507dac1df5915 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Sun, 29 Dec 2024 13:32:51 +0100 Subject: [PATCH 05/20] fix issue with filename plugin warning --- administrator/com_joomgallery/src/Helper/JoomHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/com_joomgallery/src/Helper/JoomHelper.php b/administrator/com_joomgallery/src/Helper/JoomHelper.php index e0a9d0249..2c3aa8917 100644 --- a/administrator/com_joomgallery/src/Helper/JoomHelper.php +++ b/administrator/com_joomgallery/src/Helper/JoomHelper.php @@ -1079,7 +1079,7 @@ public static function checkFilesystems() $plugin_name = \explode('-', $filesystem, 2)[0]; $plugin_fullname = 'plg_filesystem_'.$plugin_name; - if(!PluginHelper::isEnabled('filesystem', $plugin_fullname)) + if(!PluginHelper::isEnabled('filesystem', $plugin_name)) { // Plugin is not installed or not enabled. Show warning message. $lang = Factory::getLanguage(); From c0f4992183eef47e70aa40e7b71b8ee70e923feb Mon Sep 17 00:00:00 2001 From: Elfangor <39154009+Elfangor93@users.noreply.github.com> Date: Tue, 31 Dec 2024 12:00:07 +0100 Subject: [PATCH 06/20] Update administrator/com_joomgallery/src/Helper/JoomHelper.php Co-authored-by: Brian Teeman --- administrator/com_joomgallery/src/Helper/JoomHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/com_joomgallery/src/Helper/JoomHelper.php b/administrator/com_joomgallery/src/Helper/JoomHelper.php index 2c3aa8917..aec71e576 100644 --- a/administrator/com_joomgallery/src/Helper/JoomHelper.php +++ b/administrator/com_joomgallery/src/Helper/JoomHelper.php @@ -1053,7 +1053,7 @@ public static function fetchXML(string $uri): \SimpleXMLElement } /** - * Method to check wether all needed filesystem plugins are available and enabled. + * Method to check whether all needed filesystem plugins are available and enabled. * * @return bool * From 9cf04423ab8972b924f07be065684412877017b9 Mon Sep 17 00:00:00 2001 From: Elfangor <39154009+Elfangor93@users.noreply.github.com> Date: Tue, 31 Dec 2024 12:00:17 +0100 Subject: [PATCH 07/20] Update administrator/com_joomgallery/src/Helper/JoomHelper.php Co-authored-by: Brian Teeman --- administrator/com_joomgallery/src/Helper/JoomHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/com_joomgallery/src/Helper/JoomHelper.php b/administrator/com_joomgallery/src/Helper/JoomHelper.php index aec71e576..eb66800f6 100644 --- a/administrator/com_joomgallery/src/Helper/JoomHelper.php +++ b/administrator/com_joomgallery/src/Helper/JoomHelper.php @@ -1072,7 +1072,7 @@ public static function checkFilesystems() $db->setQuery($query); $filesystems = $db->loadColumn(); - // Loop throug all found filesystems + // Loop through all found filesystems foreach ($filesystems as $key => $filesystem) { // Get correstonding plugin name From 0af5ffc108d251db0a1798817dda1462d61d32e4 Mon Sep 17 00:00:00 2001 From: Elfangor <39154009+Elfangor93@users.noreply.github.com> Date: Tue, 31 Dec 2024 12:00:33 +0100 Subject: [PATCH 08/20] Update administrator/com_joomgallery/src/Helper/JoomHelper.php Co-authored-by: Brian Teeman --- administrator/com_joomgallery/src/Helper/JoomHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/com_joomgallery/src/Helper/JoomHelper.php b/administrator/com_joomgallery/src/Helper/JoomHelper.php index eb66800f6..5c7a552d4 100644 --- a/administrator/com_joomgallery/src/Helper/JoomHelper.php +++ b/administrator/com_joomgallery/src/Helper/JoomHelper.php @@ -1075,7 +1075,7 @@ public static function checkFilesystems() // Loop through all found filesystems foreach ($filesystems as $key => $filesystem) { - // Get correstonding plugin name + // Get corresponding plugin name $plugin_name = \explode('-', $filesystem, 2)[0]; $plugin_fullname = 'plg_filesystem_'.$plugin_name; From bef62b7022dfc2a5ad5bd0a2184cd821148c261d Mon Sep 17 00:00:00 2001 From: Elfangor <39154009+Elfangor93@users.noreply.github.com> Date: Tue, 31 Dec 2024 12:00:43 +0100 Subject: [PATCH 09/20] Update administrator/com_joomgallery/src/Helper/JoomHelper.php Co-authored-by: Brian Teeman --- administrator/com_joomgallery/src/Helper/JoomHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/com_joomgallery/src/Helper/JoomHelper.php b/administrator/com_joomgallery/src/Helper/JoomHelper.php index 5c7a552d4..38a5b00a6 100644 --- a/administrator/com_joomgallery/src/Helper/JoomHelper.php +++ b/administrator/com_joomgallery/src/Helper/JoomHelper.php @@ -1086,7 +1086,7 @@ public static function checkFilesystems() if(!$lang->getPaths($plugin_fullname)) { - // Language file is not availavle + // Language file is not available $langFile = JPATH_PLUGINS . '/filesystem/' . $plugin_name; // Try to load plugin language file From 88557a8585ff930643c1e2d674a0a04a95c7c1e4 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Wed, 1 Jan 2025 12:58:37 +0100 Subject: [PATCH 10/20] fix issue with recreation and local filesystem --- .../com_joomgallery/src/Model/ImageModel.php | 2 +- .../src/Service/FileManager/FileManager.php | 36 ++++++++++++++++++- .../FileManager/FileManagerInterface.php | 3 +- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/administrator/com_joomgallery/src/Model/ImageModel.php b/administrator/com_joomgallery/src/Model/ImageModel.php index 99e4df4dd..9834cb3f4 100644 --- a/administrator/com_joomgallery/src/Model/ImageModel.php +++ b/administrator/com_joomgallery/src/Model/ImageModel.php @@ -1080,7 +1080,7 @@ public function recreate(int $pk, $type='original'): bool } // Perform the recreation - if(!$this->component->getFileManager()->createImages($source, $table->filename, $table->catid, true, false)) + if(!$this->component->getFileManager()->createImages($source, $table->filename, $table->catid, true, false, [$type])) { $this->setError($table->getError()); diff --git a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php index d0f82b25f..128a45e06 100644 --- a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php +++ b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php @@ -13,6 +13,7 @@ \defined('_JEXEC') or die; use \Joomla\CMS\Language\Text; +use \Joomla\CMS\Filesystem\Path; use \Joomgallery\Component\Joomgallery\Administrator\Helper\JoomHelper; use \Joomgallery\Component\Joomgallery\Administrator\Extension\ServiceTrait; use \Joomgallery\Component\Joomgallery\Administrator\Service\FileManager\FileManagerInterface; @@ -99,12 +100,13 @@ public function __construct($catid, $selection=False) * @param object|int|string $cat Object, ID or alias of the corresponding category (default: 2) * @param bool $processing True to create imagetypes by processing source (defualt: True) * @param bool $local_source True if the source is a file located in a local folder (default: True) + * @param array $skip List of imagetypes to skip creation (default: []) * * @return bool True on success, false otherwise * * @since 4.0.0 */ - public function createImages($source, $filename, $cat=2, $processing=True, $local_source=True): bool + public function createImages($source, $filename, $cat=2, $processing=True, $local_source=True, $skip=[]): bool { if(!$filename) { @@ -131,6 +133,13 @@ public function createImages($source, $filename, $cat=2, $processing=True, $loca // Debug info $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_PROCESSING_IMAGETYPE', $imagetype->typename), true, true); + if(\in_array($imagetype->typename, $skip) || \in_array($imagetype->type_alias, $skip)) + { + $this->component->addDebug(Text::_('COM_JOOMGALLERY_SERVICE_MANIPULATION_NOT_NEEDED')); + + continue; + } + // For original images: check if processing is really needed if( $imagetype->typename == 'original' && $processing && $imagetype->params->get('jg_imgtypeanim', 0) == 1 && @@ -180,6 +189,31 @@ public function createImages($source, $filename, $cat=2, $processing=True, $loca list($file_info, $source) = $this->component->getFilesystem()->getResource($source); $isStream = true; } + elseif(\is_string($source) && ($local_source || \strpos($this->component->getFilesystem()->getFilesystem(), 'local') !== false)) + { + // The path is pointing to the local filesystem + $source = Path::clean($source); + + if(!\file_exists($source)) + { + // Add root to the path + $source = JPATH_ROOT.\DIRECTORY_SEPARATOR.$source; + + $source = Path::clean($source); + } + } + else + { + // Destroy the IMGtools service + $this->component->delIMGtools(); + + // Debug info + $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_IMAGETYPE', $filename, $imagetype->typename)); + $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_IMAGETYPE', $filename, $imagetype->typename), 'error', 'jerror'); + $error = true; + + continue; + } // Read source image if(!$this->component->getIMGtools()->read($source, $isStream)) diff --git a/administrator/com_joomgallery/src/Service/FileManager/FileManagerInterface.php b/administrator/com_joomgallery/src/Service/FileManager/FileManagerInterface.php index b5e9e1cbb..357e6e302 100644 --- a/administrator/com_joomgallery/src/Service/FileManager/FileManagerInterface.php +++ b/administrator/com_joomgallery/src/Service/FileManager/FileManagerInterface.php @@ -34,12 +34,13 @@ interface FileManagerInterface * @param object|int|string $cat Object, ID or alias of the corresponding category (default: 2) * @param bool $processing True to create imagetypes by processing source (defualt: True) * @param bool $local_source True if the source is a file located in a local folder (default: True) + * @param array $skip List of imagetypes to skip creation (default: []) * * @return bool True on success, false otherwise * * @since 4.0.0 */ - public function createImages($source, $filename, $cat=2, $processing=True, $local_source=True): bool; + public function createImages($source, $filename, $cat=2, $processing=True, $local_source=True, $skip=[]): bool; /** * Deletion of image types From ef4e176d4befd7a8ec426a8ad9ea93efb1354725 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Wed, 1 Jan 2025 13:01:52 +0100 Subject: [PATCH 11/20] fix issue: Undefined variable $image --- site/com_joomgallery/tmpl/images/default.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/com_joomgallery/tmpl/images/default.php b/site/com_joomgallery/tmpl/images/default.php index 72fd2b31d..dc484adc8 100644 --- a/site/com_joomgallery/tmpl/images/default.php +++ b/site/com_joomgallery/tmpl/images/default.php @@ -155,7 +155,7 @@ - + escape($item->title); ?> From 69e016915822bd7aaa4cd251daa5ef856afe79f3 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Thu, 2 Jan 2025 21:17:32 +0100 Subject: [PATCH 12/20] fix issue with recursive folder creation --- .../src/Service/Filesystem/Filesystem.php | 53 ++++++++++++++++--- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php index c04e88daf..e6849b1ec 100644 --- a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php +++ b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php @@ -438,6 +438,7 @@ public function getFiles(string $path = '/', array $options = []): array * @param string $name The name * @param string $path The folder * @param boolean $override Should the folder being overridden when it exists (default: true) + * @param boolean $loop True, if we are in a recursive loop (default: false) * * @return string The folder name * @@ -445,7 +446,7 @@ public function getFiles(string $path = '/', array $options = []): array * @throws \Exception * @see AdapterInterface::createFolder() */ - public function createFolder(string $name, string $path, bool $override = true): string + public function createFolder(string $name, string $path, bool $override = true, bool $loop = false): string { $adapter = $this->getFilesystem(); $path = $this->cleanPath($this->adjustPath($path), '/'); @@ -456,14 +457,13 @@ public function createFolder(string $name, string $path, bool $override = true): } catch (FileNotFoundException $e) { - // Do nothing + // Folder not found; proceed to create it } - // Check if the file exists - if - (isset($file) && !$override) + if (isset($file) && !$override) { - throw new FileExistsException(); + // No need to create folders + throw new FileExistsException('Folder already exists: ' . $path . '/' . $name); } $object = new CMSObject(); @@ -475,12 +475,49 @@ public function createFolder(string $name, string $path, bool $override = true): $result = $this->app->triggerEvent('onContentBeforeSave', ['com_media.folder', $object, true, $object]); - if(in_array(false, $result, true)) + if(\in_array(false, $result, true)) { throw new \Exception($object->getError()); } - $object->name = $this->getAdapter($object->adapter)->createFolder($object->name, $object->path); + try + { + // Try to create folders recursively + $object->name = $this->getAdapter($object->adapter)->createFolder($object->name, $object->path); + } + catch (\Exception $e) + { + if(!$loop) + { + // If it doesnt work like that, try again by creating them one by one + $folders = \explode('/', trim($object->path, '/')); + $leading_slash = \strpos($object->path, '/') === 0; + $currentPath = $leading_slash ? '/' : ''; + + // Append the current folder to the folders array + $folders[] = $object->name; + + foreach($folders as $folder) + { + try + { + // Create each folder one by one + $object->name = $this->createFolder($folder, $currentPath, $override, true); + } + catch (FileExistsException $fee) + { + // Folder already exists; no action needed + } + + // Adjust the currently existing path + $currentPath .= ($currentPath === '/' ? '' : '/') . $folder; + } + } + else + { + throw new \Exception($e->getMessage()); + } + } $this->app->triggerEvent('onContentAfterSave', ['com_media.folder', $object, true, $object]); From 822fb7f55517fb9d756a7731696bfcedb7f1ef1c Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Fri, 3 Jan 2025 17:28:44 +0100 Subject: [PATCH 13/20] check adapter using ProviderManagerHelperTrait --- .../language/en-GB/com_joomgallery.ini | 2 +- .../src/Helper/FilesystemHelper.php | 28 +++++++++++++++++++ .../com_joomgallery/src/Helper/JoomHelper.php | 23 +++++++++++---- 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 administrator/com_joomgallery/src/Helper/FilesystemHelper.php diff --git a/administrator/com_joomgallery/language/en-GB/com_joomgallery.ini b/administrator/com_joomgallery/language/en-GB/com_joomgallery.ini index cedb9aadb..bb0a00c4d 100644 --- a/administrator/com_joomgallery/language/en-GB/com_joomgallery.ini +++ b/administrator/com_joomgallery/language/en-GB/com_joomgallery.ini @@ -787,7 +787,7 @@ COM_JOOMGALLERY_SERVICE_ERROR_CREATE_FILE="File could not be created." COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_A_DIRECTORY="Requested path is not a directory." COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND="No filesystem plugin available for the specified adapter (%s)." COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_ERROR="Filesystem plugin is reporting the following error: %s" -COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_PLUGIN_NOT_ENABLED="It seems that the required filesystem plugin (%s) is not installed or not enabled. Please install and enable the plugin here." +COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_PLUGIN_NOT_ENABLED="It seems that the required adapter %s of the filesystem plugin %s is not installed, not enabled or not configured properly. Please install and enable the plugin here and make sure that the adapter is working correctly." COM_JOOMGALLERY_SERVICE_MIGRATION_CHECK_TITLE="Check results" COM_JOOMGALLERY_SERVICE_MIGRATION_GENERAL_PRECHECK_DESC="General checks like log file, site state, ..." COM_JOOMGALLERY_SERVICE_MIGRATION_SOURCE_PRECHECK_DESC="Check source extension, directories and tables if they are compatible and existent." diff --git a/administrator/com_joomgallery/src/Helper/FilesystemHelper.php b/administrator/com_joomgallery/src/Helper/FilesystemHelper.php new file mode 100644 index 000000000..4f414a3da --- /dev/null +++ b/administrator/com_joomgallery/src/Helper/FilesystemHelper.php @@ -0,0 +1,28 @@ + ** +** @copyright 2008 - 2025 JoomGallery::ProjectTeam ** +** @license GNU General Public License version 3 or later ** +*****************************************************************************************/ + +namespace Joomgallery\Component\Joomgallery\Administrator\Helper; + +// No direct access +defined('_JEXEC') or die; + +use \Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; + +/** + * Helper for the filesystem adapters + * + * @static + * @package JoomGallery + * @since 4.0.0 + */ +class FilesystemHelper +{ + use ProviderManagerHelperTrait; +} diff --git a/administrator/com_joomgallery/src/Helper/JoomHelper.php b/administrator/com_joomgallery/src/Helper/JoomHelper.php index 38a5b00a6..511abd3b8 100644 --- a/administrator/com_joomgallery/src/Helper/JoomHelper.php +++ b/administrator/com_joomgallery/src/Helper/JoomHelper.php @@ -1061,6 +1061,9 @@ public static function fetchXML(string $uri): \SimpleXMLElement */ public static function checkFilesystems() { + // Load filesystem helper + $helper = new FilesystemHelper; + // Load all used filesystems from images table $db = Factory::getContainer()->get(DatabaseInterface::class); @@ -1073,15 +1076,25 @@ public static function checkFilesystems() $filesystems = $db->loadColumn(); // Loop through all found filesystems - foreach ($filesystems as $key => $filesystem) + foreach ($filesystems as $filesystem) { - // Get corresponding plugin name + // Get corresponding names $plugin_name = \explode('-', $filesystem, 2)[0]; $plugin_fullname = 'plg_filesystem_'.$plugin_name; + $adapter_name = \explode('-', $filesystem, 2)[1]; + + // Try to get the corresponding filesystem adapter + try + { + $adapter = $helper->getAdapter($filesystem); + } catch (\Exception $e) + { + $adapter = false; + } - if(!PluginHelper::isEnabled('filesystem', $plugin_name)) + if(!$adapter) { - // Plugin is not installed or not enabled. Show warning message. + // Plugin is not installed, not enabled or not correctly configured. Show warning message. $lang = Factory::getLanguage(); if(!$lang->getPaths($plugin_fullname)) @@ -1097,7 +1110,7 @@ public static function checkFilesystems() $plugins_url = Route::_('index.php?option=com_plugins&view=plugins&filter[folder]=filesystem'); $plugin_title = Text::_($plugin_fullname); - self::getComponent()->setWarning(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_PLUGIN_NOT_ENABLED', $plugin_title, $plugins_url)); + self::getComponent()->setWarning(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_PLUGIN_NOT_ENABLED', $adapter_name, $plugin_title, $plugins_url)); } } } From 034cafe7b52d154c0976ef6eff8612afba4f64bc Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Sat, 11 Jan 2025 19:39:46 +0100 Subject: [PATCH 14/20] jg_filesystem parameter not stored in category --- administrator/com_joomgallery/src/Model/CategoryModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/com_joomgallery/src/Model/CategoryModel.php b/administrator/com_joomgallery/src/Model/CategoryModel.php index fad1ed4a2..8141cf320 100644 --- a/administrator/com_joomgallery/src/Model/CategoryModel.php +++ b/administrator/com_joomgallery/src/Model/CategoryModel.php @@ -449,7 +449,7 @@ public function save($data) } // Check if category has subcategories (children) - if(!$this->getChildren($pk)) + if($this->getChildren($pk)) { $hasChildren = true; } From 72aee191e01c4f16d1fe38d86b379df045816e38 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Mon, 13 Jan 2025 16:49:50 +0100 Subject: [PATCH 15/20] fix issue: save adapter back to default --- .../src/Model/CategoryModel.php | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/administrator/com_joomgallery/src/Model/CategoryModel.php b/administrator/com_joomgallery/src/Model/CategoryModel.php index 8141cf320..235089826 100644 --- a/administrator/com_joomgallery/src/Model/CategoryModel.php +++ b/administrator/com_joomgallery/src/Model/CategoryModel.php @@ -366,15 +366,16 @@ public function delete(&$pks) */ public function save($data) { - $table = $this->getTable(); - $context = $this->option . '.' . $this->name; - $app = Factory::getApplication(); - $isNew = true; - $catMoved = false; - $isCopy = false; - $aliasChanged = false; - $hasChildren = false; - $hasImages = false; + $table = $this->getTable(); + $context = $this->option . '.' . $this->name; + $app = Factory::getApplication(); + $isNew = true; + $catMoved = false; + $isCopy = false; + $aliasChanged = false; + $hasChildren = false; + $hasImages = false; + $adapterChanged = false; $key = $table->getKeyName(); $pk = (isset($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); @@ -459,17 +460,24 @@ public function save($data) { $hasImages = true; } + + // Check if filesystem adapter has changed + $old_params = \json_decode($table->params); + if($old_params->{'jg_filesystem'} != $data['params']['jg_filesystem']) + { + $adapterChanged = true; + } } // Check that filesystem field content is allowed - if(\key_exists('jg_filesystem', $data['params']) && $data['params']['jg_filesystem'] != '' && $data['parent_id'] != 1) + if($adapterChanged && $data['parent_id'] != 1) { // Only allowed in toplevel categories $this->setError(Text::_('COM_JOOMGALLERY_ERROR_FILESYSTEM_ONLY_TOP_LEVEL_CAT')); return false; } - elseif(\key_exists('jg_filesystem', $data['params']) && $data['params']['jg_filesystem'] != '' && ($hasChildren || $hasImages)) + elseif($adapterChanged && ($hasChildren || $hasImages)) { // Only allowed if there are no images and no subcategories $this->setError(Text::_('COM_JOOMGALLERY_ERROR_FILESYSTEM_ONLY_EMPTY_CAT')); From b9beeeb1b5d996e808c693b1af5ef68ea633ae9c Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Wed, 15 Jan 2025 08:12:54 +0100 Subject: [PATCH 16/20] Cat: Filesystem debug info when nothing changed --- .../src/Service/FileManager/FileManager.php | 28 ++++++++++++++----- .../src/Service/Filesystem/Filesystem.php | 1 - 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php index 128a45e06..dd100bcbc 100644 --- a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php +++ b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php @@ -736,7 +736,8 @@ public function renameImages($img, $filename): bool public function createCategory($foldername, $parent=1): bool { // Loop through all imagetypes - $error = false; + $error = false; + $catsCreated = 0; foreach($this->imagetypes as $key => $imagetype) { // Category path @@ -749,14 +750,21 @@ public function createCategory($foldername, $parent=1): bool } catch(\FileExistsException $e) { - // Do nothing + // Category already exists. Do nothing. } catch(\Exception $e) { // Debug info - $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_CATEGORY', $foldername)); - $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_CATEGORY', $foldername), 'error', 'jerror'); - $error = true; + if(\strpos(\strtolower($e->getMessage()), 'file exists') !== false) + { + // Category already exists. Do nothing. + } + else + { + $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_CATEGORY', $foldername)); + $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_CATEGORY', $foldername), 'error', 'jerror'); + $error = true; + } continue; } @@ -770,6 +778,9 @@ public function createCategory($foldername, $parent=1): bool continue; } + + // Count up the # of created categories + $catsCreated = $catsCreated + 1; } if($error) @@ -780,8 +791,11 @@ public function createCategory($foldername, $parent=1): bool return false; } - // Debug info - $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_SUCCESS_CREATE_CATEGORY', $foldername)); + if($catsCreated > 0) + { + // Debug info + $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_SUCCESS_CREATE_CATEGORY', $foldername)); + } return true; } diff --git a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php index e6849b1ec..b2f8f6b16 100644 --- a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php +++ b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php @@ -309,7 +309,6 @@ public function getFile(string $path = '/', array $options = []): \stdClass } catch (\Exception $e) { - $msg = $e->getMessage(); if(\strpos(\strtolower($e->getMessage()), 'account')) { $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND'), 'error', 'jerror'); From f43ee5c996a2df3f5303b2bfcc78bfae7b1f12bc Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Wed, 15 Jan 2025 08:44:11 +0100 Subject: [PATCH 17/20] fix issue with "Use real path" --- administrator/com_joomgallery/src/Helper/JoomHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/com_joomgallery/src/Helper/JoomHelper.php b/administrator/com_joomgallery/src/Helper/JoomHelper.php index 511abd3b8..09302c032 100644 --- a/administrator/com_joomgallery/src/Helper/JoomHelper.php +++ b/administrator/com_joomgallery/src/Helper/JoomHelper.php @@ -506,7 +506,7 @@ public static function getImg($img, $type, $url=true, $root=true) // Create file manager service $manager = self::getService('FileManager', array($img->catid)); // Create file manager service - $filesystem = self::getService('Filesystem'); + $filesystem = self::getService('Filesystem', array($img->filesystem)); // Real URL // Example: https://www.example.org/images/joomgallery/orig/test.jpg From 11a5f7bf7fef9919958c721b2fe7afab9eee2593 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Sat, 18 Jan 2025 11:44:33 +0100 Subject: [PATCH 18/20] adjust error handling Some filesystem adapter do not throw proper exceptions as the Joomla Core plugin (local - images) is doing it. This changes tries to recognise various exceptions and act accordingly. --- .../src/Service/FileManager/FileManager.php | 57 +++++++++++++------ .../src/Service/Filesystem/Filesystem.php | 17 +++++- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php index dd100bcbc..223df4294 100644 --- a/administrator/com_joomgallery/src/Service/FileManager/FileManager.php +++ b/administrator/com_joomgallery/src/Service/FileManager/FileManager.php @@ -306,12 +306,12 @@ public function createImages($source, $filename, $cat=2, $processing=True, $loca { $res = $this->component->getFilesystem()->createFolder(\basename($folder), \dirname($folder), false); } - catch(FileExistsException $e) + catch (FileExistsException $e) { // Folder already exists. $res = true; } - catch(\Exception $e) + catch (\Exception $e) { // Destroy the IMGtools service $this->component->delIMGtools(); @@ -378,7 +378,6 @@ public function createImages($source, $filename, $cat=2, $processing=True, $loca $error = true; continue; - } catch (InvalidPathException $e) { @@ -399,15 +398,23 @@ public function createImages($source, $filename, $cat=2, $processing=True, $loca catch (\Exception $e) { // Any other error during file creation + if(\strpos(\strtolower($e->getMessage()), 'file exists') !== false) + { + // Debug info + $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_ERROR_FILE_ALREADY_EXISTING', $filename)); + $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_ERROR_FILE_ALREADY_EXISTING', $filename), 'error', 'jerror'); + } + else + { + // Debug info + $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_ERROR', $e->getMessage())); + $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_IMAGETYPE', $filename, $imagetype->typename)); + $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_ERROR', $e->getMessage()), 'error', 'jerror'); + $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_IMAGETYPE', $filename, $imagetype->typename), 'error', 'jerror'); + } // Destroy the IMGtools service $this->component->delIMGtools(); - - // Debug info - $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_ERROR', $e->getMessage())); - $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_IMAGETYPE', $filename, $imagetype->typename)); - $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_ERROR', $e->getMessage()), 'error', 'jerror'); - $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_IMAGETYPE', $filename, $imagetype->typename), 'error', 'jerror'); $error = true; continue; @@ -459,16 +466,23 @@ public function deleteImages($img): bool } catch (FileNotFoundException $e) { - // Do nothing + // File already missing. Do nothing. } catch (\Exception $e) { - // Deletion failed - $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_DELETE_IMAGETYPE', \basename($file), $imagetype->typename)); - $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_DELETE_IMAGETYPE', \basename($file), $imagetype->typename), 'error', 'jerror'); - $error = true; + if(\strpos(\strtolower($e->getMessage()), 'not found') !== false || \strpos(\strtolower($e->getMessage()), 'no such file') !== false) + { + // File already missing. Do nothing. + } + else + { + // Deletion failed + $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_DELETE_IMAGETYPE', \basename($file), $imagetype->typename)); + $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_DELETE_IMAGETYPE', \basename($file), $imagetype->typename), 'error', 'jerror'); + $error = true; - continue; + continue; + } } // Deletion successful @@ -520,8 +534,17 @@ public function checkImages($img): array } catch (\Exception $e) { - $this->component->addDebug($e->getMessage()); - $this->component->addLog($e->getMessage(), 'error', 'jerror'); + if(\strpos(\strtolower($e->getMessage()), 'not found') !== false || \strpos(\strtolower($e->getMessage()), 'no such file') !== false) + { + // File not found + $this->component->addDebug(Text::sprintf('COM_JOOMGALLERY_ERROR_FILE_NOT_EXISTING').', '.\basename($file).' ('.$imagetype->typename.')'); + $this->component->addLog(Text::sprintf('COM_JOOMGALLERY_ERROR_FILE_NOT_EXISTING').', '.\basename($file).' ('.$imagetype->typename.')', 'error', 'jerror'); + } + else + { + $this->component->addDebug($e->getMessage()); + $this->component->addLog($e->getMessage(), 'error', 'jerror'); + } return false; } diff --git a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php index b2f8f6b16..c66144482 100644 --- a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php +++ b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php @@ -314,7 +314,7 @@ public function getFile(string $path = '/', array $options = []): \stdClass $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND'), 'error', 'jerror'); throw new \Exception(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter)); } - elseif(\strpos(\strtolower($e->getMessage()), 'no such file') !== false) + elseif(\strpos(\strtolower($e->getMessage()), 'not found') !== false || \strpos(\strtolower($e->getMessage()), 'no such file') !== false) { $this->component->addLog('FileNotFoundException in function getFile in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'jerror'); $this->component->addLog('$adapter: ' . $adapter, 'warning', 'jerror'); @@ -459,7 +459,7 @@ public function createFolder(string $name, string $path, bool $override = true, // Folder not found; proceed to create it } - if (isset($file) && !$override) + if(isset($file) && !$override) { // No need to create folders throw new FileExistsException('Folder already exists: ' . $path . '/' . $name); @@ -507,6 +507,19 @@ public function createFolder(string $name, string $path, bool $override = true, { // Folder already exists; no action needed } + catch (\Exception $e) + { + if(\strpos(\strtolower($e->getMessage()), 'file exists') !== false) + { + // Handle the case where the adapter does not throw a proper FileExistsException + // Folder already exists; no action needed + } + else + { + // Rethrow other exceptions for proper error handling + throw new \Exception($e->getMessage(), $e->getCode(), $e); + } + } // Adjust the currently existing path $currentPath .= ($currentPath === '/' ? '' : '/') . $folder; From 24a2dc1f3152d13c28ead94a04ddafaf904a4dca Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Sat, 18 Jan 2025 12:51:12 +0100 Subject: [PATCH 19/20] fix issue if file is not found --- .../com_joomgallery/src/Helper/JoomHelper.php | 38 +++++++++++-- .../Exception/AdapterNotFoundException.php | 23 ++++++++ .../src/Service/Filesystem/Filesystem.php | 53 +++++++++++++++++-- 3 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 administrator/com_joomgallery/src/Service/Filesystem/Exception/AdapterNotFoundException.php diff --git a/administrator/com_joomgallery/src/Helper/JoomHelper.php b/administrator/com_joomgallery/src/Helper/JoomHelper.php index 09302c032..6a6436386 100644 --- a/administrator/com_joomgallery/src/Helper/JoomHelper.php +++ b/administrator/com_joomgallery/src/Helper/JoomHelper.php @@ -21,9 +21,9 @@ use \Joomla\CMS\Access\Access; use \Joomla\CMS\Filesystem\Path; use \Joomla\CMS\Http\HttpFactory; -use \Joomla\CMS\Plugin\PluginHelper; use \Joomla\CMS\Language\Multilanguage; use \Joomla\Database\DatabaseInterface; +use \Joomla\Component\Media\Administrator\Exception\FileNotFoundException; /** * JoomGallery Helper for the Backend @@ -499,7 +499,7 @@ public static function getImg($img, $type, $url=true, $root=true) { // Joomgallery internal URL // Example: https://www.example.org/index.php?option=com_joomgallery&controller=images&view=image&format=raw&type=orig&id=3&catid=1 - return Route::_(self::getViewRoute('image', $img->id, $img->catid, 'raw', $type)); + return Route::_(self::getViewRoute('image', $img->id, $img->catid, 'raw', $type)); } else { @@ -510,7 +510,14 @@ public static function getImg($img, $type, $url=true, $root=true) // Real URL // Example: https://www.example.org/images/joomgallery/orig/test.jpg - return $filesystem->getUrl($manager->getImgPath($img, $type)); + try + { + return $filesystem->getUrl($manager->getImgPath($img, $type)); + } + catch (FileNotFoundException $e) + { + return self::getImgZero($type, $url, $root); + } } } else @@ -1115,6 +1122,31 @@ public static function checkFilesystems() } } + /** + * Method to get the imagetype from an image path. + * + * @param string $path The image path. + * + * @return string + * + * @since 4.0.0 + */ + public static function getImagetypeFromPath(string $path) + { + $path = Path::clean($path, '/'); + $imagetypes = JoomHelper::getRecords('imagetypes'); + + foreach($imagetypes as $imagetype) + { + if(\strpos($path, $imagetype->path) !== false) + { + return $imagetype->typename; + } + } + + return 'thumbnail'; + } + /** * Returns a list of all available access action names available * diff --git a/administrator/com_joomgallery/src/Service/Filesystem/Exception/AdapterNotFoundException.php b/administrator/com_joomgallery/src/Service/Filesystem/Exception/AdapterNotFoundException.php new file mode 100644 index 000000000..7a501883e --- /dev/null +++ b/administrator/com_joomgallery/src/Service/Filesystem/Exception/AdapterNotFoundException.php @@ -0,0 +1,23 @@ + ** +** @copyright 2008 - 2025 JoomGallery::ProjectTeam ** +** @license GNU General Public License version 3 or later ** +*****************************************************************************************/ + +namespace Joomgallery\Component\Joomgallery\Administrator\Service\Filesystem\Exception; + +// No direct access +\defined('_JEXEC') or die; + +/** + * Media adapter not found exception. + * + * @since 4.0.0 + */ +class AdapterNotFoundException extends \Exception +{ +} diff --git a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php index c66144482..d51d902b0 100644 --- a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php +++ b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php @@ -14,7 +14,6 @@ \defined('_JEXEC') or die; use \Joomla\CMS\Factory; -use \Joomla\CMS\Log\Log; use \Joomla\CMS\Language\Text; use \Joomla\CMS\Object\CMSObject; use \Joomla\CMS\Plugin\PluginHelper; @@ -32,7 +31,9 @@ use \Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; use \Joomgallery\Component\Joomgallery\Administrator\Service\Filesystem\FilesystemInterface; +use \Joomgallery\Component\Joomgallery\Administrator\Service\Filesystem\Exception\AdapterNotFoundException; use \Joomgallery\Component\Joomgallery\Administrator\Extension\ServiceTrait; +use Joomgallery\Component\Joomgallery\Administrator\Helper\JoomHelper; /** * Filesystem Base Class @@ -311,8 +312,8 @@ public function getFile(string $path = '/', array $options = []): \stdClass { if(\strpos(\strtolower($e->getMessage()), 'account')) { - $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND'), 'error', 'jerror'); - throw new \Exception(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter)); + $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter), 'error', 'jerror'); + throw new AdapterNotFoundException(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter)); } elseif(\strpos(\strtolower($e->getMessage()), 'not found') !== false || \strpos(\strtolower($e->getMessage()), 'no such file') !== false) { @@ -770,7 +771,37 @@ public function getUrl(string $path): string throw new InvalidPathException(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE')); } - $url = $this->getAdapter($adapter)->getUrl($path); + try + { + $url = $this->getAdapter($adapter)->getUrl($path); + } + catch (FileNotFoundException $e) + { + $this->component->addLog('FileNotFoundException in function getUrl in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'jerror'); + $this->component->addLog('$adapter: ' . $adapter, 'warning', 'jerror'); + $this->component->addLog('$path: ' . $path, 'warning', 'jerror'); + throw new FileNotFoundException(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND')); + } + catch (\Exception $e) + { + if(\strpos(\strtolower($e->getMessage()), 'account')) + { + $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter), 'error', 'jerror'); + throw new AdapterNotFoundException(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter)); + } + elseif(\strpos(\strtolower($e->getMessage()), 'not found') !== false || \strpos(\strtolower($e->getMessage()), 'no such file') !== false) + { + $this->component->addLog('FileNotFoundException in function getUrl in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'jerror'); + $this->component->addLog('$adapter: ' . $adapter, 'warning', 'jerror'); + $this->component->addLog('$path: ' . $path, 'warning', 'jerror'); + throw new FileNotFoundException(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND')); + } + else + { + $this->component->addLog($e->getMessage(), 'error', 'jerror'); + throw new \Exception($e->getMessage()); + } + } $event = new FetchMediaItemUrlEvent('onFetchMediaFileUrl', ['adapter' => $adapter, 'path' => $path, 'url' => $url]); $this->app->getDispatcher()->dispatch($event->getName(), $event); @@ -813,8 +844,20 @@ public function getResource(string $path): array $adapter = $this->getFilesystem(); $path = $this->cleanPath($this->adjustPath($path), '/'); + try + { + $file = $this->getFile($path); + } + catch (FileNotFoundException $e) + { + # Get the 'no-image' instead + $adapter = 'local-images'; + $type = JoomHelper::getImagetypeFromPath($path); + $path = $this->cleanPath($this->adjustPath(JoomHelper::getImgZero($type, false, false)), '/'); + $file = $this->getFile($path); + } + // Check if it is an allowed file - $file = $this->getFile($path); if($file->type != 'file' || !$this->isAllowedFile($file->path)) { $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'jerror'); From cde9653d844ffc1b9ee40c9e824ba2026f99c089 Mon Sep 17 00:00:00 2001 From: 19leunam93 Date: Sat, 18 Jan 2025 12:51:51 +0100 Subject: [PATCH 20/20] different log file for filesystem errors --- .../src/Service/Filesystem/Filesystem.php | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php index d51d902b0..b7a217023 100644 --- a/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php +++ b/administrator/com_joomgallery/src/Service/Filesystem/Filesystem.php @@ -220,7 +220,7 @@ public function createIndexHtml(string $path): string } else { - $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_FILE'), 'error', 'jerror'); + $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_FILE'), 'error', 'filesystem'); throw new InvalidPathException(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_CREATE_FILE')); } } @@ -303,28 +303,28 @@ public function getFile(string $path = '/', array $options = []): \stdClass } catch (FileNotFoundException $e) { - $this->component->addLog('FileNotFoundException in function getFile in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'jerror'); - $this->component->addLog('$adapter: ' . $adapter, 'warning', 'jerror'); - $this->component->addLog('$path: ' . $path, 'warning', 'jerror'); + $this->component->addLog('FileNotFoundException in function getFile in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'filesystem'); + $this->component->addLog('$adapter: ' . $adapter, 'warning', 'filesystem'); + $this->component->addLog('$path: ' . $path, 'warning', 'filesystem'); throw new FileNotFoundException(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND')); } catch (\Exception $e) { if(\strpos(\strtolower($e->getMessage()), 'account')) { - $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter), 'error', 'jerror'); + $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter), 'error', 'filesystem'); throw new AdapterNotFoundException(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter)); } elseif(\strpos(\strtolower($e->getMessage()), 'not found') !== false || \strpos(\strtolower($e->getMessage()), 'no such file') !== false) { - $this->component->addLog('FileNotFoundException in function getFile in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'jerror'); - $this->component->addLog('$adapter: ' . $adapter, 'warning', 'jerror'); - $this->component->addLog('$path: ' . $path, 'warning', 'jerror'); + $this->component->addLog('FileNotFoundException in function getFile in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'filesystem'); + $this->component->addLog('$adapter: ' . $adapter, 'warning', 'filesystem'); + $this->component->addLog('$path: ' . $path, 'warning', 'filesystem'); throw new FileNotFoundException(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND')); } else { - $this->component->addLog($e->getMessage(), 'error', 'jerror'); + $this->component->addLog($e->getMessage(), 'error', 'filesystem'); throw new \Exception($e->getMessage()); } } @@ -332,7 +332,7 @@ public function getFile(string $path = '/', array $options = []): \stdClass // Check if it is an allowed file if($file->type == 'file' && !$this->isAllowedFile($file->path)) { - $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'jerror'); + $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'filesystem'); throw new InvalidPathException(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE')); } @@ -626,7 +626,7 @@ public function updateFile(string $name, string $path, $data) // Check if it is an allowed file if(!$this->isAllowedFile($path . '/' . $name)) { - $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'jerror'); + $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'filesystem'); throw new InvalidPathException(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE')); } @@ -646,7 +646,7 @@ public function updateFile(string $name, string $path, $data) if(in_array(false, $result, true)) { - $this->component->addLog($object->getError(), 'error', 'jerror'); + $this->component->addLog($object->getError(), 'error', 'filesystem'); throw new \Exception($object->getError()); } @@ -677,7 +677,7 @@ public function delete(string $path) // Check if it is an allowed file if($file->type == 'file' && !$this->isAllowedFile($file->path)) { - $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'jerror'); + $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'filesystem'); throw new InvalidPathException(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE')); } @@ -695,7 +695,7 @@ public function delete(string $path) if(in_array(false, $result, true)) { - $this->component->addLog($object->getError(), 'error', 'jerror'); + $this->component->addLog($object->getError(), 'error', 'filesystem'); throw new \Exception($object->getError()); } @@ -767,7 +767,7 @@ public function getUrl(string $path): string // Check if it is an allowed file if(!$this->isAllowedFile($path)) { - $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'jerror'); + $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'filesystem'); throw new InvalidPathException(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE')); } @@ -777,28 +777,28 @@ public function getUrl(string $path): string } catch (FileNotFoundException $e) { - $this->component->addLog('FileNotFoundException in function getUrl in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'jerror'); - $this->component->addLog('$adapter: ' . $adapter, 'warning', 'jerror'); - $this->component->addLog('$path: ' . $path, 'warning', 'jerror'); + $this->component->addLog('FileNotFoundException in function getUrl in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'filesystem'); + $this->component->addLog('$adapter: ' . $adapter, 'warning', 'filesystem'); + $this->component->addLog('$path: ' . $path, 'warning', 'filesystem'); throw new FileNotFoundException(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND')); } catch (\Exception $e) { if(\strpos(\strtolower($e->getMessage()), 'account')) { - $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter), 'error', 'jerror'); + $this->component->addLog(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter), 'error', 'filesystem'); throw new AdapterNotFoundException(Text::sprintf('COM_JOOMGALLERY_SERVICE_ERROR_FILESYSTEM_NOT_FOUND', $adapter)); } elseif(\strpos(\strtolower($e->getMessage()), 'not found') !== false || \strpos(\strtolower($e->getMessage()), 'no such file') !== false) { - $this->component->addLog('FileNotFoundException in function getUrl in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'jerror'); - $this->component->addLog('$adapter: ' . $adapter, 'warning', 'jerror'); - $this->component->addLog('$path: ' . $path, 'warning', 'jerror'); + $this->component->addLog('FileNotFoundException in function getUrl in Filesystem.php: ' . Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND'), 'warning', 'filesystem'); + $this->component->addLog('$adapter: ' . $adapter, 'warning', 'filesystem'); + $this->component->addLog('$path: ' . $path, 'warning', 'filesystem'); throw new FileNotFoundException(Text::_('COM_JOOMGALLERY_SERVICE_ERROR_FILENOTFOUND')); } else { - $this->component->addLog($e->getMessage(), 'error', 'jerror'); + $this->component->addLog($e->getMessage(), 'error', 'filesystem'); throw new \Exception($e->getMessage()); } } @@ -860,7 +860,7 @@ public function getResource(string $path): array // Check if it is an allowed file if($file->type != 'file' || !$this->isAllowedFile($file->path)) { - $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'jerror'); + $this->component->addLog(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE'), 'error', 'filesystem'); throw new InvalidPathException(Text::_('COM_JOOMGALLERY_ERROR_UNSUPPORTED_FILE_TYPE')); }