|
@@ -3,7 +3,7 @@ import time
|
|
|
from threading import Timer
|
|
|
|
|
|
from watchdog.observers import Observer
|
|
|
-from watchdog.events import FileSystemEventHandler
|
|
|
+from watchdog.events import FileSystemEventHandler, FileSystemEvent
|
|
|
from tools.logger_handle import logger
|
|
|
|
|
|
|
|
@@ -13,8 +13,30 @@ class FileSaveHandler(FileSystemEventHandler):
|
|
|
self.debounce_timers = {}
|
|
|
self.upload_cache = {} # 文件路径 → 上次上传时间戳
|
|
|
self.debounce_delay = 5 # 延迟秒数
|
|
|
+ self._event_cache = {}
|
|
|
super().__init__()
|
|
|
|
|
|
+ def _should_process(self, event_path):
|
|
|
+ now = time.time()
|
|
|
+ last_time = self._event_cache.get(event_path, 0)
|
|
|
+ if now - last_time < 5: # 5秒内重复事件,忽略
|
|
|
+ return False
|
|
|
+ self._event_cache[event_path] = now
|
|
|
+ return True
|
|
|
+
|
|
|
+ def on_created(self, event: FileSystemEvent) -> None:
|
|
|
+ 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 not in ['.docx', '.doc', '.ppt', '.pptx', '.xls', '.xlsx']:
|
|
|
+ return
|
|
|
+
|
|
|
+ self._should_process(event.src_path)
|
|
|
+
|
|
|
def on_modified(self, event):
|
|
|
if event.is_directory:
|
|
|
return
|
|
@@ -26,6 +48,9 @@ class FileSaveHandler(FileSystemEventHandler):
|
|
|
if ext not in ['.docx', '.doc', '.ppt', '.pptx', '.xls', '.xlsx']:
|
|
|
return
|
|
|
|
|
|
+ if not self._should_process(event.src_path):
|
|
|
+ return
|
|
|
+
|
|
|
def do_upload():
|
|
|
if not os.path.exists(filepath):
|
|
|
return
|
|
@@ -37,6 +62,12 @@ class FileSaveHandler(FileSystemEventHandler):
|
|
|
|
|
|
logger.info(f"[文件修改] 文件已保存: {filepath}, 执行上传操作。")
|
|
|
res = self.serve_client.upload_file(filepath)
|
|
|
+ metadata = self.serve_client.load_metadata(filepath + '.metadata')
|
|
|
+ os.remove(filepath)
|
|
|
+ os.remove(filepath + '.metadata')
|
|
|
+ storage_path = os.path.dirname(filepath)
|
|
|
+ self.serve_client.download_file(metadata['file_id'], storage_path)
|
|
|
+
|
|
|
if res:
|
|
|
logger.info(f'文件{filepath}上传成功')
|
|
|
else:
|