Spaces:
Runtime error
Runtime error
Update remote_uploader.py
Browse files- remote_uploader.py +77 -16
remote_uploader.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# remote_uploader.py
|
2 |
import requests
|
3 |
import argparse
|
4 |
import os
|
@@ -13,6 +12,26 @@ class FileUploadHandler(FileSystemEventHandler):
|
|
13 |
self.space_id = space_id
|
14 |
self.uploaded_files = set() # 跟踪已上传的文件
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def on_created(self, event):
|
17 |
"""当新文件被创建时触发"""
|
18 |
if not event.is_directory:
|
@@ -30,7 +49,9 @@ class FileUploadHandler(FileSystemEventHandler):
|
|
30 |
def upload_file(self, file_path):
|
31 |
"""上传单个文件"""
|
32 |
if not os.path.exists(file_path):
|
33 |
-
|
|
|
|
|
34 |
return
|
35 |
|
36 |
filename = os.path.basename(file_path)
|
@@ -39,8 +60,13 @@ class FileUploadHandler(FileSystemEventHandler):
|
|
39 |
if file_path in self.uploaded_files:
|
40 |
return
|
41 |
|
42 |
-
|
43 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# 构建完整的 API 端点 URL
|
46 |
upload_url = f"{self.server_url.rstrip('/')}/api/remote_upload"
|
@@ -64,22 +90,36 @@ class FileUploadHandler(FileSystemEventHandler):
|
|
64 |
response = requests.post(upload_url, headers=headers, data=data, files=files)
|
65 |
|
66 |
# 打印服务器响应
|
67 |
-
|
|
|
|
|
|
|
68 |
try:
|
69 |
result = response.json()
|
70 |
print(result)
|
|
|
|
|
71 |
if response.status_code == 200:
|
72 |
self.uploaded_files.add(file_path)
|
73 |
-
|
|
|
|
|
74 |
else:
|
75 |
-
|
|
|
|
|
76 |
except requests.exceptions.JSONDecodeError:
|
77 |
print(response.text)
|
|
|
78 |
|
79 |
except IOError as e:
|
80 |
-
|
|
|
|
|
81 |
except requests.exceptions.RequestException as e:
|
82 |
-
|
|
|
|
|
83 |
|
84 |
def monitor_directory(watch_dir, server_url, api_key, space_id):
|
85 |
"""监听目录变化"""
|
@@ -87,21 +127,40 @@ def monitor_directory(watch_dir, server_url, api_key, space_id):
|
|
87 |
print(f"创建监听目录: {watch_dir}")
|
88 |
os.makedirs(watch_dir, exist_ok=True)
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
print("-" * 50)
|
|
|
95 |
|
96 |
# 首先上传目录中已���在的文件
|
97 |
-
handler = FileUploadHandler(server_url, api_key, space_id)
|
98 |
existing_files = [f for f in os.listdir(watch_dir) if os.path.isfile(os.path.join(watch_dir, f))]
|
99 |
if existing_files:
|
100 |
-
|
|
|
|
|
|
|
101 |
for filename in existing_files:
|
102 |
file_path = os.path.join(watch_dir, filename)
|
103 |
handler.upload_file(file_path)
|
|
|
104 |
print("-" * 50)
|
|
|
105 |
|
106 |
# 创建观察者并开始监听
|
107 |
observer = Observer()
|
@@ -112,7 +171,9 @@ def monitor_directory(watch_dir, server_url, api_key, space_id):
|
|
112 |
while True:
|
113 |
time.sleep(1)
|
114 |
except KeyboardInterrupt:
|
115 |
-
|
|
|
|
|
116 |
observer.stop()
|
117 |
|
118 |
observer.join()
|
|
|
|
|
1 |
import requests
|
2 |
import argparse
|
3 |
import os
|
|
|
12 |
self.space_id = space_id
|
13 |
self.uploaded_files = set() # 跟踪已上传的文件
|
14 |
|
15 |
+
def send_log(self, message):
|
16 |
+
"""发送日志到服务器"""
|
17 |
+
try:
|
18 |
+
log_url = f"{self.server_url.rstrip('/')}/api/remote_log"
|
19 |
+
headers = {
|
20 |
+
'X-API-Key': self.api_key,
|
21 |
+
'Content-Type': 'application/json'
|
22 |
+
}
|
23 |
+
data = {
|
24 |
+
'space_id': self.space_id,
|
25 |
+
'message': message,
|
26 |
+
'timestamp': time.time()
|
27 |
+
}
|
28 |
+
|
29 |
+
response = requests.post(log_url, headers=headers, json=data, timeout=5)
|
30 |
+
if response.status_code != 200:
|
31 |
+
print(f"发送日志失败: {response.status_code}")
|
32 |
+
except Exception as e:
|
33 |
+
print(f"发送日志异常: {e}")
|
34 |
+
|
35 |
def on_created(self, event):
|
36 |
"""当新文件被创建时触发"""
|
37 |
if not event.is_directory:
|
|
|
49 |
def upload_file(self, file_path):
|
50 |
"""上传单个文件"""
|
51 |
if not os.path.exists(file_path):
|
52 |
+
message = f"文件不存在: {file_path}"
|
53 |
+
print(message)
|
54 |
+
self.send_log(message)
|
55 |
return
|
56 |
|
57 |
filename = os.path.basename(file_path)
|
|
|
60 |
if file_path in self.uploaded_files:
|
61 |
return
|
62 |
|
63 |
+
message = f"检测到新文件: {filename}"
|
64 |
+
print(message)
|
65 |
+
self.send_log(message)
|
66 |
+
|
67 |
+
message = f"正在上传到服务器..."
|
68 |
+
print(message)
|
69 |
+
self.send_log(message)
|
70 |
|
71 |
# 构建完整的 API 端点 URL
|
72 |
upload_url = f"{self.server_url.rstrip('/')}/api/remote_upload"
|
|
|
90 |
response = requests.post(upload_url, headers=headers, data=data, files=files)
|
91 |
|
92 |
# 打印服务器响应
|
93 |
+
message = f"服务器响应 ({response.status_code}):"
|
94 |
+
print(message)
|
95 |
+
self.send_log(message)
|
96 |
+
|
97 |
try:
|
98 |
result = response.json()
|
99 |
print(result)
|
100 |
+
self.send_log(str(result))
|
101 |
+
|
102 |
if response.status_code == 200:
|
103 |
self.uploaded_files.add(file_path)
|
104 |
+
success_msg = f"✅ 文件 '{filename}' 上传成功!"
|
105 |
+
print(success_msg)
|
106 |
+
self.send_log(success_msg)
|
107 |
else:
|
108 |
+
error_msg = f"❌ 上传失败: {result.get('error', '未知错误')}"
|
109 |
+
print(error_msg)
|
110 |
+
self.send_log(error_msg)
|
111 |
except requests.exceptions.JSONDecodeError:
|
112 |
print(response.text)
|
113 |
+
self.send_log(response.text)
|
114 |
|
115 |
except IOError as e:
|
116 |
+
error_msg = f"读取文件时出错: {e}"
|
117 |
+
print(error_msg)
|
118 |
+
self.send_log(error_msg)
|
119 |
except requests.exceptions.RequestException as e:
|
120 |
+
error_msg = f"请求时出错: {e}"
|
121 |
+
print(error_msg)
|
122 |
+
self.send_log(error_msg)
|
123 |
|
124 |
def monitor_directory(watch_dir, server_url, api_key, space_id):
|
125 |
"""监听目录变化"""
|
|
|
127 |
print(f"创建监听目录: {watch_dir}")
|
128 |
os.makedirs(watch_dir, exist_ok=True)
|
129 |
|
130 |
+
handler = FileUploadHandler(server_url, api_key, space_id)
|
131 |
+
|
132 |
+
start_msg = f"🔍 开始监听目录: {watch_dir}"
|
133 |
+
print(start_msg)
|
134 |
+
handler.send_log(start_msg)
|
135 |
+
|
136 |
+
server_msg = f"📡 服务器地址: {server_url}"
|
137 |
+
print(server_msg)
|
138 |
+
handler.send_log(server_msg)
|
139 |
+
|
140 |
+
space_msg = f"🔑 Space ID: {space_id}"
|
141 |
+
print(space_msg)
|
142 |
+
handler.send_log(space_msg)
|
143 |
+
|
144 |
+
wait_msg = "等待新文件..."
|
145 |
+
print(wait_msg)
|
146 |
+
handler.send_log(wait_msg)
|
147 |
+
|
148 |
print("-" * 50)
|
149 |
+
handler.send_log("-" * 50)
|
150 |
|
151 |
# 首先上传目录中已���在的文件
|
|
|
152 |
existing_files = [f for f in os.listdir(watch_dir) if os.path.isfile(os.path.join(watch_dir, f))]
|
153 |
if existing_files:
|
154 |
+
existing_msg = f"发现 {len(existing_files)} 个已存在的文件,开始上传..."
|
155 |
+
print(existing_msg)
|
156 |
+
handler.send_log(existing_msg)
|
157 |
+
|
158 |
for filename in existing_files:
|
159 |
file_path = os.path.join(watch_dir, filename)
|
160 |
handler.upload_file(file_path)
|
161 |
+
|
162 |
print("-" * 50)
|
163 |
+
handler.send_log("-" * 50)
|
164 |
|
165 |
# 创建观察者并开始监听
|
166 |
observer = Observer()
|
|
|
171 |
while True:
|
172 |
time.sleep(1)
|
173 |
except KeyboardInterrupt:
|
174 |
+
stop_msg = "\n停止监听..."
|
175 |
+
print(stop_msg)
|
176 |
+
handler.send_log(stop_msg)
|
177 |
observer.stop()
|
178 |
|
179 |
observer.join()
|