Spaces:
Runtime error
Runtime error
Update remote_uploader.py
Browse files- remote_uploader.py +137 -75
remote_uploader.py
CHANGED
@@ -1,95 +1,157 @@
|
|
1 |
import asyncio
|
2 |
import websockets
|
3 |
-
import subprocess
|
4 |
import json
|
5 |
import logging
|
6 |
import sys
|
7 |
-
import urllib.parse
|
8 |
import base64
|
9 |
import os
|
10 |
-
import threading
|
11 |
-
import requests
|
12 |
from urllib.parse import urlparse
|
13 |
-
import argparse
|
14 |
|
15 |
-
#
|
16 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
17 |
logger = logging.getLogger(__name__)
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
all_files = []
|
83 |
-
for root, dirs, files in os.walk(
|
84 |
for file in files:
|
85 |
file_path = os.path.join(root, file)
|
86 |
if os.path.isfile(file_path):
|
87 |
all_files.append(file_path)
|
88 |
-
|
89 |
if not all_files:
|
90 |
-
print("目录中没有找到任何文件")
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
|
94 |
-
asyncio.run(
|
95 |
-
print("上传完成!")
|
|
|
1 |
import asyncio
|
2 |
import websockets
|
|
|
3 |
import json
|
4 |
import logging
|
5 |
import sys
|
|
|
6 |
import base64
|
7 |
import os
|
|
|
|
|
8 |
from urllib.parse import urlparse
|
|
|
9 |
|
10 |
+
# 配置日志
|
11 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
12 |
logger = logging.getLogger(__name__)
|
13 |
|
14 |
+
class WebSocketFileUploader:
|
15 |
+
def __init__(self, server_url, api_key, space_id):
|
16 |
+
# 将HTTP URL转换为WebSocket URL
|
17 |
+
parsed_url = urlparse(server_url)
|
18 |
+
scheme = 'wss' if parsed_url.scheme == 'https' else 'ws'
|
19 |
+
self.ws_url = f"{scheme}://{parsed_url.netloc}/ws/upload/{space_id}"
|
20 |
+
self.api_key = api_key
|
21 |
+
self.space_id = space_id
|
22 |
+
|
23 |
+
async def upload_file(self, file_path):
|
24 |
+
"""通过WebSocket上传单个文件"""
|
25 |
+
if not os.path.exists(file_path):
|
26 |
+
print(f"文件不存在: {file_path}")
|
27 |
+
return False
|
28 |
+
|
29 |
+
filename = os.path.basename(file_path)
|
30 |
+
print(f"正在上传文件: {filename}")
|
31 |
+
|
32 |
+
try:
|
33 |
+
# 读取文件内容并转换为base64
|
34 |
+
with open(file_path, 'rb') as f:
|
35 |
+
file_content = f.read()
|
36 |
+
file_b64 = base64.b64encode(file_content).decode('utf-8')
|
37 |
+
|
38 |
+
# 连接WebSocket并上传文件
|
39 |
+
async with websockets.connect(self.ws_url, ping_interval=20, ping_timeout=60) as websocket:
|
40 |
+
# 发送认证信息
|
41 |
+
auth_msg = {
|
42 |
+
"type": "auth",
|
43 |
+
"api_key": self.api_key,
|
44 |
+
"space_id": self.space_id
|
45 |
+
}
|
46 |
+
await websocket.send(json.dumps(auth_msg))
|
47 |
+
|
48 |
+
# 等待认证响应
|
49 |
+
response = await websocket.recv()
|
50 |
+
auth_response = json.loads(response)
|
51 |
+
|
52 |
+
if auth_response.get("type") != "auth_success":
|
53 |
+
print(f"❌ 认证失败: {auth_response.get('message', '未知错误')}")
|
54 |
+
return False
|
55 |
+
|
56 |
+
# 发送文件
|
57 |
+
file_msg = {
|
58 |
+
"type": "file_upload",
|
59 |
+
"filename": filename,
|
60 |
+
"content": file_b64,
|
61 |
+
"space_id": self.space_id
|
62 |
+
}
|
63 |
+
await websocket.send(json.dumps(file_msg))
|
64 |
+
|
65 |
+
# 等待上传响应
|
66 |
+
response = await websocket.recv()
|
67 |
+
upload_response = json.loads(response)
|
68 |
+
|
69 |
+
if upload_response.get("type") == "upload_success":
|
70 |
+
print(f"✅ 文件 '{filename}' 上传成功!")
|
71 |
+
return True
|
72 |
+
else:
|
73 |
+
print(f"❌ 上传失败: {upload_response.get('message', '未知错误')}")
|
74 |
+
return False
|
75 |
+
|
76 |
+
except Exception as e:
|
77 |
+
print(f"上传文件时出错: {e}")
|
78 |
+
return False
|
79 |
|
80 |
+
async def upload_directory_once(upload_dir, server_url, api_key, space_id):
|
81 |
+
"""一次性扫描并上传目录中的所有文件"""
|
82 |
+
if not os.path.exists(upload_dir):
|
83 |
+
print(f"目录不存在: {upload_dir}")
|
84 |
+
return
|
85 |
+
|
86 |
+
print(f"🔍 开始扫描目录: {upload_dir}")
|
87 |
+
print(f"📡 服务器地址: {server_url}")
|
88 |
+
print(f"🔑 Space ID: {space_id}")
|
89 |
+
print("-" * 50)
|
90 |
+
|
91 |
+
uploader = WebSocketFileUploader(server_url, api_key, space_id)
|
92 |
+
|
93 |
+
# 获取所有文件
|
94 |
all_files = []
|
95 |
+
for root, dirs, files in os.walk(upload_dir):
|
96 |
for file in files:
|
97 |
file_path = os.path.join(root, file)
|
98 |
if os.path.isfile(file_path):
|
99 |
all_files.append(file_path)
|
100 |
+
|
101 |
if not all_files:
|
102 |
+
print("📁 目录中没有找到任何文件")
|
103 |
+
return
|
104 |
+
|
105 |
+
print(f"📁 找到 {len(all_files)} 个文件,开始上传...")
|
106 |
+
|
107 |
+
success_count = 0
|
108 |
+
failed_count = 0
|
109 |
+
|
110 |
+
for file_path in all_files:
|
111 |
+
try:
|
112 |
+
if await uploader.upload_file(file_path):
|
113 |
+
success_count += 1
|
114 |
+
else:
|
115 |
+
failed_count += 1
|
116 |
+
# 稍微延迟一下,避免服务器压力过大
|
117 |
+
await asyncio.sleep(0.5)
|
118 |
+
except Exception as e:
|
119 |
+
print(f"上传文件 {file_path} 时发生异常: {e}")
|
120 |
+
failed_count += 1
|
121 |
+
|
122 |
+
print("-" * 50)
|
123 |
+
print(f"📊 上传完成! 成功: {success_count}, 失败: {failed_count}")
|
124 |
+
|
125 |
+
if success_count > 0:
|
126 |
+
print("🎉 文件已成功上传到您的网盘!")
|
127 |
+
|
128 |
+
return success_count, failed_count
|
129 |
+
|
130 |
+
async def main():
|
131 |
+
if len(sys.argv) < 3:
|
132 |
+
print("使用方法: python3 remote_uploader.py <api_key> <space_id> [--server <server_url>] [--upload-dir <directory>]")
|
133 |
+
sys.exit(1)
|
134 |
+
|
135 |
+
api_key = sys.argv[1]
|
136 |
+
space_id = sys.argv[2]
|
137 |
+
|
138 |
+
# 解析可选参数
|
139 |
+
server_url = "https://gkbtyo-rqvays-5001.preview.cloudstudio.work"
|
140 |
+
upload_dir = "output"
|
141 |
+
|
142 |
+
i = 3
|
143 |
+
while i < len(sys.argv):
|
144 |
+
if sys.argv[i] == "--server" and i + 1 < len(sys.argv):
|
145 |
+
server_url = sys.argv[i + 1]
|
146 |
+
i += 2
|
147 |
+
elif sys.argv[i] == "--upload-dir" and i + 1 < len(sys.argv):
|
148 |
+
upload_dir = sys.argv[i + 1]
|
149 |
+
i += 2
|
150 |
+
else:
|
151 |
+
i += 1
|
152 |
+
|
153 |
+
# 开始异步上传
|
154 |
+
await upload_directory_once(upload_dir, server_url, api_key, space_id)
|
155 |
|
156 |
+
if __name__ == "__main__":
|
157 |
+
asyncio.run(main())
|
|