dangthr commited on
Commit
fad568b
·
verified ·
1 Parent(s): 06ee46e

Update remote_uploader.py

Browse files
Files changed (1) hide show
  1. remote_uploader.py +64 -54
remote_uploader.py CHANGED
@@ -1,16 +1,31 @@
1
- import requests
2
- import argparse
 
 
 
 
3
  import os
4
- import time
 
 
 
 
5
 
6
- class FileUploader:
7
  def __init__(self, server_url, api_key, space_id):
8
- self.server_url = server_url
 
 
 
 
 
 
 
9
  self.api_key = api_key
10
  self.space_id = space_id
11
 
12
- def upload_file(self, file_path):
13
- """上传单个文件"""
14
  if not os.path.exists(file_path):
15
  print(f"文件不存在: {file_path}")
16
  return False
@@ -18,65 +33,57 @@ class FileUploader:
18
  filename = os.path.basename(file_path)
19
  print(f"正在上传文件: {filename}")
20
 
21
- # 构建完整的 API 端点 URL
22
- upload_url = f"{self.server_url.rstrip('/')}/api/remote_upload"
23
-
24
- # 准备请求头和数据
25
- headers = {
26
- 'X-API-Key': self.api_key
27
- }
28
- data = {
29
- 'space_id': self.space_id
30
- }
31
-
32
- # 读取文件内容并上传
33
  try:
 
34
  with open(file_path, 'rb') as f:
35
- files = {
36
- 'file': (filename, f)
 
 
 
 
 
 
 
 
 
 
37
  }
38
 
39
- # 发送 POST 请求
40
- response = requests.post(upload_url, headers=headers, data=data, files=files)
 
 
 
 
41
 
42
- # 打印服务器响应
43
- print(f"服务器响应 ({response.status_code}):")
44
- try:
45
- result = response.json()
46
- print(result)
47
- if response.status_code == 200:
48
- print(f"✅ 文件 '{filename}' 上传成功!")
49
- return True
50
- else:
51
- print(f"❌ 上传失败: {result.get('error', '未知错误')}")
52
- return False
53
- except requests.exceptions.JSONDecodeError:
54
- print(response.text)
55
  return False
56
 
57
- except IOError as e:
58
- print(f"读取文件时出错: {e}")
59
- return False
60
- except requests.exceptions.RequestException as e:
61
- print(f"请求时出错: {e}")
62
  return False
63
 
64
- def upload_directory_once(watch_dir, server_url, api_key, space_id):
65
  """一次性扫描并上传目录中的所有文件"""
66
- if not os.path.exists(watch_dir):
67
- print(f"目录不存在: {watch_dir}")
68
  return
69
 
70
- print(f"🔍 开始扫描目录: {watch_dir}")
71
  print(f"📡 服务器地址: {server_url}")
72
  print(f"🔑 Space ID: {space_id}")
73
  print("-" * 50)
74
 
75
- uploader = FileUploader(server_url, api_key, space_id)
76
 
77
  # 获取所有文件
78
  all_files = []
79
- for root, dirs, files in os.walk(watch_dir):
80
  for file in files:
81
  file_path = os.path.join(root, file)
82
  if os.path.isfile(file_path):
@@ -93,12 +100,12 @@ def upload_directory_once(watch_dir, server_url, api_key, space_id):
93
 
94
  for file_path in all_files:
95
  try:
96
- if uploader.upload_file(file_path):
97
  success_count += 1
98
  else:
99
  failed_count += 1
100
  # 稍微延迟一下,避免服务器压力过大
101
- time.sleep(0.5)
102
  except Exception as e:
103
  print(f"上传文件 {file_path} 时发生异常: {e}")
104
  failed_count += 1
@@ -111,14 +118,17 @@ def upload_directory_once(watch_dir, server_url, api_key, space_id):
111
 
112
  return success_count, failed_count
113
 
114
- if __name__ == "__main__":
115
- parser = argparse.ArgumentParser(description="一次性文件上传器 - 扫描并上传指定文件夹中的所有文件")
116
  parser.add_argument("api_key", help="您的 API 密钥")
117
  parser.add_argument("space_id", help="Space ID")
118
- parser.add_argument("--server", default="http://127.0.0.1:5001", help="服务器的 URL 地址 (默认: http://127.0.0.1:5001)")
119
  parser.add_argument("--upload-dir", default="output", help="要上传的目录 (默认: output)")
120
 
121
  args = parser.parse_args()
122
 
123
- # 开始一次性上传
124
- upload_directory_once(args.upload_dir, args.server, args.api_key, args.space_id)
 
 
 
 
1
+ import asyncio
2
+ import websockets
3
+ import json
4
+ 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
 
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):
 
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
 
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())