From 84373a7617bc99a83f892f06308634ee26d6fc7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 25 Mar 2021 18:05:13 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Confirm=20overwrite=20dialog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On file upload, if the target file already exists, a confirmation will now be shown that allows to explicitly overwrite the existing file, rename the file prior to upload (with a collision free suggestion provided) or cancel the upload. A feature flag allows to disable this confirmation again to restore the old behaviour in case the new one clashes with people's workflows. Implements #3873 --- src/octoprint/server/api/files.py | 65 ++++++++- src/octoprint/server/api/settings.py | 8 ++ src/octoprint/settings.py | 1 + src/octoprint/static/js/app/client/files.js | 23 ++-- .../static/js/app/viewmodels/files.js | 123 +++++++++++++++--- .../static/js/app/viewmodels/settings.js | 1 + src/octoprint/templates/dialogs/files.jinja2 | 24 ++++ .../dialogs/settings/features.jinja2 | 7 + 8 files changed, 219 insertions(+), 33 deletions(-) diff --git a/src/octoprint/server/api/files.py b/src/octoprint/server/api/files.py index a1608d4a4..f77bd414e 100644 --- a/src/octoprint/server/api/files.py +++ b/src/octoprint/server/api/files.py @@ -163,6 +163,54 @@ def readGcodeFiles(): return jsonify(files=files, free=usage.free, total=usage.total) +@api.route("/files/test", methods=["POST"]) +@Permissions.FILES_LIST.require(403) +def runFilesTest(): + valid_commands = { + "sanitize": ["storage", "path", "filename"], + "exists": ["storage", "path", "filename"], + } + + command, data, response = get_json_command_from_request(request, valid_commands) + if response is not None: + return response + + def sanitize(storage, path, filename): + sanitized_path = fileManager.sanitize_path(storage, path) + sanitized_name = fileManager.sanitize_name(storage, filename) + joined = fileManager.join_path(storage, sanitized_path, sanitized_name) + return sanitized_path, sanitized_name, joined + + if command == "sanitize": + _, _, sanitized = sanitize(data["storage"], data["path"], data["filename"]) + return jsonify(sanitized=sanitized) + elif command == "exists": + storage = data["storage"] + path = data["path"] + filename = data["filename"] + + sanitized_path, _, sanitized = sanitize(storage, path, filename) + + exists = fileManager.file_exists(storage, sanitized) + if exists: + suggestion = filename + name, ext = os.path.splitext(filename) + counter = 0 + while fileManager.file_exists( + storage, + fileManager.join_path( + storage, + sanitized_path, + fileManager.sanitize_name(storage, suggestion), + ), + ): + counter += 1 + suggestion = name + "_{}".format(counter) + ext + return jsonify(exists=True, suggestion=suggestion) + else: + return jsonify(exists=False) + + @api.route("/files/", methods=["GET"]) @Permissions.FILES_LIST.require(403) @with_revalidation_checking( @@ -507,6 +555,11 @@ def uploadGcodeFile(target): canonPath, canonFilename = fileManager.canonicalize( FileDestinations.LOCAL, upload.filename ) + if request.values.get("path"): + canonPath = request.values.get("path") + if request.values.get("filename"): + canonFilename = request.values.get("filename") + futurePath = fileManager.sanitize_path(FileDestinations.LOCAL, canonPath) futureFilename = fileManager.sanitize_name( FileDestinations.LOCAL, canonFilename @@ -519,12 +572,6 @@ def uploadGcodeFile(target): if futureFilename is None: abort(415, description="Can not upload file, wrong format?") - if "path" in request.values and request.values["path"]: - # we currently only support uploads to sdcard via local, so first target is local instead of "target" - futurePath = fileManager.sanitize_path( - FileDestinations.LOCAL, request.values["path"] - ) - # prohibit overwriting currently selected file while it's being printed futureFullPath = fileManager.join_path( FileDestinations.LOCAL, futurePath, futureFilename @@ -539,6 +586,12 @@ def uploadGcodeFile(target): description="Trying to overwrite file that is currently being printed", ) + if ( + fileManager.file_exists(FileDestinations.LOCAL, futureFullPathInStorage) + and request.values.get("noOverwrite") in valid_boolean_trues + ): + abort(409, description="File already exists and noOverwrite was set") + reselect = printer.is_current_file(futureFullPathInStorage, sd) user = current_user.get_name() diff --git a/src/octoprint/server/api/settings.py b/src/octoprint/server/api/settings.py index f4126a7e4..5a85e5a77 100644 --- a/src/octoprint/server/api/settings.py +++ b/src/octoprint/server/api/settings.py @@ -150,6 +150,9 @@ def getSettings(): "printCancelConfirmation": s.getBoolean( ["feature", "printCancelConfirmation"] ), + "uploadOverwriteConfirmation": s.getBoolean( + ["feature", "uploadOverwriteConfirmation"] + ), "g90InfluencesExtruder": s.getBoolean(["feature", "g90InfluencesExtruder"]), "autoUppercaseBlacklist": s.get(["feature", "autoUppercaseBlacklist"]), }, @@ -617,6 +620,11 @@ def _saveSettings(data): ["feature", "printCancelConfirmation"], data["feature"]["printCancelConfirmation"], ) + if "uploadOverwriteConfirmation" in data["feature"]: + s.setBoolean( + ["feature", "uploadOverwriteConfirmation"], + data["feature"]["uploadOverwriteConfirmation"], + ) if "g90InfluencesExtruder" in data["feature"]: s.setBoolean( ["feature", "g90InfluencesExtruder"], diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 1a248707d..a6fdcd6b9 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -281,6 +281,7 @@ default_settings = { "modelSizeDetection": True, "printStartConfirmation": False, "printCancelConfirmation": True, + "uploadOverwriteConfirmation": True, "autoUppercaseBlacklist": ["M117", "M118"], "g90InfluencesExtruder": False, }, diff --git a/src/octoprint/static/js/app/client/files.js b/src/octoprint/static/js/app/client/files.js index f066f7112..517f57d1e 100644 --- a/src/octoprint/static/js/app/client/files.js +++ b/src/octoprint/static/js/app/client/files.js @@ -7,6 +7,7 @@ })(this, function (OctoPrintClient, $, _) { var url = "api/files"; var downloadUrl = "downloads/files"; + var testUrl = url + "/test"; var OctoPrintFilesClient = function (base) { this.base = base; @@ -198,20 +199,22 @@ return recursiveSearch(path.split("/"), root); }; - OctoPrintFilesClient.prototype.pathForElement = function (element) { - // TODO Remove in 1.4.x - log.warn( - "pathForElement has been renamed to pathForEntry, please use that instead" + OctoPrintFilesClient.prototype.sanitize = function (location, path, filename, opts) { + return this.base.issueCommand( + testUrl, + "sanitize", + {storage: location, path: path, filename: filename}, + opts ); - return this.pathForEntry(element); }; - OctoPrintFilesClient.prototype.elementByPath = function (location, startElement) { - // TODO Remove in 1.4.x - log.warn( - "elementByPath has been renamed to entryForPath, please use that instead" + OctoPrintFilesClient.prototype.exists = function (location, path, filename, opts) { + return this.base.issueCommand( + testUrl, + "exists", + {storage: location, path: path, filename: filename}, + opts ); - return this.entryForPath(location, startElement); }; OctoPrintClient.registerComponent("files", OctoPrintFilesClient); diff --git a/src/octoprint/static/js/app/viewmodels/files.js b/src/octoprint/static/js/app/viewmodels/files.js index 7349e49cb..5c3633155 100644 --- a/src/octoprint/static/js/app/viewmodels/files.js +++ b/src/octoprint/static/js/app/viewmodels/files.js @@ -97,6 +97,9 @@ $(function () { ); }); + self.uploadExistsDialog = undefined; + self.uploadFilename = ko.observable(undefined); + self.allItems = ko.observable(undefined); self.currentPath = ko.observable(""); self.uploadProgressText = ko.observable(); @@ -1221,6 +1224,8 @@ $(function () { } }); + self.uploadExistsDialog = $("#upload_exists_dialog"); + //~~ Gcode upload self.uploadButton = $("#gcode_upload"); @@ -1419,22 +1424,18 @@ $(function () { if (button === undefined) return; - button - .fileupload({ - url: url, - dataType: "json", - dropZone: enable ? drop : null, - drop: function (e, data) {}, - submit: self._handleUploadStart, - done: self._handleUploadDone, - fail: self._handleUploadFail, - always: self._handleUploadAlways, - progressall: self._handleUploadProgress - }) - .bind("fileuploadsubmit", function (e, data) { - if (self.currentPath() !== "") - data.formData = {path: self.currentPath()}; - }); + button.fileupload({ + url: url, + dataType: "json", + dropZone: enable ? drop : null, + drop: function (e, data) {}, + add: self._handleUploadAdd, + submit: self._handleUploadStart, + done: self._handleUploadDone, + fail: self._handleUploadFail, + always: self._handleUploadAlways, + progressall: self._handleUploadProgress + }); }; self._enableDragNDrop = function (enable) { @@ -1460,6 +1461,89 @@ $(function () { } }; + self._handleUploadAdd = function (e, data) { + var file = data.files[0]; + var path = self.currentPath(); + + var formData = {}; + if (path !== "") { + formData.path = path; + } + + if (self.settingsViewModel.feature_uploadOverwriteConfirmation()) { + OctoPrint.files + .exists("local", path, file.name) + .done(function (response) { + if (response.exists) { + $("h3", self.uploadExistsDialog).text( + _.sprintf(gettext("File already exists: %(name)s"), { + name: file.name + }) + ); + $("input", self.uploadExistsDialog) + .val("") + .prop("placeholder", response.suggestion); + $("a.upload-rename", self.uploadExistsDialog) + .prop("disabled", false) + .off("click") + .on("click", function () { + var newName = $( + "input", + self.uploadExistsDialog + ).val(); + if (newName === "") newName = response.suggestion; + + OctoPrint.files + .exists("local", path, newName) + .done(function (r) { + if (r.exists) { + $( + ".control-group", + self.uploadExistsDialog + ).addClass("error"); + $( + ".help-block", + self.uploadExistsDialog + ).show(); + } else { + $( + ".control-group", + self.uploadExistsDialog + ).removeClass("error"); + $( + ".help-block", + self.uploadExistsDialog + ).hide(); + + self.uploadExistsDialog.modal("hide"); + + formData.filename = newName; + formData.noOverwrite = true; + data.formData = formData; + + data.submit(); + } + }); + }); + $("a.upload-overwrite", self.uploadExistsDialog) + .off("click") + .on("click", function () { + self.uploadExistsDialog.modal("hide"); + data.formData = formData; + data.submit(); + }); + self.uploadExistsDialog.modal("show"); + } else { + data.formData = formData; + data.submit(); + } + }); + } else { + data.formData = formData; + data.submit(); + } + }; + self._handleUploadStart = function (e, data) { self.ignoreUpdatedFilesEvent = true; return true; @@ -1607,6 +1691,11 @@ $(function () { "printerProfilesViewModel", "accessViewModel" ], - elements: ["#files_wrapper", "#add_folder_dialog", "#move_file_or_folder_dialog"] + elements: [ + "#files_wrapper", + "#add_folder_dialog", + "#move_file_or_folder_dialog", + "#upload_exists_dialog" + ] }); }); diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index 09bf5a2e1..1457d4a17 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -173,6 +173,7 @@ $(function () { self.feature_modelSizeDetection = ko.observable(undefined); self.feature_printStartConfirmation = ko.observable(undefined); self.feature_printCancelConfirmation = ko.observable(undefined); + self.feature_uploadOverwriteConfirmation = ko.observable(undefined); self.feature_g90InfluencesExtruder = ko.observable(undefined); self.feature_autoUppercaseBlacklist = ko.observable(undefined); diff --git a/src/octoprint/templates/dialogs/files.jinja2 b/src/octoprint/templates/dialogs/files.jinja2 index b841abf1d..75cc5e71e 100644 --- a/src/octoprint/templates/dialogs/files.jinja2 +++ b/src/octoprint/templates/dialogs/files.jinja2 @@ -41,3 +41,27 @@ {{ _('Move') }} + + diff --git a/src/octoprint/templates/dialogs/settings/features.jinja2 b/src/octoprint/templates/dialogs/settings/features.jinja2 index 05d33b0f6..452c9942c 100644 --- a/src/octoprint/templates/dialogs/settings/features.jinja2 +++ b/src/octoprint/templates/dialogs/settings/features.jinja2 @@ -28,6 +28,13 @@ +
+
+ +
+