Exemple #1
0
def _create_etag(path, filter, recursive, lm=None):
    if lm is None:
        lm = _create_lastmodified(path, recursive)

    if lm is None:
        return None

    hash = hashlib.sha1()

    def hash_update(value):
        value = value.encode("utf-8")
        hash.update(value)

    hash_update(str(lm))
    hash_update(str(filter))
    hash_update(str(recursive))

    path = path[len("/api/files"):]
    if path.startswith("/"):
        path = path[1:]

    if "/" in path:
        storage, _ = path.split("/", 1)
    else:
        storage = path

    if path == "" or storage == FileDestinations.SDCARD:
        # include sd data in etag
        hash_update(
            repr(sorted(printer.get_sd_files(), key=lambda x: sv(x["name"]))))

    hash_update(
        _DATA_FORMAT_VERSION)  # increment version if we change the API format

    return hash.hexdigest()
Exemple #2
0
def _create_etag(path, recursive, lm=None):
    if lm is None:
        lm = _create_lastmodified(path, recursive)

    if lm is None:
        return None

    hash = hashlib.sha1()
    hash.update(str(lm))
    hash.update(str(recursive))

    if path.endswith("/files") or path.endswith("/files/sdcard"):
        # include sd data in etag
        hash.update(repr(sorted(printer.get_sd_files(), key=lambda x: x[0])))

    return hash.hexdigest()
Exemple #3
0
def _create_etag(path, recursive, lm=None):
	if lm is None:
		lm = _create_lastmodified(path, recursive)

	if lm is None:
		return None

	hash = hashlib.sha1()
	hash.update(str(lm))
	hash.update(str(recursive))

	if path.endswith("/files") or path.endswith("/files/sdcard"):
		# include sd data in etag
		hash.update(repr(sorted(printer.get_sd_files(), key=lambda x: x[0])))

	return hash.hexdigest()
Exemple #4
0
def _create_etag(path, filter, recursive, lm=None):
	if lm is None:
		lm = _create_lastmodified(path, recursive)

	if lm is None:
		return None

	hash = hashlib.sha1()
	hash.update(str(lm))
	hash.update(str(filter))
	hash.update(str(recursive))

	if path.endswith("/files") or path.endswith("/files/sdcard"):
		# include sd data in etag
		hash.update(repr(sorted(printer.get_sd_files(), key=lambda x: x[0])))

	hash.update(_DATA_FORMAT_VERSION) # increment version if we change the API format

	return hash.hexdigest()
Exemple #5
0
def _create_etag(path, filter, recursive, lm=None):
	if lm is None:
		lm = _create_lastmodified(path, recursive)

	if lm is None:
		return None

	hash = hashlib.sha1()
	hash.update(str(lm))
	hash.update(str(filter))
	hash.update(str(recursive))

	if path.endswith("/files") or path.endswith("/files/sdcard"):
		# include sd data in etag
		hash.update(repr(sorted(printer.get_sd_files(), key=lambda x: x[0])))

	hash.update(_DATA_FORMAT_VERSION) # increment version if we change the API format

	return hash.hexdigest()
Exemple #6
0
def _verifyFileExists(origin, filename):
	if origin == FileDestinations.SDCARD:
		return filename in map(lambda x: x[0], printer.get_sd_files())
	else:
		return fileManager.file_exists(origin, filename)
