Add open_file function in server

This commit is contained in:
Timothyxxx
2023-12-05 13:10:06 +08:00
parent a5dd4408fe
commit 8301096fd4

View File

@@ -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")