Example #1
0
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)
Example #2
0
def uploadGcodeFile(target):
    if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
        return make_response("Invalid target: %s" % target, 400)

    if "gcode_file" in request.files.keys():
        file = request.files["gcode_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

        # 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.get("cura", "enabled") and not gcodefiles.isGcodeFileName(futureFilename)):
            return make_response("Can not upload file %s, wrong format?" % file.filename, 400)

        # 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, 403)

        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 selectOrPrint.
            """
            sd = destination == FileDestinations.SDCARD
            if sd:
                printer.addSdFile(filename, absFilename, selectAndOrPrint)
            else:
                selectAndOrPrint(absFilename, destination)

        def selectAndOrPrint(nameToSelect, destination):
            """
            Callback for when the file is ready to be selected and optionally printed. For SD file uploads this 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)

        eventManager.fire("Upload", filename)
        return jsonify(files=gcodeManager.getAllFileData(), filename=filename, done=done)
    else:
        return make_response("No gcode_file included", 400)
Example #3
0
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)
Example #4
0
def uploadGcodeFile(target):
    if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
        return make_response("Invalid target: %s" % target, 400)

    if "gcode_file" in request.files.keys():
        file = request.files["gcode_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

        # 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, 400)

        # 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, 403)

        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 selectOrPrint.
			"""
            sd = destination == FileDestinations.SDCARD
            if sd:
                printer.addSdFile(filename, absFilename, selectAndOrPrint)
            else:
                selectAndOrPrint(absFilename, destination)

        def selectAndOrPrint(nameToSelect, destination):
            """
			Callback for when the file is ready to be selected and optionally printed. For SD file uploads this 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)

        eventManager.fire("Upload", filename)
        return jsonify(files=gcodeManager.getAllFileData(),
                       filename=filename,
                       done=done)
    else:
        return make_response("No gcode_file included", 400)