From b3e7977b55492a968289dd9dcc5f207b1e25d9a8 Mon Sep 17 00:00:00 2001 From: Jim Graham Date: Thu, 28 Sep 2023 10:59:55 -0400 Subject: [PATCH] Improve file system error feedback Provides more action-specific messaging when a file/folder action can not be taken --- core/lexicon/en/file.inc.php | 16 ++++++++++-- .../src/Revolution/Sources/modMediaSource.php | 26 ++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/core/lexicon/en/file.inc.php b/core/lexicon/en/file.inc.php index ac8ad0a7648..5efce137955 100644 --- a/core/lexicon/en/file.inc.php +++ b/core/lexicon/en/file.inc.php @@ -14,10 +14,13 @@ $_lang['file_edit'] = 'Edit File'; $_lang['file_open'] = 'Open File Url'; $_lang['file_err_ae'] = 'File %s already exists'; -$_lang['file_err_create'] = 'An unknown error occurred while trying to create the file.'; +$_lang['file_err_create_general_exception'] = 'An unknown error occurred while trying to create the file. Please check the MODX and/or server error logs for more information.'; +$_lang['file_err_create_write_exception'] = 'The file could not be created. Please verify you have write permissions for its target directory and try again.'; $_lang['file_err_ext_not_allowed'] = 'File extension `[[+ext]]` is not permitted.'; $_lang['file_err_filter'] = 'No files match the specified filter.'; $_lang['file_err_invalid'] = 'The file is not a regular file and cannot be deleted.'; +$_lang['file_err_move_general_exception'] = 'An unknown error occurred while trying to move the file. Please check the MODX and/or server error logs for more information.'; +$_lang['file_err_move_write_exception'] = 'The file could not be moved. Please verify you have write permissions for both the file and its target directory and try again.'; $_lang['file_err_nf'] = 'File does not exist!'; $_lang['file_err_ns'] = 'Please specify a valid file.'; $_lang['file_err_open'] = 'Cannot open file: '; @@ -25,6 +28,8 @@ $_lang['file_err_remove'] = 'MODX failed to delete the file. Please make sure your permissions are set correctly.'; $_lang['file_err_too_large'] = 'Uploaded file is too large at [[+size]] bytes. Please ensure your files are less than [[+allowed]] bytes.'; $_lang['file_err_unzip'] = 'Unzip Failed!'; +$_lang['file_err_update_general_exception'] = 'An unknown system error occurred while trying to update this file. Please check the MODX and/or server error logs for more information.'; +$_lang['file_err_update_write_exception'] = 'The file could not be updated. Please verify you have write permissions for it and try again.'; $_lang['file_err_upload'] = 'An error occurred while trying to upload the files.'; $_lang['file_extensions'] = 'File Extensions'; $_lang['file_folder_path'] = 'Path'; @@ -40,9 +45,12 @@ $_lang['file_folder_err_ae'] = 'A directory already exists with that name in that location.'; $_lang['file_folder_err_create'] = 'An unknown error occurred while trying to create the directory.'; $_lang['file_folder_err_invalid'] = 'The specified directory is not a directory.'; +$_lang['file_folder_err_move_general_exception'] = 'An unknown error occurred while trying to move the directory. Please check the MODX and/or server error logs for more information.'; +$_lang['file_folder_err_move_write_exception'] = 'The directory could not be moved. Please verify you have write permissions for both this directory and its target directory and try again.'; $_lang['file_folder_err_ns'] = 'Please specify a valid directory.'; $_lang['file_folder_err_ns_name'] = 'Please specify a valid name for the directory.'; -$_lang['file_folder_err_rename'] = 'An unknown error occurred while trying to rename the directory.'; +$_lang['file_folder_err_rename_general_exception'] = 'An unknown error occurred while trying to rename the directory. Please check the MODX and/or server error logs for more information.'; +$_lang['file_folder_err_rename_write_exception'] = 'The directory could not be renamed. Please verify you have write permissions for it and try again.'; $_lang['file_folder_err_rename_protected'] = 'Renaming the protected system directory is not permitted.'; $_lang['file_folder_err_remove'] = 'An error occurred while trying to delete the directory.'; $_lang['file_folder_err_remove_protected'] = 'Deleting the protected system directory is not permitted.'; @@ -115,3 +123,7 @@ $_lang['upload.clear_list.notpermitted'] = 'Delete not permitted only'; $_lang['upload.msg.title.error'] = 'Error'; $_lang['upload.upload.success'] = 'Upload successful'; + +/** Deprecated keys */ +$_lang['file_err_create'] = $_lang['file_err_create_general_exception']; +$_lang['file_folder_err_rename'] = $_lang['file_folder_err_rename_general_exception']; diff --git a/core/src/Revolution/Sources/modMediaSource.php b/core/src/Revolution/Sources/modMediaSource.php index e44f8bcee15..53fb8601606 100644 --- a/core/src/Revolution/Sources/modMediaSource.php +++ b/core/src/Revolution/Sources/modMediaSource.php @@ -726,7 +726,11 @@ public function createObject($path, $name, $content) try { $this->filesystem->write($path, $content, $config); } catch (FilesystemException | UnableToWriteFile $e) { - $this->addError('name', $this->xpdo->lexicon('file_err_create')); + $messageKey = $e instanceof UnableToWriteFile + ? 'file_err_create_write_exception' + : 'file_err_create_general_exception' + ; + $this->addError('name', $this->xpdo->lexicon($messageKey)); $this->xpdo->log(modX::LOG_LEVEL_ERROR, $e->getMessage()); return false; } @@ -812,7 +816,13 @@ public function moveObject($from, $to, $point = 'append', $to_source = 0) try { $this->filesystem->move($path, $newPath); } catch (FilesystemException | UnableToMoveFile $e) { - $this->addError('from', $this->xpdo->lexicon('file_err_rename')); + // $this->addError('from', $this->xpdo->lexicon('file_err_rename')); + $prefix = $mimeType === 'directory' ? 'file_folder_' : 'file_' ; + $messageKey = $e instanceof UnableToMoveFile + ? $prefix . 'err_move_write_exception' + : $prefix . 'err_move_write_general' + ; + $this->addError('name', $this->xpdo->lexicon($messageKey)); $this->xpdo->log(modX::LOG_LEVEL_ERROR, $e->getMessage()); return false; } @@ -960,7 +970,11 @@ public function renameContainer($oldPath, $newName) try { $this->filesystem->move($oldPath, $newPath); } catch (FilesystemException | UnableToMoveFile $e) { - $this->addError('name', $this->xpdo->lexicon('file_folder_err_rename')); + $messageKey = $e instanceof UnableToMoveFile + ? 'file_folder_err_rename_write_exception' + : 'file_folder_err_rename_general_exception' + ; + $this->addError('name', $this->xpdo->lexicon($messageKey)); $this->xpdo->log(modX::LOG_LEVEL_ERROR, $e->getMessage()); return false; } @@ -1071,7 +1085,11 @@ public function updateObject($path, $content) try { $this->filesystem->write($path, $content, $config); } catch (FilesystemException | UnableToWriteFile $e) { - $this->addError('name', $this->xpdo->lexicon('file_folder_err_update')); + $messageKey = $e instanceof UnableToWriteFile + ? 'file_err_update_write_exception' + : 'file_err_update_write_general' + ; + $this->addError('name', $this->xpdo->lexicon($messageKey)); $this->xpdo->log(modX::LOG_LEVEL_ERROR, $e->getMessage()); return false; }