From 8301096fd4da10a22e979975cc4cb6db152532cf Mon Sep 17 00:00:00 2001 From: Timothyxxx <384084775@qq.com> Date: Tue, 5 Dec 2023 13:10:06 +0800 Subject: [PATCH] Add open_file function in server --- desktop_env/server/main.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/desktop_env/server/main.py b/desktop_env/server/main.py index 93884ca..d584e86 100644 --- a/desktop_env/server/main.py +++ b/desktop_env/server/main.py @@ -9,7 +9,6 @@ import pyautogui from PIL import ImageGrab, Image from flask import Flask, request, jsonify, send_file - app = Flask(__name__) pyautogui.PAUSE = 0 @@ -84,6 +83,7 @@ def get_platform(): def get_cursor_position(): return pyautogui.position().x, pyautogui.position().y + @app.route("/setup/download_file", methods=['POST']) def download_file(): data = request.json @@ -92,7 +92,7 @@ def download_file(): if not url or not path: return "Path or URL not supplied!", 400 - + path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) @@ -103,15 +103,36 @@ def download_file(): response.raise_for_status() with open(path, 'wb') as f: - for chunk in response.iter_content(chunk_size=8192): - if chunk: + for chunk in response.iter_content(chunk_size=8192): + if chunk: f.write(chunk) return "File downloaded successfully" except requests.RequestException as e: print(f"Failed to download {url}. Retrying... ({max_retries - i - 1} attempts left)") - + return f"Failed to download {url}. No retries left. Error: {e}", 500 + +@app.route("/setup/open_file", methods=['POST']) +def open_file(): + data = request.json + path = data.get('path', None) + + if not path: + return "Path not supplied!", 400 + + path = Path(path) + + if not path.exists(): + return f"File not found: {path}", 404 + + try: + os.startfile(path) + return "File opened successfully" + except Exception as e: + return f"Failed to open {path}. Error: {e}", 500 + + if __name__ == '__main__': app.run(debug=True, host="0.0.0.0")