Exemple #7
0
def _getFileList(origin, path=None, filter=None, recursive=False, allow_from_cache=True):
	if origin == FileDestinations.SDCARD:
		sdFileList = printer.get_sd_files()

		files = []
		if sdFileList is not None:
			for sdFile, sdSize in sdFileList:
				file = {
					"type": "machinecode",
					"name": sdFile,
					"path": sdFile,
					"origin": FileDestinations.SDCARD,
					"refs": {
						"resource": url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFile, _external=True)
					}
				}
				if sdSize is not None:
					file.update({"size": sdSize})
				files.append(file)
	else:
		filter_func = None
		if filter:
			filter_func = lambda entry, entry_data: octoprint.filemanager.valid_file_type(entry, type=filter)

		with _file_cache_mutex:
			cache_key = "{}:{}:{}:{}".format(origin, path, recursive, filter)
			files, lastmodified = _file_cache.get(cache_key, ([], None))
			if not allow_from_cache or lastmodified is None or lastmodified < fileManager.last_modified(origin, path=path, recursive=recursive):
				files = fileManager.list_files(origin, path=path, filter=filter_func, recursive=recursive)[origin].values()
				lastmodified = fileManager.last_modified(origin, path=path, recursive=recursive)
				_file_cache[cache_key] = (files, lastmodified)

		def analyse_recursively(files, path=None):
			if path is None:
				path = ""

			result = []
			for file_or_folder in files:
				# make a shallow copy in order to not accidentally modify the cached data
				file_or_folder = dict(file_or_folder)

				file_or_folder["origin"] = FileDestinations.LOCAL

				if file_or_folder["type"] == "folder":
					if "children" in file_or_folder:
						file_or_folder["children"] = analyse_recursively(file_or_folder["children"].values(), path + file_or_folder["name"] + "/")

					file_or_folder["refs"] = dict(resource=url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=path + file_or_folder["name"], _external=True))
				else:
					if "analysis" in file_or_folder and octoprint.filemanager.valid_file_type(file_or_folder["name"], type="gcode"):
						file_or_folder["gcodeAnalysis"] = file_or_folder["analysis"]
						del file_or_folder["analysis"]

					if "history" in file_or_folder and octoprint.filemanager.valid_file_type(file_or_folder["name"], type="gcode"):
						# convert print log
						history = file_or_folder["history"]
						del file_or_folder["history"]
						success = 0
						failure = 0
						last = None
						for entry in history:
							success += 1 if "success" in entry and entry["success"] else 0
							failure += 1 if "success" in entry and not entry["success"] else 0
							if not last or ("timestamp" in entry and "timestamp" in last and entry["timestamp"] > last["timestamp"]):
								last = entry
						if last:
							prints = dict(
								success=success,
								failure=failure,
								last=dict(
									success=last["success"],
									date=last["timestamp"]
								)
							)
							if "printTime" in last:
								prints["last"]["printTime"] = last["printTime"]
							file_or_folder["prints"] = prints

					file_or_folder["refs"] = dict(resource=url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=file_or_folder["path"], _external=True),
					                              download=url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + file_or_folder["path"])

				result.append(file_or_folder)

			return result

		files = analyse_recursively(files)

	return files
Exemple #8
0
def _verifyFileExists(origin, filename):
	if origin == FileDestinations.SDCARD:
		return filename in map(lambda x: x[0], printer.get_sd_files())
	else:
		return fileManager.file_exists(origin, filename)
Exemple #9
0
def _getFileList(origin, path=None, filter=None, recursive=False, allow_from_cache=True):
	if origin == FileDestinations.SDCARD:
		sdFileList = printer.get_sd_files()

		files = []
		if sdFileList is not None:
			for sdFile, sdSize in sdFileList:
				type_path = octoprint.filemanager.get_file_type(sdFile)
				if not type_path:
					# only supported extensions
					continue
				else:
					file_type = type_path[0]

				file = {
					"type": file_type,
					"typePath": type_path,
					"name": sdFile,
					"display": sdFile,
					"path": sdFile,
					"origin": FileDestinations.SDCARD,
					"refs": {
						"resource": url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFile, _external=True)
					}
				}
				if sdSize is not None:
					file.update({"size": sdSize})
				files.append(file)
	else:
		filter_func = None
		if filter:
			filter_func = lambda entry, entry_data: octoprint.filemanager.valid_file_type(entry, type=filter)

		with _file_cache_mutex:
			cache_key = "{}:{}:{}:{}".format(origin, path, recursive, filter)
			files, lastmodified = _file_cache.get(cache_key, ([], None))
			if not allow_from_cache or lastmodified is None or lastmodified < fileManager.last_modified(origin, path=path, recursive=recursive):
				files = fileManager.list_files(origin, path=path, filter=filter_func, recursive=recursive)[origin].values()
				lastmodified = fileManager.last_modified(origin, path=path, recursive=recursive)
				_file_cache[cache_key] = (files, lastmodified)

		def analyse_recursively(files, path=None):
			if path is None:
				path = ""

			result = []
			for file_or_folder in files:
				# make a shallow copy in order to not accidentally modify the cached data
				file_or_folder = dict(file_or_folder)

				file_or_folder["origin"] = FileDestinations.LOCAL

				if file_or_folder["type"] == "folder":
					if "children" in file_or_folder:
						file_or_folder["children"] = analyse_recursively(file_or_folder["children"].values(), path + file_or_folder["name"] + "/")

					file_or_folder["refs"] = dict(resource=url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=path + file_or_folder["name"], _external=True))
				else:
					if "analysis" in file_or_folder and octoprint.filemanager.valid_file_type(file_or_folder["name"], type="gcode"):
						file_or_folder["gcodeAnalysis"] = file_or_folder["analysis"]
						del file_or_folder["analysis"]

					if "history" in file_or_folder and octoprint.filemanager.valid_file_type(file_or_folder["name"], type="gcode"):
						# convert print log
						history = file_or_folder["history"]
						del file_or_folder["history"]
						success = 0
						failure = 0
						last = None
						for entry in history:
							success += 1 if "success" in entry and entry["success"] else 0
							failure += 1 if "success" in entry and not entry["success"] else 0
							if not last or ("timestamp" in entry and "timestamp" in last and entry["timestamp"] > last["timestamp"]):
								last = entry
						if last:
							prints = dict(
								success=success,
								failure=failure,
								last=dict(
									success=last["success"],
									date=last["timestamp"]
								)
							)
							if "printTime" in last:
								prints["last"]["printTime"] = last["printTime"]
							file_or_folder["prints"] = prints

					file_or_folder["refs"] = dict(resource=url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=file_or_folder["path"], _external=True),
					                              download=url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + file_or_folder["path"])

				result.append(file_or_folder)

			return result

		files = analyse_recursively(files)

	return files
