diff --git a/src/octoprint/server/api/files.py b/src/octoprint/server/api/files.py index b615c173..5a2ff6e1 100644 --- a/src/octoprint/server/api/files.py +++ b/src/octoprint/server/api/files.py @@ -51,14 +51,17 @@ def _getFileList(origin): files = [] if sdFileList is not None: - for sdFile in sdFileList: - files.append({ + for sdFile, sdSize in sdFileList: + file = { "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: files = gcodeManager.getAllFileData() for file in files: diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index c182d983..54285cb3 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -147,7 +147,8 @@ default_settings = { "numExtruders": 1, "includeCurrentToolInTemps": True, "hasBed": True, - "repetierStyleTargetTemperature": False + "repetierStyleTargetTemperature": False, + "extendedSdFileList": False } } } diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 42c39624..b6f4bcaa 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -645,13 +645,28 @@ class MachineCom(object): ##~~ SD file list # if we are currently receiving an sd file list, each line is just a filename, so just read it and abort processing - if self._sdFileList and isGcodeFileName(line.strip().lower()) and not 'End file list' in line: - filename = line.strip().lower() - if filterNonAscii(filename): - self._logger.warn("Got a file from printer's SD that has a non-ascii filename (%s), that shouldn't happen according to the protocol" % filename) + if self._sdFileList and not "End file list" in line: + fileinfo = line.strip().split(None, 2) + if len(fileinfo) > 1: + # we got extended file information here, so let's split filename and size and try to make them a bit nicer + filename, size = fileinfo + filename = filename.lower() + try: + size = int(size) + except ValueError: + # whatever that was, it was not an integer, so we'll just ignore it and set size to None + size = None else: - self._sdFiles.append(filename) - continue + # no extended file information, so only the filename is there and we set size to None + filename = fileinfo[0].lower() + size = None + + if isGcodeFileName(filename): + if filterNonAscii(filename): + self._logger.warn("Got a file from printer's SD that has a non-ascii filename (%s), that shouldn't happen according to the protocol" % filename) + else: + self._sdFiles.append((filename, size)) + continue ##~~ Temperature processing if ' T:' in line or line.startswith('T:') or ' T0:' in line or line.startswith('T0:'): diff --git a/src/octoprint/util/virtual.py b/src/octoprint/util/virtual.py index cc0b34b2..021bc5b6 100644 --- a/src/octoprint/util/virtual.py +++ b/src/octoprint/util/virtual.py @@ -155,8 +155,20 @@ class VirtualPrinter(): def _listSd(self): self.readList.append("Begin file list") - for osFile in os.listdir(self._virtualSd): - self.readList.append(osFile.upper()) + if settings().getBoolean(["devel", "virtualPrinter", "extendedSdFileList"]): + self.readList.extend( + map( + lambda x: "%s %d" % (x.upper(), os.stat(os.path.join(self._virtualSd, x)).st_size), + os.listdir(self._virtualSd) + ) + ) + else: + self.readList.extend( + map( + lambda x: x.upper(), + os.listdir(self._virtualSd) + ) + ) self.readList.append("End file list") self._sendOk()