def printerState(): if not printer.isOperational(): return make_response("Printer is not operational", 409) # process excludes excludes = [] if "exclude" in request.values: excludeStr = request.values["exclude"] if len(excludeStr.strip()) > 0: excludes = filter(lambda x: x in ["temperature", "sd", "state"], map(lambda x: x.strip(), excludeStr.split(","))) result = {} # add temperature information if not "temperature" in excludes: result.update({"temperature": _getTemperatureData(lambda x: x)}) # add sd information if not "sd" in excludes and settings().getBoolean(["feature", "sdSupport"]): result.update({"sd": {"ready": printer.isSdReady()}}) # add state information if not "state" in excludes: state = printer.getCurrentData()["state"] result.update({"state": state}) return jsonify(result)
def readGcodeFiles(): filter = None if "filter" in request.values: filter = request.values["filter"] if filter is not None: print("lkj readGcodeFiles filter:%s" % repr(filter) ) print("lkj readGcodeFiles request.values:%s" % str(request.values) ) files = _getFileList(FileDestinations.LOCAL, filter=filter) print("lkj 1 readGcodeFiles files:%s" % str(files) ) #lkj files.extend(_getFileList(FileDestinations.SDCARD)) sdFree = 0 if printer.isSdReady(): files.extend(_getFileList(FileDestinations.FastbotSDCARD)) sdFree = util.getFreeBytes(fileManager._storage(FileDestinations.FastbotSDCARD).get_basefolder()) print("lkj 2 readGcodeFiles files:%s, sdFree:%s" % (str(files), str(sdFree)) ) return jsonify(files=files, free=util.getFreeBytes(settings().getBaseFolder("uploads")), sdFree=sdFree)
def downloadSDGcodeFiles(): data = request.json fileName = None if "filename" in data: print("lkj downloadSDGcodeFiles 3.4") fileName = data["filename"] if "origin" in data: print("lkj downloadSDGcodeFiles 3.4") origin = data["origin"] print("lkj downloadSDGcodeFiles request.values:%s" % str(request.values)) print("lkj downloadSDGcodeFiles data:%s" % str(data)) if origin not in [ FileDestinations.LOCAL, FileDestinations.SDCARD, FileDestinations.FastbotSDCARD ]: return make_response("Unknown origin: %s" % origin, 404) print("lkj downloadSDGcodeFiles 1") #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 if origin == FileDestinations.FastbotSDCARD and not printer.isSdReady(): return jsonify("") print("lkj downloadSDGcodeFiles 2") baseFolder = fileManager._storage(origin).get_basefolder() print("lkj downloadSDGcodeFiles 3.31 ") print("lkj downloadSDGcodeFiles 4") fileFullName = baseFolder + "/" + fileName print("lkj downloadSDGcodeFiles 5") buf_size = 4096 print("download file handler:%s" % fileFullName) response = make_response(view_function()) response.headers['Content-Type'] = 'application/octet-stream' response.headers[ 'Content-Disposition'] = 'attachment; filename=' + fileName response.write return response '''
def downloadSDGcodeFiles(): data = request.json fileName = None if "filename" in data: print("lkj downloadSDGcodeFiles 3.4") fileName = data["filename"] if "origin" in data: print("lkj downloadSDGcodeFiles 3.4") origin = data["origin"] print("lkj downloadSDGcodeFiles request.values:%s" % str(request.values) ) print("lkj downloadSDGcodeFiles data:%s" % str(data) ) if origin not in [FileDestinations.LOCAL, FileDestinations.SDCARD, FileDestinations.FastbotSDCARD]: return make_response("Unknown origin: %s" % origin, 404) print("lkj downloadSDGcodeFiles 1") #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 if origin == FileDestinations.FastbotSDCARD and not printer.isSdReady(): return jsonify("") print("lkj downloadSDGcodeFiles 2") baseFolder = fileManager._storage(origin).get_basefolder() print("lkj downloadSDGcodeFiles 3.31 " ) print("lkj downloadSDGcodeFiles 4") fileFullName = baseFolder + "/" + fileName print("lkj downloadSDGcodeFiles 5") buf_size = 4096 print("download file handler:%s" % fileFullName) response = make_response(view_function()) response.headers['Content-Type'] = 'application/octet-stream' response.headers['Content-Disposition'] = 'attachment; filename='+fileName response.write return response '''
def readGcodeFiles(): filter = None if "filter" in request.values: filter = request.values["filter"] if filter is not None: print("lkj readGcodeFiles filter:%s" % repr(filter)) print("lkj readGcodeFiles request.values:%s" % str(request.values)) files = _getFileList(FileDestinations.LOCAL, filter=filter) print("lkj 1 readGcodeFiles files:%s" % str(files)) #lkj files.extend(_getFileList(FileDestinations.SDCARD)) sdFree = 0 if printer.isSdReady(): files.extend(_getFileList(FileDestinations.FastbotSDCARD)) sdFree = util.getFreeBytes( fileManager._storage( FileDestinations.FastbotSDCARD).get_basefolder()) print("lkj 2 readGcodeFiles files:%s, sdFree:%s" % (str(files), str(sdFree))) return jsonify(files=files, free=util.getFreeBytes(settings().getBaseFolder("uploads")), sdFree=sdFree)
def uploadGcodeFile(target): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) if not "file" in request.files.keys(): return make_response("No file included", 400) if target == FileDestinations.SDCARD and not settings().getBoolean(["feature", "sdSupport"]): return make_response("SD card support is disabled", 404) file = request.files["file"] sd = target == FileDestinations.SDCARD 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 if sd: # validate that all preconditions for SD upload are met before attempting it if not (printer.isOperational() and not (printer.isPrinting() or printer.isPaused())): return make_response("Can not upload to SD card, printer is either not operational or already busy", 409) if not printer.isSdReady(): return make_response("Can not upload to SD card, not yet initialized", 409) # determine current job currentFilename = None currentSd = None currentJob = printer.getCurrentJob() if currentJob is not None and "filename" in currentJob.keys() and "sd" in currentJob.keys(): currentFilename = currentJob["filename"] currentSd = currentJob["sd"] # determine future filename of file to be uploaded, abort if it can't be uploaded futureFilename = gcodeManager.getFutureFilename(file) if futureFilename is None or (not settings().getBoolean(["cura", "enabled"]) and not gcodefiles.isGcodeFileName(futureFilename)): return make_response("Can not upload file %s, wrong format?" % file.filename, 415) # prohibit overwriting currently selected file while it's being printed if futureFilename == currentFilename and sd == currentSd and printer.isPrinting() or printer.isPaused(): return make_response("Trying to overwrite file that is currently being printed: %s" % currentFilename, 409) filename = None def fileProcessingFinished(filename, absFilename, destination): """ Callback for when the file processing (upload, optional slicing, addition to analysis queue) has finished. Depending on the file's destination triggers either streaming to SD card or directly calls selectAndOrPrint. """ if destination == FileDestinations.SDCARD: return filename, printer.addSdFile(filename, absFilename, selectAndOrPrint) else: selectAndOrPrint(absFilename, destination) return filename def selectAndOrPrint(nameToSelect, 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. """ sd = destination == FileDestinations.SDCARD if selectAfterUpload or printAfterSelect or (currentFilename == filename and currentSd == sd): printer.selectFile(nameToSelect, sd, printAfterSelect) destination = FileDestinations.SDCARD if sd else FileDestinations.LOCAL filename, done = gcodeManager.addFile(file, destination, fileProcessingFinished) if filename is None: return make_response("Could not upload the file %s" % file.filename, 500) sdFilename = None if isinstance(filename, tuple): filename, sdFilename = filename eventManager.fire(Events.UPLOAD, {"file": filename, "target": target}) files = {} if done: files.update({ FileDestinations.LOCAL: { "name": filename, "origin": FileDestinations.LOCAL, "refs": { "resource": url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=filename, _external=True), "download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + filename } } }) if sd and sdFilename: files.update({ FileDestinations.SDCARD: { "name": sdFilename, "origin": FileDestinations.SDCARD, "refs": { "resource": url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFilename, _external=True) } } }) return make_response(jsonify(files=files, done=done), 201)
def uploadGcodeFile(target): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) if not "file" in request.files.keys(): return make_response("No file included", 400) if target == FileDestinations.SDCARD and not settings().getBoolean( ["feature", "sdSupport"]): return make_response("SD card support is disabled", 404) file = request.files["file"] sd = target == FileDestinations.SDCARD 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 if sd: # validate that all preconditions for SD upload are met before attempting it if not (printer.isOperational() and not (printer.isPrinting() or printer.isPaused())): return make_response( "Can not upload to SD card, printer is either not operational or already busy", 409) if not printer.isSdReady(): return make_response( "Can not upload to SD card, not yet initialized", 409) # determine current job currentFilename = None currentSd = None currentJob = printer.getCurrentJob() if currentJob is not None and "filename" in currentJob.keys( ) and "sd" in currentJob.keys(): currentFilename = currentJob["filename"] currentSd = currentJob["sd"] # determine future filename of file to be uploaded, abort if it can't be uploaded futureFilename = gcodeManager.getFutureFilename(file) if futureFilename is None or ( not settings().getBoolean(["cura", "enabled"]) and not gcodefiles.isGcodeFileName(futureFilename)): return make_response( "Can not upload file %s, wrong format?" % file.filename, 415) # prohibit overwriting currently selected file while it's being printed if futureFilename == currentFilename and sd == currentSd and printer.isPrinting( ) or printer.isPaused(): return make_response( "Trying to overwrite file that is currently being printed: %s" % currentFilename, 409) filename = None def fileProcessingFinished(filename, absFilename, destination): """ Callback for when the file processing (upload, optional slicing, addition to analysis queue) has finished. Depending on the file's destination triggers either streaming to SD card or directly calls selectAndOrPrint. """ if destination == FileDestinations.SDCARD: return filename, printer.addSdFile(filename, absFilename, selectAndOrPrint) else: selectAndOrPrint(absFilename, destination) return filename def selectAndOrPrint(nameToSelect, 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. """ sd = destination == FileDestinations.SDCARD if selectAfterUpload or printAfterSelect or ( currentFilename == filename and currentSd == sd): printer.selectFile(nameToSelect, sd, printAfterSelect) destination = FileDestinations.SDCARD if sd else FileDestinations.LOCAL filename, done = gcodeManager.addFile(file, destination, fileProcessingFinished) if filename is None: return make_response("Could not upload the file %s" % file.filename, 500) sdFilename = None if isinstance(filename, tuple): filename, sdFilename = filename eventManager.fire(Events.UPLOAD, {"file": filename, "target": target}) files = {} if done: files.update({ FileDestinations.LOCAL: { "name": filename, "origin": FileDestinations.LOCAL, "refs": { "resource": url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=filename, _external=True), "download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + filename } } }) if sd and sdFilename: files.update({ FileDestinations.SDCARD: { "name": sdFilename, "origin": FileDestinations.SDCARD, "refs": { "resource": url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFilename, _external=True) } } }) return make_response(jsonify(files=files, done=done), 201)
def printerSdState(): if not settings().getBoolean(["feature", "sdSupport"]): return make_response("SD support is disabled", 404) return jsonify(ready=printer.isSdReady())
def uploadGcodeFile(target): if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) input_name = "file" input_upload_name = input_name + "." + settings().get(["server", "uploads", "nameSuffix"]) input_upload_path = input_name + "." + settings().get(["server", "uploads", "pathSuffix"]) if input_upload_name in request.values and input_upload_path in request.values: import shutil upload = util.Object() upload.filename = request.values[input_upload_name] upload.save = lambda new_path: shutil.move(request.values[input_upload_path], new_path) elif input_name in request.files: upload = request.files[input_name] else: return make_response("No file included", 400) if target == FileDestinations.SDCARD and not settings().getBoolean(["feature", "sdSupport"]): return make_response("SD card support is disabled", 404) sd = target == FileDestinations.SDCARD 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 if sd: # validate that all preconditions for SD upload are met before attempting it if not (printer.isOperational() and not (printer.isPrinting() or printer.isPaused())): return make_response("Can not upload to SD card, printer is either not operational or already busy", 409) if not printer.isSdReady(): return make_response("Can not upload to SD card, not yet initialized", 409) # determine current job currentFilename = None currentOrigin = None currentJob = printer.getCurrentJob() if currentJob is not None and "file" in currentJob.keys(): currentJobFile = currentJob["file"] if "name" in currentJobFile.keys() and "origin" in currentJobFile.keys(): currentFilename = currentJobFile["name"] currentOrigin = currentJobFile["origin"] # determine future filename of file to be uploaded, abort if it can't be uploaded try: futureFilename = fileManager.sanitize_name(FileDestinations.LOCAL, upload.filename) except: futureFilename = None if futureFilename is None or not (slicingManager.slicing_enabled or octoprint.filemanager.valid_file_type(futureFilename, type="gcode")): return make_response("Can not upload file %s, wrong format?" % upload.filename, 415) # prohibit overwriting currently selected file while it's being printed if futureFilename == currentFilename and target == currentOrigin and printer.isPrinting() or printer.isPaused(): return make_response("Trying to overwrite file that is currently being printed: %s" % currentFilename, 409) def fileProcessingFinished(filename, absFilename, destination): """ Callback for when the file processing (upload, optional slicing, addition to analysis queue) has finished. Depending on the file's destination triggers either streaming to SD card or directly calls selectAndOrPrint. """ if destination == FileDestinations.SDCARD and octoprint.filemanager.valid_file_type(filename, "gcode"): return filename, printer.addSdFile(filename, absFilename, selectAndOrPrint) else: selectAndOrPrint(filename, absFilename, destination) return 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 octoprint.filemanager.valid_file_type(added_file, "gcode") and (selectAfterUpload or printAfterSelect or (currentFilename == filename and currentOrigin == destination)): printer.selectFile(absFilename, destination == FileDestinations.SDCARD, printAfterSelect) added_file = fileManager.add_file(FileDestinations.LOCAL, upload.filename, upload, allow_overwrite=True) if added_file is None: return make_response("Could not upload the file %s" % upload.filename, 500) if octoprint.filemanager.valid_file_type(added_file, "stl"): filename = added_file done = True else: filename = fileProcessingFinished(added_file, fileManager.get_absolute_path(FileDestinations.LOCAL, added_file), target) done = True sdFilename = None if isinstance(filename, tuple): filename, sdFilename = filename eventManager.fire(Events.UPLOAD, {"file": filename, "target": target}) files = {} location = url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=filename, _external=True) files.update({ FileDestinations.LOCAL: { "name": filename, "origin": FileDestinations.LOCAL, "refs": { "resource": location, "download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + filename } } }) if sd and sdFilename: location = url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFilename, _external=True) files.update({ FileDestinations.SDCARD: { "name": sdFilename, "origin": FileDestinations.SDCARD, "refs": { "resource": location } } }) r = make_response(jsonify(files=files, done=done), 201) r.headers["Location"] = location return r
def uploadGcodeFile(target): print("lkj uploadGcodeFile target:%s" % str(target)) if target in ["extruder1", "extruder2", "bed"]: return upload_temp_curve_firmware(target) if target == "firmware": return uploadFirmwareFile(target) if target == FileDestinations.FastbotSDCARD: return uploadFastBotSDCARD(target) if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target: %s" % target, 404) input_name = "file" input_upload_name = input_name + "." + settings().get( ["server", "uploads", "nameSuffix"]) input_upload_path = input_name + "." + settings().get( ["server", "uploads", "pathSuffix"]) if input_upload_name in request.values and input_upload_path in request.values: import shutil upload = util.Object() upload.filename = request.values[input_upload_name] upload.save = lambda new_path: shutil.move( request.values[input_upload_path], new_path) elif input_name in request.files: upload = request.files[input_name] else: return make_response("No file included", 400) if target == FileDestinations.SDCARD and not settings().getBoolean( ["feature", "sdSupport"]): return make_response("SD card support is disabled", 404) sd = target == FileDestinations.SDCARD 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 if sd: # validate that all preconditions for SD upload are met before attempting it if not (printer.isOperational() and not (printer.isPrinting() or printer.isPaused())): return make_response( "Can not upload to SD card, printer is either not operational or already busy", 409) if not printer.isSdReady(): return make_response( "Can not upload to SD card, not yet initialized", 409) # determine current job currentFilename = None currentOrigin = None currentJob = printer.getCurrentJob() if currentJob is not None and "file" in currentJob.keys(): currentJobFile = currentJob["file"] if "name" in currentJobFile.keys() and "origin" in currentJobFile.keys( ): currentFilename = currentJobFile["name"] currentOrigin = currentJobFile["origin"] # determine future filename of file to be uploaded, abort if it can't be uploaded try: futureFilename = fileManager.sanitize_name(FileDestinations.LOCAL, upload.filename) except: futureFilename = None if futureFilename is None or not (slicingManager.slicing_enabled or octoprint.filemanager.valid_file_type( futureFilename, type="gcode")): return make_response( "Can not upload file %s, wrong format?" % upload.filename, 415) # prohibit overwriting currently selected file while it's being printed if futureFilename == currentFilename and target == currentOrigin and printer.isPrinting( ) or printer.isPaused(): return make_response( "Trying to overwrite file that is currently being printed: %s" % currentFilename, 409) def fileProcessingFinished(filename, absFilename, destination): """ Callback for when the file processing (upload, optional slicing, addition to analysis queue) has finished. Depending on the file's destination triggers either streaming to SD card or directly calls selectAndOrPrint. """ if destination == FileDestinations.SDCARD and octoprint.filemanager.valid_file_type( filename, "gcode"): return filename, printer.addSdFile(filename, absFilename, selectAndOrPrint) else: selectAndOrPrint(filename, absFilename, destination) return 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 octoprint.filemanager.valid_file_type(added_file, "gcode") and ( selectAfterUpload or printAfterSelect or (currentFilename == filename and currentOrigin == destination)): printer.selectFile(absFilename, destination == FileDestinations.SDCARD, printAfterSelect) added_file = fileManager.add_file(FileDestinations.LOCAL, upload.filename, upload, allow_overwrite=True) if added_file is None: return make_response("Could not upload the file %s" % upload.filename, 500) if octoprint.filemanager.valid_file_type(added_file, "stl"): filename = added_file done = True else: filename = fileProcessingFinished( added_file, fileManager.get_absolute_path(FileDestinations.LOCAL, added_file), target) done = True sdFilename = None if isinstance(filename, tuple): filename, sdFilename = filename eventManager.fire(Events.UPLOAD, {"file": filename, "target": target}) files = {} location = url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=filename, _external=True) files.update({ FileDestinations.LOCAL: { "name": filename, "origin": FileDestinations.LOCAL, "refs": { "resource": location, "download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + filename } } }) if sd and sdFilename: location = url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFilename, _external=True) files.update({ FileDestinations.SDCARD: { "name": sdFilename, "origin": FileDestinations.SDCARD, "refs": { "resource": location } } }) r = make_response(jsonify(files=files, done=done), 201) r.headers["Location"] = location return r