From a24d3f26aeaa7572802a18bcf5e430bd44a2532b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 16 Mar 2021 11:02:01 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20caching=20behaviour=20on?= =?UTF-8?q?=20SD=20card=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `force=true` is supplied, the SD card files should be refreshed. Also finally documented the `force` parameter in the API docs. Closes #4051 --- docs/api/files.rst | 8 ++++++-- src/octoprint/printer/standard.py | 20 +++++++++++--------- src/octoprint/server/api/files.py | 4 ++-- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/docs/api/files.rst b/docs/api/files.rst index d295ee2b9..0757bde41 100644 --- a/docs/api/files.rst +++ b/docs/api/files.rst @@ -14,7 +14,8 @@ Retrieve all files .. http:get:: /api/files Retrieve information regarding all files currently available and regarding the disk space still available - locally in the system. + locally in the system. The results are cached for performance reasons. If you + want to override the cache, supply the query parameter ``force`` and set it to ``true``. By default only returns the files and folders in the root directory. If the query parameter ``recursive`` is provided and set to ``true``, returns all files and folders. @@ -234,6 +235,7 @@ Retrieve all files "free": "3.2GB" } + :param force: If set to ``true``, forces a refresh, overriding the cache. :param recursive: If set to ``true``, return all files and folders recursively. Otherwise only return items on same level. :statuscode 200: No error @@ -245,7 +247,8 @@ Retrieve files from specific location .. http:get:: /api/files/(string:location) Retrieve information regarding the files currently available on the selected `location` and -- if targeting - the ``local`` location -- regarding the disk space still available locally in the system. + the ``local`` location -- regarding the disk space still available locally in the system. The results are cached for performance reasons. If you + want to override the cache, supply the query parameter ``force`` and set it to ``true``. By default only returns the files and folders in the root directory. If the query parameter ``recursive`` is provided and set to ``true``, returns all files and folders. @@ -305,6 +308,7 @@ Retrieve files from specific location :param location: The origin location from which to retrieve the files. Currently only ``local`` and ``sdcard`` are supported, with ``local`` referring to files stored in OctoPrint's ``uploads`` folder and ``sdcard`` referring to files stored on the printer's SD card (if available). + :param force: If set to ``true``, forces a refresh, overriding the cache. :param recursive: If set to ``true``, return all files and folders recursively. Otherwise only return items on same level. :statuscode 200: No error :statuscode 404: If `location` is neither ``local`` nor ``sdcard`` diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 5316b8d1e..deef6978a 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -920,16 +920,18 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): # ~~ sd file handling def get_sd_files(self, *args, **kwargs): - if self._comm is None or not self._comm.isSdReady(): - result = [] - else: - result = list( - map( - lambda x: {"name": x[0][1:], "size": x[1], "display": x[2]}, - self._comm.getSdFiles(), - ) + if not self.is_sd_ready(): + return [] + + if kwargs.get("refresh"): + self.refresh_sd_files(blocking=True) + + return list( + map( + lambda x: {"name": x[0][1:], "size": x[1], "display": x[2]}, + self._comm.getSdFiles(), ) - return result + ) def add_sd_file( self, filename, path, on_success=None, on_failure=None, *args, **kwargs diff --git a/src/octoprint/server/api/files.py b/src/octoprint/server/api/files.py index 385daee99..a1608d4a4 100644 --- a/src/octoprint/server/api/files.py +++ b/src/octoprint/server/api/files.py @@ -157,7 +157,7 @@ def readGcodeFiles(): recursive=recursive, allow_from_cache=not force, ) - files.extend(_getFileList(FileDestinations.SDCARD)) + files.extend(_getFileList(FileDestinations.SDCARD, allow_from_cache=not force)) usage = psutil.disk_usage(settings().getBaseFolder("uploads", check_writable=False)) return jsonify(files=files, free=usage.free, total=usage.total) @@ -254,7 +254,7 @@ def _getFileList( origin, path=None, filter=None, recursive=False, level=0, allow_from_cache=True ): if origin == FileDestinations.SDCARD: - sdFileList = printer.get_sd_files() + sdFileList = printer.get_sd_files(refresh=not allow_from_cache) files = [] if sdFileList is not None: