diff --git a/alist-sync-web.py b/alist-sync-web.py index 3396bf1..7304b5d 100644 --- a/alist-sync-web.py +++ b/alist-sync-web.py @@ -504,6 +504,9 @@ def _execute_single_task(self, task: Dict): if task.get('regexPatterns'): os.environ['REGEX_PATTERNS'] = task.get('regexPatterns') + # 添加强制覆盖环境变量 + os.environ['FORCE_OVERWRITE'] = str(task.get('forceOverwrite')) + if task['syncMode'] == 'data': self._handle_data_sync(task) elif task['syncMode'] == 'file': diff --git a/alist_sync.py b/alist_sync.py index 1407bee..7d97803 100644 --- a/alist_sync.py +++ b/alist_sync.py @@ -497,6 +497,17 @@ def _copy_item_with_check(self, src_dir: str, dst_dir: str, item: Dict) -> bool: logger.info(f"复制文件: {item_name}") return self._copy_item(src_dir, dst_dir, item_name) else: + # 设置了强制覆盖后执行覆盖操作 + force_overwrite = os.environ.get("FORCE_OVERWRITE", "false").lower() == "true" + if force_overwrite: + logger.info(f"强制覆盖文件: {item_name}") + # 删除旧文件 + if not self._directory_operation("remove", dir=dst_dir, names=[item_name]): + logger.error(f"删除目标文件失败: {dst_path}") + return False + # 复制新文件 + return self._copy_item(src_dir, dst_dir, item_name) + # 获取源文件和目标文件信息 src_size = item.get("size") dst_info = self.get_file_info(dst_path) diff --git a/templates/index.html b/templates/index.html index 5c3e3a5..8a43bb4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -333,6 +333,12 @@

Alist-Sync 配置中心

+
+ +
+ +
+
@@ -1194,7 +1200,9 @@

Alist-Sync 配置中心

syncDelAction: $task.find(`input[name="syncDelAction_${taskId}"]:checked`).val(), syncMode: syncMode, excludeDirs: $task.find('input[name="excludeDirs[]"]').val(), - cron: $task.find('input[name="cron[]"]').val() + cron: $task.find('input[name="cron[]"]').val(), + // 收集强制覆盖状态 + forceOverwrite: $task.find(`input[name="forceOverwrite[]"]`).is(':checked') }; if(syncMode === 'data') { @@ -1504,6 +1512,9 @@

Alist-Sync 配置中心

$task.find('input[name="regexPatterns[]"]').val(task.regexPatterns); } + // 回显强制覆盖状态 + $task.find(`input[name="forceOverwrite[]"]`).prop('checked', task.forceOverwrite); + // 等待一小段时间确保DOM更新完成 await new Promise(resolve => setTimeout(resolve, 50)); }