Exemplo n.º 1
0
def printerState():
	if not printer.is_operational():
		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.is_sd_ready()}})

	# add state information
	if not "state" in excludes:
		state = printer.get_current_data()["state"]
		result.update({"state": state})

	return jsonify(result)
Exemplo n.º 2
0
def printerState():
    if not printer.is_operational():
        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 = [
                x for x in [x.strip() for x in excludeStr.split(",")]
                if x in ["temperature", "sd", "state"]
            ]

    result = {}

    processor = lambda x: x
    if not printerProfileManager.get_current_or_default()["heatedBed"]:
        processor = _delete_bed

    # add temperature information
    if not "temperature" in excludes:
        result.update({"temperature": _get_temperature_data(processor)})

    # add sd information
    if not "sd" in excludes and settings().getBoolean(["feature", "sdSupport"
                                                       ]):
        result.update({"sd": {"ready": printer.is_sd_ready()}})

    # add state information
    if not "state" in excludes:
        state = printer.get_current_data()["state"]
        result.update({"state": state})

    return jsonify(result)
Exemplo n.º 3
0
def printerState():
    if not printer.is_operational():
        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:
        processor = lambda x: x
        heated_bed = printerProfileManager.get_current_or_default(
        )["heatedBed"]
        heated_chamber = printerProfileManager.get_current_or_default(
        )["heatedChamber"]
        filbox = printerProfileManager.get_current_or_default()["filbox"]
        if not heated_bed and not heated_chamber and not filbox:
            processor = _keep_tools
        elif not heated_bed:
            processor = _delete_bed
        elif not heated_chamber:
            processor = _delete_chamber
        elif not filbox:
            processor = _delete_filbox

        result.update({"temperature": _get_temperature_data(processor)})

    # add sd information
    if not "sd" in excludes and settings().getBoolean(["feature", "sdSupport"
                                                       ]):
        result.update({"sd": {"ready": printer.is_sd_ready()}})

    # add state information
    if not "state" in excludes:
        state = printer.get_current_data()["state"]
        result.update({"state": state})

    return jsonify(result)
Exemplo n.º 4
0
def printerState():
	if not printer.is_operational():
		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:
		processor = lambda x: x
		heated_bed = printerProfileManager.get_current_or_default()["heatedBed"]
		heated_chamber = printerProfileManager.get_current_or_default()["heatedChamber"]
		if not heated_bed and not heated_chamber:
			processor = _keep_tools
		elif not heated_bed:
			processor = _delete_bed
		elif not heated_chamber:
			processor = _delete_chamber

		result.update({"temperature": _get_temperature_data(processor)})

	# add sd information
	if not "sd" in excludes and settings().getBoolean(["feature", "sdSupport"]):
		result.update({"sd": {"ready": printer.is_sd_ready()}})

	# add state information
	if not "state" in excludes:
		state = printer.get_current_data()["state"]
		result.update({"state": state})

	return jsonify(result)
