From 45dab6b10c042ccb1b8bc29212db7dd043a52f5b Mon Sep 17 00:00:00 2001 From: Kestin Goforth Date: Thu, 4 Aug 2022 05:09:16 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Better=20error=20handling=20for?= =?UTF-8?q?=20uploading=20and=20removing=20files=20and=20folders=20(#4588)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Notify user if file or folder removal fails * Improve error messages for failed file or folder removal * Notify user if file is too large to upload * Improve notification for failed file and folder uploads --- src/octoprint/filemanager/storage.py | 2 +- src/octoprint/server/api/files.py | 55 +++++++++++++---- .../static/js/app/viewmodels/files.js | 60 +++++++++++++++++-- src/octoprint/templates/dialogs/files.jinja2 | 3 + tests/filemanager/test_localstorage.py | 4 +- 5 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/octoprint/filemanager/storage.py b/src/octoprint/filemanager/storage.py index 007685d93..f17582de9 100644 --- a/src/octoprint/filemanager/storage.py +++ b/src/octoprint/filemanager/storage.py @@ -740,7 +740,7 @@ class LocalFileStorage(StorageInterface): ): raise StorageError( f"{destination_name} does already exist in {destination_path}", - code=StorageError.INVALID_DESTINATION, + code=StorageError.ALREADY_EXISTS, ) source_meta = self._get_metadata_entry(source_path, source_name) diff --git a/src/octoprint/server/api/files.py b/src/octoprint/server/api/files.py index 35c8b4da9..511301424 100644 --- a/src/octoprint/server/api/files.py +++ b/src/octoprint/server/api/files.py @@ -18,6 +18,7 @@ import octoprint.slicing from octoprint.access.permissions import Permissions from octoprint.events import Events from octoprint.filemanager.destinations import FileDestinations +from octoprint.filemanager.storage import StorageError from octoprint.server import ( NO_CONTENT, current_user, @@ -660,11 +661,8 @@ def uploadGcodeFile(target): allow_overwrite=True, display=canonFilename, ) - except octoprint.filemanager.storage.StorageError as e: - if e.code == octoprint.filemanager.storage.StorageError.INVALID_FILE: - abort(415, description="Could not upload file, invalid type") - else: - abort(500, description="Could not upload file") + except (OSError, StorageError) as e: + _abortWithException(e) else: filename = fileProcessingFinished( added_file, @@ -776,11 +774,8 @@ def uploadGcodeFile(target): added_folder = fileManager.add_folder( target, futureFullPath, display=canonName ) - except octoprint.filemanager.storage.StorageError as e: - if e.code == octoprint.filemanager.storage.StorageError.INVALID_DIRECTORY: - abort(400, description="Could not create folder, invalid directory") - else: - abort(500, description="Could not create folder") + except (OSError, StorageError) as e: + _abortWithException(e) location = url_for( ".readGcodeFile", @@ -1235,7 +1230,10 @@ def deleteGcodeFile(filename, target): if target == FileDestinations.SDCARD: printer.delete_sd_file(filename, tags={"source:api", "api:files.sd"}) else: - fileManager.remove_file(target, filename) + try: + fileManager.remove_file(target, filename) + except (OSError, StorageError) as e: + _abortWithException(e) elif _verifyFolderExists(target, filename): if _isBusy(target, filename): @@ -1254,11 +1252,44 @@ def deleteGcodeFile(filename, target): printer.unselect_file() # delete it - fileManager.remove_folder(target, filename, recursive=True) + try: + fileManager.remove_folder(target, filename, recursive=True) + except (OSError, StorageError) as e: + _abortWithException(e) return NO_CONTENT +def _abortWithException(error): + if type(error) is StorageError: + logging.getLogger(__name__).error( + f"{error.message}: {error.code}", exc_info=error.cause + ) + if error.code == StorageError.INVALID_DIRECTORY: + abort(400, description="Could not create folder, invalid directory") + elif error.code == StorageError.INVALID_FILE: + abort(415, description="Could not upload file, invalid type") + elif error.code == StorageError.INVALID_SOURCE: + abort(404, description="Source path does not exist, invalid source") + elif error.code == StorageError.INVALID_DESTINATION: + abort(400, description="Destination is invalid") + elif error.code == StorageError.DOES_NOT_EXIST: + abort(404, description="Does not exit") + elif error.code == StorageError.ALREADY_EXISTS: + abort(409, description="File or folder already exists") + elif error.code == StorageError.SOURCE_EQUALS_DESTINATION: + abort(400, description="Source and destination are the same folder") + elif error.code == StorageError.NOT_EMPTY: + abort(409, description="Folder is not empty") + elif error.code == StorageError.UNKNOWN: + abort(500, description=str(error.cause).split(":")[0]) + else: + abort(500, description=error.message) + else: + logging.getLogger(__name__).exception(error) + abort(500, description=str(error).split(":")[0]) + + def _getCurrentFile(): currentJob = printer.get_current_job() if ( diff --git a/src/octoprint/static/js/app/viewmodels/files.js b/src/octoprint/static/js/app/viewmodels/files.js index 637a56c0b..b0d4c4f15 100644 --- a/src/octoprint/static/js/app/viewmodels/files.js +++ b/src/octoprint/static/js/app/viewmodels/files.js @@ -888,8 +888,27 @@ $(function () { deferred.reject(); }); }) - .fail(function () { + .fail(function (data) { deferred.reject(); + + // Notify user + var error = + "

" + + gettext( + "Could not remove entry. Please check octoprint.log for possible reasons." + ) + + "

"; + if (data.responseJSON && data.responseJSON.error) { + error += pnotifyAdditionalInfo( + "
" + _.escape(data.responseJSON.error) + "
" + ); + } + new PNotify({ + title: gettext("Failed to remove entry"), + text: error, + type: "error", + hide: false + }); }); return deferred.promise().always(function () { @@ -1584,6 +1603,7 @@ $(function () { self._handleUploadAdd = function (e, data) { var file = data.files[0]; var path = self.currentPath(); + var fileSizeTooBig = file.size > self.freeSpace(); var formData = {}; if (path !== "") { @@ -1600,10 +1620,13 @@ $(function () { name: file.name }) ); + $("p, form", self.uploadExistsDialog).toggle(!fileSizeTooBig); + $("span", self.uploadExistsDialog).toggle(fileSizeTooBig); $("input", self.uploadExistsDialog) .val("") .prop("placeholder", response.suggestion); $("a.upload-rename", self.uploadExistsDialog) + .toggle(!fileSizeTooBig) .prop("disabled", false) .off("click") .on("click", function () { @@ -1659,8 +1682,35 @@ $(function () { } }); } else { - data.formData = formData; - data.submit(); + if (fileSizeTooBig) { + var error = + "

" + + gettext( + "Could not upload the file. There is not enough disk space remaining." + ) + + "

"; + + error += + "
" +
+                        _.sprintf(gettext("Free Space: %(freespace)s"), {
+                            freespace: self.freeSpaceString()
+                        }) +
+                        "
" + + _.sprintf(gettext("File Size: %(filesize)s"), { + filesize: formatSize(file.size) + }) + + "
"; + + new PNotify({ + title: "Upload failed", + text: error, + type: "error", + hide: false + }); + } else { + data.formData = formData; + data.submit(); + } } }; @@ -1729,9 +1779,9 @@ $(function () { } error += "

"; - if (data.jqXHR.responseText) { + if (data.jqXHR.responseJSON && data.jqXHR.responseJSON.error) { error += pnotifyAdditionalInfo( - "
" + _.escape(data.jqXHR.responseText) + "
" + "
" + _.escape(data.jqXHR.responseJSON.error) + "
" ); } new PNotify({ diff --git a/src/octoprint/templates/dialogs/files.jinja2 b/src/octoprint/templates/dialogs/files.jinja2 index 73933fbb3..44e6c16a6 100644 --- a/src/octoprint/templates/dialogs/files.jinja2 +++ b/src/octoprint/templates/dialogs/files.jinja2 @@ -64,6 +64,9 @@ +