import requests import json import os CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config.json') def load_config(): if os.path.exists(CONFIG_PATH): with open(CONFIG_PATH, 'r') as f: return json.load(f) return {"server_url": "http://localhost:5000", "default_expiry": "24h"} def save_config(config): with open(CONFIG_PATH, 'w') as f: json.dump(config, f, indent=4) def upload_file(filepath, expiry='24h', server_url=None, progress_callback=None): if server_url is None: config = load_config() server_url = config.get('server_url', 'http://localhost:5000') url = f"{server_url}/api/upload" with open(filepath, 'rb') as f: files = {'file': (os.path.basename(filepath), f)} data = {'expiry': expiry} response = requests.post(url, files=files, data=data) if response.status_code == 200: return response.json() else: error_msg = response.json().get('error', '未知错误') raise Exception(f"上传失败: {error_msg}") def get_file_info(file_id, server_url=None): if server_url is None: config = load_config() server_url = config.get('server_url', 'http://localhost:5000') url = f"{server_url}/api/file/{file_id}" response = requests.get(url) if response.status_code == 200: return response.json() else: return None