Esempio n. 1
0
def upload_temp_curve_firmware(target):
    print("lkj upload_temp_curve_firmware target:%s" % str(target))

    print("lkj post request.values:%s" % str(request.values))
    print("lkj post request.files:%s" % str(request.files))

    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:
        print("lkj here 1")
        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:
        print("lkj here 2")
        upload = request.files[input_name]
    else:
        return make_response("No file included", 400)
    print("lkj post 2")
    # 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

    print("lkj futureFilename:%s" % futureFilename)

    try:
        added_file = fileManager.add_temp_curve_file(FileDestinations.LOCAL,
                                                     upload.filename,
                                                     upload,
                                                     allow_overwrite=True,
                                                     target=target)
    except:
        added_file = None
    if added_file is None:
        print("lkj post added_file is None")
        return make_response("Could not upload the file %s" % upload.filename,
                             500)
    print("lkj added_file: %s" % added_file)

    files = {}
    done = True
    files.update({
        FileDestinations.LOCAL: {
            "name": added_file,
            "origin": FileDestinations.LOCAL
        }
    })
    r = make_response(jsonify(files=files, done=done), 201)
    return r
Esempio n. 2
0
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.is_operational()
                and not (printer.is_printing() or printer.is_paused())):
            return make_response(
                "Can not upload to SD card, printer is either not operational or already busy",
                409)
        if not printer.is_sd_ready():
            return make_response(
                "Can not upload to SD card, not yet initialized", 409)

    # determine current job
    currentFilename = None
    currentOrigin = None
    currentJob = printer.get_current_job()
    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:
        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.is_printing(
    ) or printer.is_paused():
        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.add_sd_file(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.select_file(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.path_on_disk(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
                "download":
                url_for("index", _external=True) + "uploads" + "/" + 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
Esempio n. 3
0
def uploadFastBotSDCARD(target):
    print("lkj uploadFastBotSDCARD target:%s" % str(target))
    #target = FileDestinations.FastbotSDCARD

    print("lkj post request.values:%s" % str(request.values))
    print("lkj post request.files:%s" % str(request.files))

    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)

    print("lkj post upload:%s" % str(upload))

    if target == FileDestinations.FastbotSDCARD and not settings().getBoolean(
        ["feature", "sdSupport"]):
        return make_response("SD card support is disabled", 404)
    print("lkj uploadGcodeFile 2")
    sd = target == FileDestinations.FastbotSDCARD
    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

    print("lkj uploadGcodeFile 3")
    # 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.FastbotSDCARD, 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)
    print("lkj uploadGcodeFile 4")
    # 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)
    print("lkj uploadGcodeFile 5")

    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.
		"""
        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.FastbotSDCARD,
                               printAfterSelect)

    print("lkj uploadGcodeFile 6")
    added_file = fileManager.add_file(FileDestinations.FastbotSDCARD,
                                      upload.filename,
                                      upload,
                                      allow_overwrite=True)
    if added_file is None:
        return make_response("Could not upload the file %s" % upload.filename,
                             500)

    print("lkj uploadGcodeFile 6.0, added_file:%s" % str(added_file))
    if octoprint.filemanager.valid_file_type(added_file, "stl"):
        filename = added_file
        done = True
        print("lkj uploadGcodeFile 6.1")
    else:
        filename = fileProcessingFinished(
            added_file,
            fileManager.get_absolute_path(FileDestinations.FastbotSDCARD,
                                          added_file), target)
        done = True

    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
                        }
                }
        })
	'''
    print("lkj uploadGcodeFile 7 sdFilename:%s" % sdFilename)
    if sd and sdFilename:
        print("lkj uploadGcodeFile 7.1")
        location = url_for(".readGcodeFile",
                           target=FileDestinations.FastbotSDCARD,
                           filename=sdFilename,
                           _external=True)
        files.update({
            FileDestinations.FastbotSDCARD: {
                "name": sdFilename,
                "origin": FileDestinations.FastbotSDCARD,
                "refs": {
                    "resource": location
                }
            }
        })
    print("lkj uploadFastBotSDCARD 8")
    r = make_response(jsonify(files=files, done=done), 201)
    r.headers["Location"] = location
    return r
Esempio n. 4
0
def uploadFirmwareFile(target):
    print("lkj uploadFirmwareFile target:%s" % str(target))
    target = FileDestinations.LOCAL
    if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
        return make_response("Unknown target: %s" % target, 404)

    print("lkj post target:%s" % str(target))
    print("lkj post request.values:%s" % str(request.values))
    print("lkj post request.files:%s" % str(request.files))

    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:
        print("lkj here 1")
        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:
        print("lkj here 2")
        upload = request.files[input_name]
    else:
        return make_response("No file included", 400)
    print("lkj post 2")
    # 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

    print("lkj futureFilename:%s" % futureFilename)

    try:
        added_file = fileManager.add_firmware_file(FileDestinations.LOCAL,
                                                   upload.filename,
                                                   upload,
                                                   allow_overwrite=True)
    except:
        added_file = None
    if added_file is None:
        print("lkj post added_file is None")
        return make_response("Could not upload the file %s" % upload.filename,
                             500)
    print("lkj added_file: %s" % added_file)

    files = {}
    done = True
    files.update({
        FileDestinations.LOCAL: {
            "name": added_file,
            "origin": FileDestinations.LOCAL
        }
    })
    r = make_response(jsonify(files=files, done=done), 201)
    #lkj
    from octoprint.server import printer
    if printer.isOperational():
        #cmd = ("M205", param=added_file)
        #printer.command()
        pass

    #r.headers["Location"] = added_file
    return r