def _copyMoveCommand(self, workerID, target, command, source, destination): from octoprint.server.api.files import _verifyFolderExists, _verifyFileExists if not _verifyFileExists(target, source) and not _verifyFolderExists(target, source): return if _verifyFolderExists(target, destination): path, name = self._file_manager.split_path(target, source) destination = self._file_manager.join_path(target, destination, name) if _verifyFileExists(target, destination) or _verifyFolderExists(target, destination): return if command == "copy": if self._file_manager.file_exists(target, source): self._file_manager.copy_file(target, source, destination) elif self._file_manager.folder_exists(target, source): self._file_manager.copy_folder(target, source, destination) elif command == "move": from octoprint.server.api.files import _isBusy if _isBusy(target, source): self._plugin_manager.send_plugin_message(self._identifier, dict(type="failed", workerID=workerID, lastfile=source, reason="Trying to delete a file that is currently in use")) return # deselect the file if it's currently selected from octoprint.server.api.files import _getCurrentFile currentOrigin, currentFilename = _getCurrentFile() if currentFilename is not None and source == currentFilename: self._printer.unselect_file() if self._file_manager.file_exists(target, source): self._file_manager.move_file(target, source, destination) elif self._file_manager.folder_exists(target, source): self._file_manager.move_folder(target, source, destination)
def _deleteCommand(self, workerID, target, source): from octoprint.server.api.files import _verifyFolderExists, _verifyFileExists, _isBusy # prohibit deleting or moving files that are currently in use from octoprint.server.api.files import _getCurrentFile currentOrigin, currentFilename = _getCurrentFile() if _verifyFileExists(target, source): if _isBusy(target, source): self._plugin_manager.send_plugin_message( self._identifier, dict(type="failed", workerID=workerID, lastfile=source, reason= "Trying to delete a file that is currently in use")) return # deselect the file if it's currently selected if currentFilename is not None and source == currentFilename: self._printer.unselect_file() # delete it if target == FileDestinations.SDCARD: self._printer.delete_sd_file(source) else: self._file_manager.remove_file(target, source) elif _verifyFolderExists(target, source): if not target in [FileDestinations.LOCAL]: return make_response("Unknown target: %s" % target, 404) folderpath = source if _isBusy(target, folderpath): self._plugin_manager.send_plugin_message( self._identifier, dict( type="failed", workerID=workerID, lastfile=folderpath, reason= "Trying to delete a folder that contains a file that is currently in use" )) return # deselect the file if it's currently selected if currentFilename is not None and self._file_manager.file_in_path( target, folderpath, currentFilename): self._printer.unselect_file() # delete it self._file_manager.remove_folder(target, folderpath)
def _deleteCommand(self, workerID, target, source): from octoprint.server.api.files import _verifyFolderExists, _verifyFileExists, _isBusy # prohibit deleting or moving files that are currently in use from octoprint.server.api.files import _getCurrentFile currentOrigin, currentFilename = _getCurrentFile() if _verifyFileExists(target, source): if _isBusy(target, source): self._plugin_manager.send_plugin_message(self._identifier, dict(type="failed", workerID=workerID, lastfile=source, reason="Trying to delete a file that is currently in use")) return # deselect the file if it's currently selected if currentFilename is not None and source == currentFilename: self._printer.unselect_file() # delete it if target == FileDestinations.SDCARD: self._printer.delete_sd_file(source) else: self._file_manager.remove_file(target, source) elif _verifyFolderExists(target, source): if not target in [FileDestinations.LOCAL]: return make_response("Unknown target: %s" % target, 404) folderpath = source if _isBusy(target, folderpath): self._plugin_manager.send_plugin_message(self._identifier, dict(type="failed", workerID=workerID, lastfile=folderpath, reason="Trying to delete a folder that contains a file that is currently in use")) return # deselect the file if it's currently selected if currentFilename is not None and self._file_manager.file_in_path(target, folderpath, currentFilename): self._printer.unselect_file() # delete it self._file_manager.remove_folder(target, folderpath)
def gcodeFileCommand(self, target, filename): if target not in [FileDestinations.LOCAL]: return make_response("Unknown target: %s" % target, 404) if not self._settings.global_get_boolean(["feature", "sdSupport"]): return make_response("SD card support is disabled", 404) # valid file commands, dict mapping command name to mandatory parameters valid_commands = { "uploadSd": [] } command, data, response = get_json_command_from_request(request, valid_commands) if response is not None: return response if command == "uploadSd": from octoprint.server.api.files import _verifyFolderExists, _verifyFileExists if not _verifyFileExists(FileDestinations.LOCAL, filename): return make_response("File not found on '%s': %s" % (FileDestinations.LOCAL, filename), 404) from octoprint.filemanager import valid_file_type if not valid_file_type(filename, type="machinecode"): return make_response("Cannot upload {filename} to SD, not a machinecode file".format(**locals()), 415) # validate that all preconditions for SD upload are met before attempting it if not (self._printer.is_operational() and not (self._printer.is_printing() or self._printer.is_paused())): return make_response("Can not upload to SD card, printer is either not operational or already busy", 409) if not self._printer.is_sd_ready(): return make_response("Can not upload to SD card, not yet initialized", 409) # determine current job currentFilename = None currentFullPath = None currentOrigin = None currentJob = self._printer.get_current_job() if currentJob is not None and "file" in currentJob.keys(): currentJobFile = currentJob["file"] if currentJobFile is not None and "name" in currentJobFile.keys() and "origin" in currentJobFile.keys() and \ currentJobFile["name"] is not None and currentJobFile["origin"] is not None: currentPath, currentFilename = self._file_manager.split_path(FileDestinations.LOCAL, currentJobFile["name"]) currentOrigin = currentJobFile["origin"] selectAfterUpload = "select" in request.values.keys() and request.values["select"] in valid_boolean_trues printAfterSelect = "print" in request.values.keys() and request.values["print"] in valid_boolean_trues filePath, fileName = self._file_manager.sanitize(FileDestinations.LOCAL, filename) fullPath = self._file_manager.join_path(FileDestinations.LOCAL, filePath, fileName) def selectAndOrPrint(filename, absFilename, destination): """ Callback for when the file is ready to be selected and optionally printed. For SD file uploads this is only the case after they have finished streaming to the printer, which is why this callback is also used for the corresponding call to addSdFile. Selects the just uploaded file if either selectAfterUpload or printAfterSelect are True, or if the exact file is already selected, such reloading it. """ if selectAfterUpload or printAfterSelect or ( currentFilename == filename and currentOrigin == destination): self._printer.select_file(absFilename, destination == FileDestinations.SDCARD, printAfterSelect) sdFilename = self._printer.add_sd_file(fileName, fullPath, selectAndOrPrint) from octoprint.events import Events self._event_bus.fire(Events.UPLOAD, {"file": sdFilename, "target": FileDestinations.SDCARD}) location = url_for("api.readGcodeFile", target=FileDestinations.SDCARD, filename=sdFilename, _external=True) files = { FileDestinations.SDCARD: { "name": sdFilename, "origin": FileDestinations.SDCARD, "refs": { "resource": location } } } r = make_response(jsonify(files=files, done=True), 201) r.headers["Location"] = location return r