Exemple #10
0
def _getFileList(origin, filter=None):
	if origin == FileDestinations.SDCARD:
		sdFileList = printer.get_sd_files()

		files = []
		if sdFileList is not None:
			for sdFile, sdSize in sdFileList:
				file = {
					"type": "machinecode",
					"name": sdFile,
					"origin": FileDestinations.SDCARD,
					"refs": {
						"resource": url_for(".readGcodeFile", target=FileDestinations.SDCARD, filename=sdFile, _external=True)
					}
				}
				if sdSize is not None:
					file.update({"size": sdSize})
				files.append(file)
	else:
		filter_func = None
		if filter:
			filter_func = lambda entry, entry_data: octoprint.filemanager.valid_file_type(entry, type=filter)
		files = fileManager.list_files(origin, filter=filter_func, recursive=False)[origin].values()
		for file in files:
			file["origin"] = FileDestinations.LOCAL

			if "analysis" in file and octoprint.filemanager.valid_file_type(file["name"], type="gcode"):
				file["gcodeAnalysis"] = file["analysis"]
				del file["analysis"]

			if "history" in file and octoprint.filemanager.valid_file_type(file["name"], type="gcode"):
				# convert print log
				history = file["history"]
				del file["history"]
				success = 0
				failure = 0
				last = None
				for entry in history:
					success += 1 if "success" in entry and entry["success"] else 0
					failure += 1 if "success" in entry and not entry["success"] else 0
					if not last or ("timestamp" in entry and "timestamp" in last and entry["timestamp"] > last["timestamp"]):
						last = entry
				if last:
					prints = dict(
						success=success,
						failure=failure,
						last=dict(
							success=last["success"],
							date=last["timestamp"]
						)
					)
					if "printTime" in last:
						prints["last"]["printTime"] = last["printTime"]
					file["prints"] = prints

			file.update({
				"refs": {
					"resource": url_for(".readGcodeFile", target=FileDestinations.LOCAL, filename=file["name"], _external=True),
					"download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + file["name"]
				}
			})
	return files
Exemple #11
0
def _verifyFileExists(origin, filename):
    if origin == FileDestinations.SDCARD:
        return filename in (x["name"] for x in printer.get_sd_files())
    else:
        return fileManager.file_exists(origin, filename)
Exemple #12
0
def _getFileList(origin, filter=None):
    if origin == FileDestinations.SDCARD:
        sdFileList = printer.get_sd_files()

        files = []
        if sdFileList is not None:
            for sdFile, sdSize in sdFileList:
                file = {
                    "type": "machinecode",
                    "name": sdFile,
                    "origin": FileDestinations.SDCARD,
                    "refs": {
                        "resource":
                        url_for(".readGcodeFile",
                                target=FileDestinations.SDCARD,
                                filename=sdFile,
                                _external=True)
                    }
                }
                if sdSize is not None:
                    file.update({"size": sdSize})
                files.append(file)
    else:
        filter_func = None
        if filter:
            filter_func = lambda entry, entry_data: octoprint.filemanager.valid_file_type(
                entry, type=filter)
        files = fileManager.list_files(origin,
                                       filter=filter_func,
                                       recursive=False)[origin].values()
        for file in files:
            file["origin"] = FileDestinations.LOCAL

            if "analysis" in file and octoprint.filemanager.valid_file_type(
                    file["name"], type="gcode"):
                file["gcodeAnalysis"] = file["analysis"]
                del file["analysis"]

            if "history" in file and octoprint.filemanager.valid_file_type(
                    file["name"], type="gcode"):
                # convert print log
                history = file["history"]
                del file["history"]
                success = 0
                failure = 0
                last = None
                for entry in history:
                    success += 1 if "success" in entry and entry[
                        "success"] else 0
                    failure += 1 if "success" in entry and not entry[
                        "success"] else 0
                    if not last or ("timestamp" in entry
                                    and "timestamp" in last and
                                    entry["timestamp"] > last["timestamp"]):
                        last = entry
                if last:
                    prints = dict(success=success,
                                  failure=failure,
                                  last=dict(success=last["success"],
                                            date=last["timestamp"]))
                    if "printTime" in last:
                        prints["last"]["printTime"] = last["printTime"]
                    file["prints"] = prints

            file.update({
                "refs": {
                    "resource":
                    url_for(".readGcodeFile",
                            target=FileDestinations.LOCAL,
                            filename=file["name"],
                            _external=True),
                    "download":
                    url_for("index", _external=True) + "downloads/files/" +
                    FileDestinations.LOCAL + "/" + file["name"]
                }
            })
    return files