Exemplo n.º 5
0
def uploadGcodeFile(target):
	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:
		if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
			return make_response("Unknown target: %s" % target, 404)

		upload = octoprint.filemanager.util.DiskFileWrapper(request.values[input_upload_name], request.values[input_upload_path])

		# Store any additional user data the caller may have passed.
		userdata = None
		if "userdata" in request.values:
			import json
			try:
				userdata = json.loads(request.values["userdata"])
			except:
				return make_response("userdata contains invalid JSON", 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 future filename of file to be uploaded, abort if it can't be uploaded
		try:
			# FileDestinations.LOCAL = should normally be target, but can't because SDCard handling isn't implemented yet
			futurePath, futureFilename = fileManager.sanitize(FileDestinations.LOCAL, upload.filename)
		except:
			futurePath = None
			futureFilename = None

		if futureFilename is None:
			return make_response("Can not upload file %s, wrong format?" % upload.filename, 415)

		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)
		futureFullPathInStorage = fileManager.path_in_storage(FileDestinations.LOCAL, futureFullPath)

		if not printer.can_modify_file(futureFullPathInStorage, sd):
			return make_response("Trying to overwrite file that is currently being printed: %s" % futureFullPath, 409)

		reselect = printer.is_current_file(futureFullPathInStorage, sd)

		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 reselect):
				printer.select_file(absFilename, destination == FileDestinations.SDCARD, printAfterSelect)

		try:
			added_file = fileManager.add_file(FileDestinations.LOCAL, futureFullPathInStorage, upload, allow_overwrite=True)
		except octoprint.filemanager.storage.StorageError as e:
			if e.code == octoprint.filemanager.storage.StorageError.INVALID_FILE:
				return make_response("Could not upload the file \"{}\", invalid type".format(upload.filename), 400)
			else:
				return make_response("Could not upload the file \"{}\"".format(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 = not sd

		if userdata is not None:
			# upload included userdata, add this now to the metadata
			fileManager.set_additional_metadata(FileDestinations.LOCAL, added_file, "userdata", userdata)

		sdFilename = None
		if isinstance(filename, tuple):
			filename, sdFilename = filename

		eventManager.fire(Events.UPLOAD, {"name": futureFilename,
		                                  "path": filename,
		                                  "target": target,

		                                  # TODO deprecated, remove in 1.4.0
		                                  "file": filename})

		files = {}
		location = url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=filename, _external=True)
		files.update({
			FileDestinations.LOCAL: {
				"name": futureFilename,
				"path": 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,
					"path": sdFilename,
					"origin": FileDestinations.SDCARD,
					"refs": {
						"resource": location
					}
				}
			})

		r = make_response(jsonify(files=files, done=done), 201)
		r.headers["Location"] = location
		return r

	elif "foldername" in request.values:
		foldername = request.values["foldername"]

		if not target in [FileDestinations.LOCAL]:
			return make_response("Unknown target: %s" % target, 400)

		futurePath, futureName = fileManager.sanitize(target, foldername)
		if not futureName or not futurePath:
			return make_response("Can't create a folder with an empty name", 400)

		if "path" in request.values and request.values["path"]:
			futurePath = fileManager.sanitize_path(FileDestinations.LOCAL,
			                                       request.values["path"])

		futureFullPath = fileManager.join_path(target, futurePath, futureName)
		if octoprint.filemanager.valid_file_type(futureName):
			return make_response("Can't create a folder named %s, please try another name" % futureName, 409)

		try:
			added_folder = fileManager.add_folder(target, futureFullPath)
		except octoprint.filemanager.storage.StorageError as e:
			if e.code == octoprint.filemanager.storage.StorageError.INVALID_DIRECTORY:
				return make_response("Could not create folder {}, invalid directory".format(futureName))
			else:
				return make_response("Could not create folder {}".format(futureName))

		location = url_for(".readGcodeFile",
		                   target=FileDestinations.LOCAL,
		                   filename=added_folder,
		                   _external=True)
		folder = dict(name=futureName,
		              path=added_folder,
		              origin=target,
		              refs=dict(resource=location))

		r = make_response(jsonify(folder=folder, done=True), 201)
		r.headers["Location"] = location
		return r

	else:
		return make_response("No file to upload and no folder to create", 400)
Exemplo n.º 6
0
def uploadGcodeFile(target):
	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:
		if not target in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
			return make_response("Unknown target: %s" % target, 404)

		upload = octoprint.filemanager.util.DiskFileWrapper(request.values[input_upload_name], request.values[input_upload_path])

		# Store any additional user data the caller may have passed.
		userdata = None
		if "userdata" in request.values:
			import json
			try:
				userdata = json.loads(request.values["userdata"])
			except:
				return make_response("userdata contains invalid JSON", 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 future filename of file to be uploaded, abort if it can't be uploaded
		try:
			# FileDestinations.LOCAL = should normally be target, but can't because SDCard handling isn't implemented yet
			canonPath, canonFilename = fileManager.canonicalize(FileDestinations.LOCAL, upload.filename)
			futurePath = fileManager.sanitize_path(FileDestinations.LOCAL, canonPath)
			futureFilename = fileManager.sanitize_name(FileDestinations.LOCAL, canonFilename)
		except:
			canonFilename = None
			futurePath = None
			futureFilename = None

		if futureFilename is None:
			return make_response("Can not upload file %s, wrong format?" % upload.filename, 415)

		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)
		futureFullPathInStorage = fileManager.path_in_storage(FileDestinations.LOCAL, futureFullPath)

		if not printer.can_modify_file(futureFullPathInStorage, sd):
			return make_response("Trying to overwrite file that is currently being printed: %s" % futureFullPath, 409)

		reselect = printer.is_current_file(futureFullPathInStorage, sd)

		user = current_user.get_name()

		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, "machinecode"):
				return filename, printer.add_sd_file(filename, absFilename, selectAndOrPrint, tags={"source:api", "api:files.sd"})
			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 reselect):
				printer.select_file(absFilename, destination == FileDestinations.SDCARD, printAfterSelect, user)

		try:
			added_file = fileManager.add_file(FileDestinations.LOCAL, futureFullPathInStorage, upload,
			                                  allow_overwrite=True,
			                                  display=canonFilename)
		except octoprint.filemanager.storage.StorageError as e:
			if e.code == octoprint.filemanager.storage.StorageError.INVALID_FILE:
				return make_response("Could not upload the file \"{}\", invalid type".format(upload.filename), 400)
			else:
				return make_response("Could not upload the file \"{}\"".format(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 = not sd

		if userdata is not None:
			# upload included userdata, add this now to the metadata
			fileManager.set_additional_metadata(FileDestinations.LOCAL, added_file, "userdata", userdata)

		sdFilename = None
		if isinstance(filename, tuple):
			filename, sdFilename = filename

		eventManager.fire(Events.UPLOAD, {"name": futureFilename,
		                                  "path": filename,
		                                  "target": target,

		                                  # TODO deprecated, remove in 1.4.0
		                                  "file": filename})

		files = {}
		location = url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=filename, _external=True)
		files.update({
			FileDestinations.LOCAL: {
				"name": futureFilename,
				"path": 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,
					"path": sdFilename,
					"origin": FileDestinations.SDCARD,
					"refs": {
						"resource": location
					}
				}
			})

		r = make_response(jsonify(files=files, done=done), 201)
		r.headers["Location"] = location
		return r

	elif "foldername" in request.values:
		foldername = request.values["foldername"]

		if not target in [FileDestinations.LOCAL]:
			return make_response("Unknown target: %s" % target, 400)

		canonPath, canonName = fileManager.canonicalize(target, foldername)
		futurePath = fileManager.sanitize_path(target, canonPath)
		futureName = fileManager.sanitize_name(target, canonName)
		if not futureName or not futurePath:
			return make_response("Can't create a folder with an empty name", 400)

		if "path" in request.values and request.values["path"]:
			futurePath = fileManager.sanitize_path(FileDestinations.LOCAL,
			                                       request.values["path"])

		futureFullPath = fileManager.join_path(target, futurePath, futureName)
		if octoprint.filemanager.valid_file_type(futureName):
			return make_response("Can't create a folder named %s, please try another name" % futureName, 409)

		try:
			added_folder = fileManager.add_folder(target, futureFullPath, display=canonName)
		except octoprint.filemanager.storage.StorageError as e:
			if e.code == octoprint.filemanager.storage.StorageError.INVALID_DIRECTORY:
				return make_response("Could not create folder {}, invalid directory".format(futureName))
			else:
				return make_response("Could not create folder {}".format(futureName))

		location = url_for(".readGcodeFile",
		                   target=FileDestinations.LOCAL,
		                   filename=added_folder,
		                   _external=True)
		folder = dict(name=futureName,
		              path=added_folder,
		              origin=target,
		              refs=dict(resource=location))

		r = make_response(jsonify(folder=folder, done=True), 201)
		r.headers["Location"] = location
		return r

	else:
		return make_response("No file to upload and no folder to create", 400)
Exemplo n.º 7
0
def printerSdState():
	if not settings().getBoolean(["feature", "sdSupport"]):
		return make_response("SD support is disabled", 404)

	return jsonify(ready=printer.is_sd_ready())
Exemplo n.º 8
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:
		upload = octoprint.filemanager.util.DiskFileWrapper(request.values[input_upload_name], request.values[input_upload_path])
	else:
		return make_response("No file included", 400)

	# Store any additional user data the caller may have passed.
	userdata = None
	if "userdata" in request.values:
		import json
		try:
			userdata = json.loads(request.values["userdata"])
		except:
			return make_response("userdata contains invalid JSON", 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 = not sd

	if userdata is not None:
		# upload included userdata, add this now to the metadata
		fileManager.set_additional_metadata(FileDestinations.LOCAL, added_file, "userdata", userdata)

	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
Exemplo n.º 9
0
def uploadGcodeFile(target):
    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:
        if target not in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
            abort(404)

        upload = octoprint.filemanager.util.DiskFileWrapper(
            request.values[input_upload_name],
            request.values[input_upload_path])

        # Store any additional user data the caller may have passed.
        userdata = None
        if "userdata" in request.values:
            import json

            try:
                userdata = json.loads(request.values["userdata"])
            except Exception:
                abort(400, description="userdata contains invalid JSON")

        if target == FileDestinations.SDCARD and not settings().getBoolean(
            ["feature", "sdSupport"]):
            abort(404)

        sd = target == FileDestinations.SDCARD
        selectAfterUpload = ("select" in request.values and
                             request.values["select"] in valid_boolean_trues
                             and Permissions.FILES_SELECT.can())
        printAfterSelect = ("print" in request.values
                            and request.values["print"] in valid_boolean_trues
                            and Permissions.PRINT.can())

        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())):
                abort(
                    409,
                    description=
                    "Can not upload to SD card, printer is either not operational or already busy",
                )
            if not printer.is_sd_ready():
                abort(
                    409,
                    description="Can not upload to SD card, not yet initialized"
                )

        # determine future filename of file to be uploaded, abort if it can't be uploaded
        try:
            # FileDestinations.LOCAL = should normally be target, but can't because SDCard handling isn't implemented yet
            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)
        except Exception:
            canonFilename = None
            futurePath = None
            futureFilename = None

        if futureFilename is None:
            abort(415, description="Can not upload file, wrong format?")

        # prohibit overwriting currently selected file while it's being printed
        futureFullPath = fileManager.join_path(FileDestinations.LOCAL,
                                               futurePath, futureFilename)
        futureFullPathInStorage = fileManager.path_in_storage(
            FileDestinations.LOCAL, futureFullPath)

        if not printer.can_modify_file(futureFullPathInStorage, sd):
            abort(
                409,
                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()

        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, "machinecode")):
                return filename, printer.add_sd_file(
                    filename,
                    absFilename,
                    on_success=selectAndOrPrint,
                    tags={"source:api", "api:files.sd"},
                )
            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 reselect):
                printer.select_file(
                    absFilename,
                    destination == FileDestinations.SDCARD,
                    printAfterSelect,
                    user,
                )

        try:
            added_file = fileManager.add_file(
                FileDestinations.LOCAL,
                futureFullPathInStorage,
                upload,
                allow_overwrite=True,
                display=canonFilename,
            )
        except octoprint.filemanager.storage.StorageError as e:
            if e.code == octoprint.filemanager.storage.StorageError.INVALID_FILE:
                abort(400, description="Could not upload file, invalid type")
            else:
                abort(500, description="Could not upload file")

        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 = not sd

        if userdata is not None:
            # upload included userdata, add this now to the metadata
            fileManager.set_additional_metadata(FileDestinations.LOCAL,
                                                added_file, "userdata",
                                                userdata)

        sdFilename = None
        if isinstance(filename, tuple):
            filename, sdFilename = filename

        payload = {
            "name": futureFilename,
            "path": filename,
            "target": target,
            "select": selectAfterUpload,
            "print": printAfterSelect,
        }
        if userdata is not None:
            payload["userdata"] = userdata
        eventManager.fire(Events.UPLOAD, payload)

        files = {}
        location = url_for(
            ".readGcodeFile",
            target=FileDestinations.LOCAL,
            filename=filename,
            _external=True,
        )
        files.update({
            FileDestinations.LOCAL: {
                "name": futureFilename,
                "path": filename,
                "origin": FileDestinations.LOCAL,
                "refs": {
                    "resource":
                    location,
                    "download":
                    url_for("index", _external=True) + "downloads/files/" +
                    FileDestinations.LOCAL + "/" + urlquote(filename),
                },
            }
        })

        if sd and sdFilename:
            location = url_for(
                ".readGcodeFile",
                target=FileDestinations.SDCARD,
                filename=sdFilename,
                _external=True,
            )
            files.update({
                FileDestinations.SDCARD: {
                    "name": sdFilename,
                    "path": sdFilename,
                    "origin": FileDestinations.SDCARD,
                    "refs": {
                        "resource": location
                    },
                }
            })

        r = make_response(jsonify(files=files, done=done), 201)
        r.headers["Location"] = location
        return r

    elif "foldername" in request.values:
        foldername = request.values["foldername"]

        if target not in [FileDestinations.LOCAL]:
            abort(400, description="target is invalid")

        canonPath, canonName = fileManager.canonicalize(target, foldername)
        futurePath = fileManager.sanitize_path(target, canonPath)
        futureName = fileManager.sanitize_name(target, canonName)
        if not futureName or not futurePath:
            abort(400, description="folder name is empty")

        if "path" in request.values and request.values["path"]:
            futurePath = fileManager.sanitize_path(FileDestinations.LOCAL,
                                                   request.values["path"])

        futureFullPath = fileManager.join_path(target, futurePath, futureName)
        if octoprint.filemanager.valid_file_type(futureName):
            abort(409,
                  description="Can't create folder, please try another name")

        try:
            added_folder = fileManager.add_folder(target,
                                                  futureFullPath,
                                                  display=canonName)
        except octoprint.filemanager.storage.StorageError as e:
            if e.code == octoprint.filemanager.storage.StorageError.INVALID_DIRECTORY:
                abort(400,
                      description="Could not create folder, invalid directory")
            else:
                abort(500, description="Could not create folder")

        location = url_for(
            ".readGcodeFile",
            target=FileDestinations.LOCAL,
            filename=added_folder,
            _external=True,
        )
        folder = {
            "name": futureName,
            "path": added_folder,
            "origin": target,
            "refs": {
                "resource": location
            },
        }

        r = make_response(jsonify(folder=folder, done=True), 201)
        r.headers["Location"] = location
        return r

    else:
        abort(400, description="No file to upload and no folder to create")
Exemplo n.º 10
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:
        upload = octoprint.filemanager.util.DiskFileWrapper(
            request.values[input_upload_name],
            request.values[input_upload_path])
    else:
        return make_response("No file included", 400)

    # Store any additional user data the caller may have passed.
    userdata = None
    if "userdata" in request.values:
        import json
        try:
            userdata = json.loads(request.values["userdata"])
        except:
            return make_response("userdata contains invalid JSON", 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

    if userdata is not None:
        # upload included userdata, add this now to the metadata
        fileManager.set_additional_metadata(FileDestinations.LOCAL, added_file,
                                            "userdata", userdata)

    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
Exemplo n.º 11
0
def printerSdState():
    if not settings().getBoolean(["feature", "sdSupport"]):
        abort(404, description="SD support is disabled")

    return jsonify(ready=printer.is_sd_ready())