dangthr commited on
Commit
6129265
·
verified ·
1 Parent(s): fb2047d

Update remote_uploader.py

Browse files
Files changed (1) hide show
  1. remote_uploader.py +107 -99
remote_uploader.py CHANGED
@@ -5,130 +5,138 @@ import logging
5
  import sys
6
  import base64
7
  import os
8
- import argparse
9
 
10
  # 配置日志
11
  logging.basicConfig(level=logging.INFO, 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
- if server_url.startswith('http://'):
18
- self.ws_url = server_url.replace('http://', 'ws://') + '/ws/upload'
19
- elif server_url.startswith('https://'):
20
- self.ws_url = server_url.replace('https://', 'wss://') + '/ws/upload'
21
- else:
22
- self.ws_url = f"ws://{server_url}/ws/upload"
23
-
24
- self.api_key = api_key
25
- self.space_id = space_id
26
-
27
- async def upload_file(self, file_path):
28
- """使用WebSocket上传单个文件"""
29
- if not os.path.exists(file_path):
30
- print(f"文件不存在: {file_path}")
31
- return False
32
-
33
- filename = os.path.basename(file_path)
34
- print(f"正在上传文件: {filename}")
35
-
36
- try:
37
- # 读取文件内容并编码为base64
38
- with open(file_path, 'rb') as f:
39
- file_content = f.read()
40
- file_b64 = base64.b64encode(file_content).decode('utf-8')
41
-
42
- # 连接到WebSocket服务器
43
- async with websockets.connect(self.ws_url) as websocket:
44
- # 发送认证和文件数据
45
- upload_msg = {
46
- "type": "file_upload",
47
- "api_key": self.api_key,
48
- "space_id": self.space_id,
49
- "filename": filename,
50
- "content": file_b64
51
- }
52
-
53
- await websocket.send(json.dumps(upload_msg))
54
- logger.debug(f"发送文件上传请求: {filename}")
55
-
56
- # 等待服务器响应
57
- response = await websocket.recv()
58
- data = json.loads(response)
59
-
60
- if data.get("type") == "success":
61
- print(f"✅ 文件 '{filename}' 上传成功!")
62
- return True
63
- else:
64
- print(f"❌ 上传失败: {data.get('error', '未知错误')}")
65
- return False
66
-
67
- except Exception as e:
68
- print(f"上传文件 {filename} 时出错: {e}")
69
- return False
70
-
71
- async def upload_directory_once(upload_dir, server_url, api_key, space_id):
72
- """一次性扫描并上传目录中的所有文件"""
73
- if not os.path.exists(upload_dir):
74
- print(f"目录不存在: {upload_dir}")
75
- return
76
 
77
- print(f"🔍 开始扫描目录: {upload_dir}")
78
- print(f"📡 服务器地址: {server_url}")
79
- print(f"🔑 Space ID: {space_id}")
80
- print("-" * 50)
81
-
82
- uploader = WebSocketFileUploader(server_url, api_key, space_id)
83
 
84
  # 获取所有文件
85
- all_files = []
86
- for root, dirs, files in os.walk(upload_dir):
87
  for file in files:
88
  file_path = os.path.join(root, file)
89
  if os.path.isfile(file_path):
90
- all_files.append(file_path)
 
 
 
 
91
 
92
- if not all_files:
93
- print("📁 目录中没有找到任何文件")
94
- return
95
 
96
- print(f"📁 找到 {len(all_files)} 个文件,开始上传...")
 
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  success_count = 0
99
  failed_count = 0
100
 
101
- for file_path in all_files:
102
  try:
103
- if await uploader.upload_file(file_path):
104
- success_count += 1
105
- else:
106
- failed_count += 1
107
- # 稍微延迟一下,避免服务器压力过大
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  await asyncio.sleep(0.5)
 
109
  except Exception as e:
110
- print(f"上传文件 {file_path} 时发生异常: {e}")
111
  failed_count += 1
112
 
113
- print("-" * 50)
114
- print(f"📊 上传完成! 成功: {success_count}, 失败: {failed_count}")
115
 
116
- if success_count > 0:
117
- print("🎉 文件已成功上传到您的网盘!")
118
-
119
- return success_count, failed_count
120
 
121
- async def main():
122
- parser = argparse.ArgumentParser(description="WebSocket文件上传器 - 扫描并上传指定文件夹中的所有文件")
123
- parser.add_argument("api_key", help="您的 API 密钥")
124
- parser.add_argument("space_id", help="Space ID")
125
- parser.add_argument("--server", default="https://gkbtyo-rqvays-5001.preview.cloudstudio.work", help="服务器的 URL 地址")
126
- parser.add_argument("--upload-dir", default="output", help="要上传的目录 (默认: output)")
 
 
127
 
128
- args = parser.parse_args()
 
 
129
 
130
- # 开始上传
131
- await upload_directory_once(args.upload_dir, args.server, args.api_key, args.space_id)
132
 
133
  if __name__ == "__main__":
134
- asyncio.run(main())
 
5
  import sys
6
  import base64
7
  import os
8
+ import time
9
 
10
  # 配置日志
11
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
12
  logger = logging.getLogger(__name__)
13
 
14
+ async def upload_files_via_websocket(space_id, user_token, website_url):
15
+ """通过WebSocket上传output目录中的所有文件"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ output_dir = "output"
18
+ if not os.path.exists(output_dir):
19
+ logger.error(f"输出目录不存在: {output_dir}")
20
+ return False
 
 
21
 
22
  # 获取所有文件
23
+ files_to_upload = []
24
+ for root, dirs, files in os.walk(output_dir):
25
  for file in files:
26
  file_path = os.path.join(root, file)
27
  if os.path.isfile(file_path):
28
+ files_to_upload.append(file_path)
29
+
30
+ if not files_to_upload:
31
+ logger.info("输出目录中没有找到任何文件")
32
+ return True
33
 
34
+ logger.info(f"找到 {len(files_to_upload)} 个文件需要上传")
 
 
35
 
36
+ # 构建WebSocket URL
37
+ ws_url = website_url.replace('http://', 'ws://').replace('https://', 'wss://') + '/socket.io/?EIO=4&transport=websocket'
38
 
39
+ try:
40
+ # 连接到WebSocket服务器
41
+ logger.info(f"正在连接到 WebSocket 服务器: {ws_url}")
42
+
43
+ # 使用socket.io客户端协议
44
+ import socketio
45
+
46
+ sio = socketio.AsyncClient()
47
+
48
+ @sio.event
49
+ async def connect():
50
+ logger.info("WebSocket 连接成功")
51
+ # 注册客户端
52
+ await sio.emit('register_client', {
53
+ 'space_id': space_id,
54
+ 'user_token': user_token
55
+ })
56
+
57
+ @sio.event
58
+ async def registered(data):
59
+ logger.info(f"客户端注册成功: {data}")
60
+ # 开始上传文件
61
+ await upload_files(sio, files_to_upload, space_id, user_token)
62
+
63
+ @sio.event
64
+ async def upload_success(data):
65
+ logger.info(f"文件上传成功: {data}")
66
+
67
+ @sio.event
68
+ async def upload_error(data):
69
+ logger.error(f"文件上传失败: {data}")
70
+
71
+ @sio.event
72
+ async def error(data):
73
+ logger.error(f"WebSocket 错误: {data}")
74
+
75
+ @sio.event
76
+ async def disconnect():
77
+ logger.info("WebSocket 连接断开")
78
+
79
+ # 连接到服务器
80
+ await sio.connect(website_url)
81
+
82
+ # 等待所有文件上传完成
83
+ await sio.wait()
84
+
85
+ except Exception as e:
86
+ logger.error(f"WebSocket 连接或上传过程中发生错误: {e}")
87
+ return False
88
+
89
+ async def upload_files(sio, files_to_upload, space_id, user_token):
90
+ """上传文件列表"""
91
  success_count = 0
92
  failed_count = 0
93
 
94
+ for file_path in files_to_upload:
95
  try:
96
+ filename = os.path.basename(file_path)
97
+ logger.info(f"正在上传文件: {filename}")
98
+
99
+ # 读取文件内容并编码为base64
100
+ with open(file_path, 'rb') as f:
101
+ file_content = f.read()
102
+ file_b64 = base64.b64encode(file_content).decode('utf-8')
103
+
104
+ # 发送文件上传请求
105
+ await sio.emit('file_upload', {
106
+ 'space_id': space_id,
107
+ 'user_token': user_token,
108
+ 'filename': filename,
109
+ 'content': file_b64
110
+ })
111
+
112
+ success_count += 1
113
+ # 稍微延迟避免服务器压力过大
114
  await asyncio.sleep(0.5)
115
+
116
  except Exception as e:
117
+ logger.error(f"上传文件 {file_path} 时发生错误: {e}")
118
  failed_count += 1
119
 
120
+ logger.info(f"上传完成! 成功: {success_count}, 失败: {failed_count}")
 
121
 
122
+ # 上传完成后断开连接
123
+ await sio.disconnect()
 
 
124
 
125
+ def main():
126
+ if len(sys.argv) != 4:
127
+ print("用法: python3 websocket_uploader.py <space_id> <user_token> <website_url>")
128
+ sys.exit(1)
129
+
130
+ space_id = sys.argv[1]
131
+ user_token = sys.argv[2]
132
+ website_url = sys.argv[3]
133
 
134
+ logger.info(f"开始 WebSocket 文件上传")
135
+ logger.info(f"Space ID: {space_id}")
136
+ logger.info(f"Website URL: {website_url}")
137
 
138
+ # 运行异步上传
139
+ asyncio.run(upload_files_via_websocket(space_id, user_token, website_url))
140
 
141
  if __name__ == "__main__":
142
+ main()