Exemple #13
0
def _getFileList(origin,
                 path=None,
                 filter=None,
                 recursive=False,
                 level=0,
                 allow_from_cache=True):
    if origin == FileDestinations.SDCARD:
        sdFileList = printer.get_sd_files(refresh=not allow_from_cache)

        files = []
        if sdFileList is not None:
            for f in sdFileList:
                type_path = octoprint.filemanager.get_file_type(f["name"])
                if not type_path:
                    # only supported extensions
                    continue
                else:
                    file_type = type_path[0]

                file = {
                    "type": file_type,
                    "typePath": type_path,
                    "name": f["name"],
                    "display": f["display"] if f["display"] else f["name"],
                    "path": f["name"],
                    "origin": FileDestinations.SDCARD,
                    "refs": {
                        "resource":
                        url_for(
                            ".readGcodeFile",
                            target=FileDestinations.SDCARD,
                            filename=f["name"],
                            _external=True,
                        )
                    },
                }
                if f["size"] is not None:
                    file.update({"size": f["size"]})
                files.append(file)
    else:
        filter_func = None
        if filter:
            filter_func = lambda entry, entry_data: octoprint.filemanager.valid_file_type(
                entry, type=filter)

        with _file_cache_mutex:
            cache_key = f"{origin}:{path}:{recursive}:{filter}"
            files, lastmodified = _file_cache.get(cache_key, ([], None))
            # recursive needs to be True for lastmodified queries so we get lastmodified of whole subtree - #3422
            if (not allow_from_cache or lastmodified is None
                    or lastmodified < fileManager.last_modified(
                        origin, path=path, recursive=True)):
                files = list(
                    fileManager.list_files(
                        origin,
                        path=path,
                        filter=filter_func,
                        recursive=recursive,
                        level=level,
                        force_refresh=not allow_from_cache,
                    )[origin].values())
                lastmodified = fileManager.last_modified(origin,
                                                         path=path,
                                                         recursive=True)
                _file_cache[cache_key] = (files, lastmodified)

        def analyse_recursively(files, path=None):
            if path is None:
                path = ""

            result = []
            for file_or_folder in files:
                # make a shallow copy in order to not accidentally modify the cached data
                file_or_folder = dict(file_or_folder)

                file_or_folder["origin"] = FileDestinations.LOCAL

                if file_or_folder["type"] == "folder":
                    if "children" in file_or_folder:
                        file_or_folder["children"] = analyse_recursively(
                            file_or_folder["children"].values(),
                            path + file_or_folder["name"] + "/",
                        )

                    file_or_folder["refs"] = {
                        "resource":
                        url_for(
                            ".readGcodeFile",
                            target=FileDestinations.LOCAL,
                            filename=path + file_or_folder["name"],
                            _external=True,
                        )
                    }
                else:
                    if ("analysis" in file_or_folder
                            and octoprint.filemanager.valid_file_type(
                                file_or_folder["name"], type="gcode")):
                        file_or_folder["gcodeAnalysis"] = file_or_folder[
                            "analysis"]
                        del file_or_folder["analysis"]

                    if ("history" in file_or_folder
                            and octoprint.filemanager.valid_file_type(
                                file_or_folder["name"], type="gcode")):
                        # convert print log
                        history = file_or_folder["history"]
                        del file_or_folder["history"]
                        success = 0
                        failure = 0
                        last = None
                        for entry in history:
                            success += 1 if "success" in entry and entry[
                                "success"] else 0
                            failure += (1 if "success" in entry
                                        and not entry["success"] else 0)
                            if not last or ("timestamp" in entry
                                            and "timestamp" in last
                                            and entry["timestamp"] >
                                            last["timestamp"]):
                                last = entry
                        if last:
                            prints = {
                                "success": success,
                                "failure": failure,
                                "last": {
                                    "success": last["success"],
                                    "date": last["timestamp"],
                                },
                            }
                            if "printTime" in last:
                                prints["last"]["printTime"] = last["printTime"]
                            file_or_folder["prints"] = prints

                    file_or_folder["refs"] = {
                        "resource":
                        url_for(
                            ".readGcodeFile",
                            target=FileDestinations.LOCAL,
                            filename=file_or_folder["path"],
                            _external=True,
                        ),
                        "download":
                        url_for("index", _external=True) + "downloads/files/" +
                        FileDestinations.LOCAL + "/" +
                        urlquote(file_or_folder["path"]),
                    }

                result.append(file_or_folder)

            return result

        files = analyse_recursively(files)

    return files