dangthr commited on
Commit
5e8a249
·
verified ·
1 Parent(s): 76b5b20

Update remote_uploader.py

Browse files
Files changed (1) hide show
  1. remote_uploader.py +87 -147
remote_uploader.py CHANGED
@@ -1,157 +1,97 @@
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())
 
1
  import asyncio
2
  import websockets
 
 
 
 
3
  import os
4
+ import time
5
+ import base64
6
+ import logging
7
+ import argparse
8
+ import json
9
+ import urllib.parse
10
 
11
+ # Configure logging
12
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
13
  logger = logging.getLogger(__name__)
14
 
15
+ async def upload_directory(space_id, api_key, upload_dir):
16
+ # 使用查询参数传递 api_key
17
+ encoded_secret = urllib.parse.quote(api_key)
18
+ uri = f"wss://remote-terminal-worker.nianxi4563.workers.dev/terminal/{space_id}?secret={encoded_secret}"
19
+ logger.info(f"Attempting to connect to {uri}")
20
+
21
+ try:
22
+ async with websockets.connect(uri, ping_interval=20, ping_timeout=60) as websocket:
23
+ logger.info("Connected to WebSocket")
24
+
25
+ # 发送机器注册信息(尽管去除 machine_id,但保持兼容)
26
+ machine_info = {"type": "machine", "card_id": space_id}
27
+ await websocket.send(json.dumps(machine_info))
28
+ logger.debug(f"Sent machine registration: {machine_info}")
29
+
30
+ # 不等待 machine_id,直接继续
31
+
32
+ # 获取所有文件
33
+ all_files = []
34
+ for root, dirs, files in os.walk(upload_dir):
35
+ for file in files:
36
+ file_path = os.path.join(root, file)
37
+ if os.path.isfile(file_path):
38
+ all_files.append(file_path)
39
+
40
+ if not all_files:
41
+ logger.info("No files found in directory")
42
+ return 0, 0
43
+
44
+ logger.info(f"Found {len(all_files)} files, starting upload...")
45
+
46
+ success_count = 0
47
+ failed_count = 0
48
+
49
+ for file_path in all_files:
50
+ try:
51
+ with open(file_path, 'rb') as f:
52
+ file_content = f.read()
53
+ file_b64 = base64.b64encode(file_content).decode('utf-8')
54
+ file_msg = {
55
+ "type": "file_upload",
56
+ "filename": os.path.basename(file_path),
57
+ "content": file_b64
58
+ }
59
+ await websocket.send(json.dumps(file_msg))
60
+ logger.info(f"Uploaded file: {file_path}")
61
+ success_count += 1
62
+ except Exception as e:
63
+ logger.error(f"Error uploading file {file_path}: {e}")
64
+ failed_count += 1
65
+ # 稍微延迟,避免服务器压力过大
66
+ await asyncio.sleep(0.5)
67
+
68
+ logger.info(f"Upload completed! Success: {success_count}, Failed: {failed_count}")
69
+ return success_count, failed_count
70
+
71
+ except Exception as e:
72
+ logger.error(f"Failed to connect or upload: {e}")
73
+ return 0, len(os.listdir(upload_dir)) if os.path.exists(upload_dir) else 0
74
+
75
+ if __name__ == "__main__":
76
+ parser = argparse.ArgumentParser(description="一次性文件上传器 - 使用 WebSocket 扫描并上传指定文件夹中的所有文件")
77
+ parser.add_argument("api_key", help="您的 API 密钥 (作为用户 token)")
78
+ parser.add_argument("space_id", help="Space ID (作为 card_id)")
79
+ parser.add_argument("--upload-dir", default="output", help="要上传的目录 (默认: output)")
80
+
81
+ args = parser.parse_args()
82
+
83
+ if not os.path.exists(args.upload_dir):
84
+ print(f"目录不存在: {args.upload_dir}")
85
+ exit(1)
86
+
87
+ print(f"🔍 开始扫描目录: {args.upload_dir}")
88
+ print(f"🔑 Space ID (card_id): {args.space_id}")
 
89
  print("-" * 50)
90
+
91
+ success, failed = asyncio.run(upload_directory(args.space_id, args.api_key, args.upload_dir))
92
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  print("-" * 50)
94
+ print(f"📊 上传完成! 成功: {success}, 失败: {failed}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ if success > 0:
97
+ print("🎉 文件已成功上传到您的网盘!")