From 27a17ed5a67c39f8565bc3167e8e1a29c842ac8d Mon Sep 17 00:00:00 2001 From: Arksine Date: Mon, 9 Nov 2020 07:01:15 -0500 Subject: [PATCH] file_manager: update request handlers to accept WebRequest objects Signed-off-by: Eric Callahan --- moonraker/plugins/file_manager.py | 48 +++++++++++++------------------ 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/moonraker/plugins/file_manager.py b/moonraker/plugins/file_manager.py index df769a6..5208335 100644 --- a/moonraker/plugins/file_manager.py +++ b/moonraker/plugins/file_manager.py @@ -102,12 +102,12 @@ class FileManager: def get_fixed_path_args(self): return dict(self.fixed_path_args) - async def _handle_filelist_request(self, path, method, args): - root = args.get('root', "gcodes") + async def _handle_filelist_request(self, web_request): + root = web_request.get_str('root', "gcodes") return self.get_file_list(root, list_format=True, notify=True) - async def _handle_metadata_request(self, path, method, args): - requested_file = args.get('filename') + async def _handle_metadata_request(self, web_request): + requested_file = web_request.get_str('filename') metadata = self.gcode_metadata.get(requested_file, None) if metadata is None: raise self.server.error( @@ -115,19 +115,12 @@ class FileManager: metadata['filename'] = requested_file return metadata - async def _handle_directory_request(self, path, method, args): - directory = args.get('path', "gcodes") + async def _handle_directory_request(self, web_request): + directory = web_request.get_str('path', "gcodes") root, rel_path, dir_path = self._convert_path(directory) - method = method.upper() - if method == 'GET': - is_extended = args.get('extended', False) - if isinstance(is_extended, str): - val = is_extended.lower() - if val in ["true", "false"]: - is_extended = True if val == "true" else False - if not isinstance(is_extended, bool): - raise self.server.error( - f"Invalid argument for 'extended': {is_extended}") + action = web_request.get_action() + if action == 'GET': + is_extended = web_request.get_boolean('extended', False) # Get list of files and subdirectories for this target dir_info = self._list_directory(dir_path) # Check to see if a filelist update is necessary @@ -142,14 +135,14 @@ class FileManager: if metadata is not None and is_extended: f.update(metadata) return dir_info - elif method == 'POST' and root in FULL_ACCESS_ROOTS: + elif action == 'POST' and root in FULL_ACCESS_ROOTS: # Create a new directory try: os.mkdir(dir_path) except Exception as e: raise self.server.error(str(e)) self.notify_filelist_changed("create_dir", rel_path, root) - elif method == 'DELETE' and root in FULL_ACCESS_ROOTS: + elif action == 'DELETE' and root in FULL_ACCESS_ROOTS: # Remove a directory if directory.strip("/") == root: raise self.server.error( @@ -157,9 +150,7 @@ class FileManager: if not os.path.isdir(dir_path): raise self.server.error( f"Directory does not exist ({directory})") - force = args.get('force', False) - if isinstance(force, str): - force = force.lower() == "true" + force = web_request.get_boolean('force', False) if force: # Make sure that the directory does not contain a file # loaded by the virtual_sdcard @@ -211,9 +202,10 @@ class FileManager: disk_path = os.path.join(disk_path, rel_path) return root, rel_path, disk_path - async def _handle_file_move_copy(self, path, method, args): - source = args.get("source") - destination = args.get("dest") + async def _handle_file_move_copy(self, web_request): + source = web_request.get_str("source") + destination = web_request.get_str("dest") + ep = web_request.get_endpoint() if source is None: raise self.server.error("File move/copy request issing source") if destination is None: @@ -230,7 +222,7 @@ class FileManager: if os.path.exists(dest_path): await self._handle_operation_check(dest_path) action = op_result = "" - if path == "/server/files/move": + if ep == "/server/files/move": if source_root not in FULL_ACCESS_ROOTS: raise self.server.error( f"Source path is read-only, cannot move: {source_root}") @@ -246,7 +238,7 @@ class FileManager: else: self.gcode_metadata.remove_file(src_rel_path) action = "move_item" - elif path == "/server/files/copy": + elif ep == "/server/files/copy": try: if os.path.isdir(source_path): op_result = shutil.copytree(source_path, dest_path) @@ -504,8 +496,8 @@ class FileManager: return simple_list return flist - async def _handle_file_delete(self, path, method, args): - file_path = args.get("path") + async def _handle_file_delete(self, web_request): + file_path = web_request.get_str("path") return await self.delete_file(file_path) async def delete_file(self, path):