diff --git a/client-file-collector/README.md b/client-file-collector/README.md index 269f953..cdee350 100644 --- a/client-file-collector/README.md +++ b/client-file-collector/README.md @@ -26,7 +26,8 @@ - 每轮轮询会扫描每行配置的本地目录。 - 仅同步新增或修改过的文件(避免重复上传同一文件)。 -- 不会删除本地文件。 +- 单个文件上传成功后,会将该文件从本地目录转移至备份目录(源目录名 + `_bak`,例如 `D:\sbFile\DRFID02004` -> `D:\sbFile\DRFID02004_bak`),源目录中对应文件即被移除。 +- 若备份目录中已存在同名文件,会自动在文件名后追加时间戳避免覆盖。 - Windows 下首次启动 `QMSFileCollector.exe` 时,会自动写入当前用户自启动项(优先写入 `Startup` 启动文件夹,失败时回退注册表 `HKCU\Software\Microsoft\Windows\CurrentVersion\Run`)。 ## 打包步骤 diff --git a/client-file-collector/client_file_collector.py b/client-file-collector/client_file_collector.py index 7c40f42..2a27d19 100644 --- a/client-file-collector/client_file_collector.py +++ b/client-file-collector/client_file_collector.py @@ -3,6 +3,7 @@ import json import os import queue +import shutil import sys import threading import time @@ -420,7 +421,10 @@ class CollectorApp: continue if self._upload_file(row, file_path): - self.upload_state[state_key] = signature + if self._backup_and_remove_file(row, file_path): + self.upload_state[state_key] = signature + else: + self.upload_state.pop(state_key, None) def _file_signature(self, file_path): try: @@ -430,6 +434,34 @@ class CollectorApp: self.log("读取文件状态失败: %s, 原因: %s" % (file_path, e)) return None + def _get_backup_dir(self, source_path): + source = Path(source_path) + return source.parent / (source.name + "_bak") + + def _backup_and_remove_file(self, row, file_path): + source_path = row["source_path"] + row_index = row["row_index"] + backup_dir = self._get_backup_dir(source_path) + + try: + backup_dir.mkdir(parents=True, exist_ok=True) + except Exception as e: + self.log("第%d行创建备份目录失败: %s, 原因: %s" % (row_index, backup_dir, e)) + return False + + target = backup_dir / file_path.name + if target.exists(): + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + target = backup_dir / ("%s_%s%s" % (file_path.stem, timestamp, file_path.suffix)) + + try: + shutil.move(str(file_path), str(target)) + self.log("第%d行源文件已转移至备份: %s -> %s" % (row_index, file_path.name, target)) + return True + except Exception as e: + self.log("第%d行源文件备份转移失败: %s, 原因: %s" % (row_index, file_path.name, e)) + return False + def _upload_file(self, row, file_path): url = self._base_url() + "/collector/client/upload" data = { diff --git a/client-file-collector/dist/QMSFileCollector.exe b/client-file-collector/dist/QMSFileCollector.exe index c6edf72..4862cab 100644 Binary files a/client-file-collector/dist/QMSFileCollector.exe and b/client-file-collector/dist/QMSFileCollector.exe differ diff --git a/client-file-collector/dist/collector_config.json b/client-file-collector/dist/collector_config.json index d21482a..bc9ce86 100644 --- a/client-file-collector/dist/collector_config.json +++ b/client-file-collector/dist/collector_config.json @@ -1,5 +1,5 @@ { - "base_url": "http://192.168.1.162:9000", + "base_url": "http://192.168.1.162:9097", "poll_seconds": "10", "rows": [ {