From 487d52f08a7cf2fda2ff9e8e6879edaf42f1063d Mon Sep 17 00:00:00 2001 From: bigcat88 Date: Mon, 6 Jan 2025 13:55:04 +0200 Subject: [PATCH] "move_userdata" endpoint placed before other "userdata" endpoints Signed-off-by: bigcat88 --- app/user_manager.py | 102 ++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/app/user_manager.py b/app/user_manager.py index 9bde2860a..4050a7bd9 100644 --- a/app/user_manager.py +++ b/app/user_manager.py @@ -211,6 +211,57 @@ class UserManager(): return path + @routes.post("/userdata/{file}/move/{dest}") + async def move_userdata(request): + """ + Move or rename a user data file. + + This endpoint handles moving or renaming files within a user's data directory, with options for + controlling overwrite behavior and response format. + + Path Parameters: + - file: The source file path (URL encoded if necessary) + - dest: The destination file path (URL encoded if necessary) + + Query Parameters: + - overwrite (optional): If "false", prevents overwriting existing files. Defaults to "true". + - full_info (optional): If "true", returns detailed file information (path, size, modified time). + If "false", returns only the relative file path. + + Returns: + - 400: If either 'file' or 'dest' parameter is missing + - 403: If either requested path is not allowed + - 404: If the source file does not exist + - 409: If overwrite=false and the destination file already exists + - 200: JSON response with either: + - Full file information (if full_info=true) + - Relative file path (if full_info=false) + """ + source = get_user_data_path(request, check_exists=True) + if not isinstance(source, str): + return source + + dest = get_user_data_path(request, check_exists=False, param="dest") + if not isinstance(source, str): + return dest + + overwrite = request.query.get("overwrite", 'true') != "false" + full_info = request.query.get('full_info', 'false').lower() == "true" + + if not overwrite and os.path.exists(dest): + return web.Response(status=409, text="File already exists") + + logging.info(f"moving '{source}' -> '{dest}'") + shutil.move(source, dest) + + user_path = self.get_request_user_filepath(request, None) + if full_info: + resp = get_file_info(dest, user_path) + else: + resp = os.path.relpath(dest, user_path) + + return web.json_response(resp) + @routes.get("/userdata/{file:.+}") async def getuserdata(request): path = get_user_data_path(request, check_exists=True) @@ -277,54 +328,3 @@ class UserManager(): os.remove(path) return web.Response(status=204) - - @routes.post("/userdata/{file}/move/{dest}") - async def move_userdata(request): - """ - Move or rename a user data file. - - This endpoint handles moving or renaming files within a user's data directory, with options for - controlling overwrite behavior and response format. - - Path Parameters: - - file: The source file path (URL encoded if necessary) - - dest: The destination file path (URL encoded if necessary) - - Query Parameters: - - overwrite (optional): If "false", prevents overwriting existing files. Defaults to "true". - - full_info (optional): If "true", returns detailed file information (path, size, modified time). - If "false", returns only the relative file path. - - Returns: - - 400: If either 'file' or 'dest' parameter is missing - - 403: If either requested path is not allowed - - 404: If the source file does not exist - - 409: If overwrite=false and the destination file already exists - - 200: JSON response with either: - - Full file information (if full_info=true) - - Relative file path (if full_info=false) - """ - source = get_user_data_path(request, check_exists=True) - if not isinstance(source, str): - return source - - dest = get_user_data_path(request, check_exists=False, param="dest") - if not isinstance(source, str): - return dest - - overwrite = request.query.get("overwrite", 'true') != "false" - full_info = request.query.get('full_info', 'false').lower() == "true" - - if not overwrite and os.path.exists(dest): - return web.Response(status=409, text="File already exists") - - logging.info(f"moving '{source}' -> '{dest}'") - shutil.move(source, dest) - - user_path = self.get_request_user_filepath(request, None) - if full_info: - resp = get_file_info(dest, user_path) - else: - resp = os.path.relpath(dest, user_path) - - return web.json_response(resp)