|
@@ -1,29 +1,57 @@
|
|
|
import os
|
|
|
import time
|
|
|
+from threading import Timer
|
|
|
|
|
|
from watchdog.observers import Observer
|
|
|
from watchdog.events import FileSystemEventHandler
|
|
|
from tools.logger_handle import logger
|
|
|
-from tools.oss_client import MinioClient, oss_handle
|
|
|
|
|
|
|
|
|
class FileSaveHandler(FileSystemEventHandler):
|
|
|
def __init__(self, serve_client):
|
|
|
self.serve_client = serve_client
|
|
|
+ self.debounce_timers = {}
|
|
|
+ self.upload_cache = {} # 文件路径 → 上次上传时间戳
|
|
|
+ self.debounce_delay = 5 # 延迟秒数
|
|
|
super().__init__()
|
|
|
|
|
|
def on_modified(self, event):
|
|
|
+ if event.is_directory:
|
|
|
+ return
|
|
|
+ filepath = event.src_path
|
|
|
ext = os.path.splitext(event.src_path)[-1]
|
|
|
if '~$' in event.src_path:
|
|
|
return
|
|
|
|
|
|
- if ext in ['.docx', '.doc', '.ppt', '.pptx', '.xls', '.xlsx']:
|
|
|
- logger.info(f"[文件修改] 文件已保存: {event.src_path}, 执行上传操作。")
|
|
|
- res = self.serve_client.upload_file(event.src_path)
|
|
|
+ if ext not in ['.docx', '.doc', '.ppt', '.pptx', '.xls', '.xlsx']:
|
|
|
+ return
|
|
|
+
|
|
|
+ def do_upload():
|
|
|
+ if not os.path.exists(filepath):
|
|
|
+ return
|
|
|
+
|
|
|
+ mtime = os.path.getmtime(filepath)
|
|
|
+ if self.upload_cache.get(filepath) == mtime:
|
|
|
+ logger.debug(f"[重复上传跳过] {filepath}")
|
|
|
+ return
|
|
|
+
|
|
|
+ logger.info(f"[文件修改] 文件已保存: {filepath}, 执行上传操作。")
|
|
|
+ res = self.serve_client.upload_file(filepath)
|
|
|
if res:
|
|
|
- logger.info(f'文件{event.src_path}上传成功')
|
|
|
+ logger.info(f'文件{filepath}上传成功')
|
|
|
else:
|
|
|
- logger.info(f'文件{event.src_path}上传失败')
|
|
|
+ logger.info(f'文件{filepath}上传失败')
|
|
|
+
|
|
|
+ self.upload_cache[filepath] = mtime
|
|
|
+ self.debounce_timers.pop(filepath, None)
|
|
|
+
|
|
|
+ if filepath in self.debounce_timers:
|
|
|
+ self.debounce_timers[filepath].cancel()
|
|
|
+
|
|
|
+ # 创建新的延迟上传任务
|
|
|
+ timer = Timer(self.debounce_delay, do_upload)
|
|
|
+ self.debounce_timers[filepath] = timer
|
|
|
+ timer.start()
|
|
|
|
|
|
|
|
|
def start_watchdog(serve_client, work_path):
|
|
@@ -31,7 +59,7 @@ def start_watchdog(serve_client, work_path):
|
|
|
observer = Observer()
|
|
|
observer.schedule(event_handler, work_path, recursive=True)
|
|
|
observer.start()
|
|
|
- logger.info('watch dog start')
|
|
|
+ logger.info(f'watch dog start {serve_client}')
|
|
|
try:
|
|
|
while True:
|
|
|
time.sleep(1)
|