from datetime import datetime import os import threading from configs.basic_config import * from configs.kb_config import KB_CHAT_TEMP_DIR def get_storage_abspath(path: str) -> str: """获取文件的存储绝对路径 Args: path: 文件路径 Returns: str: 规范化后的完整存储路径 """ try: # 规范化路径 specified_dir = KB_CHAT_TEMP_DIR normalized_path = os.path.normpath(os.path.join(specified_dir, path)) # 确保存储目录存在 if not os.path.exists(normalized_path): os.makedirs(normalized_path, exist_ok=True) return normalized_path except Exception as e: logger.error(f"获取存储路径失败: {str(e)}") # 发生异常时返回当前工作目录下的路径 data_dir = os.path.join(os.getcwd(), "data") if not os.path.exists(data_dir): os.makedirs(data_dir, exist_ok=True) return os.path.join(data_dir, path) def task_to_dict(task): """ 将 TranslationTask ORM 对象转换为字典,包含所有字段并将 datetime 转为 ISO 格式字符串。 """ return { "id": task.id, "filename": task.filename, "src_lang": task.src_lang, "dst_lang": task.dst_lang, "is_dual": task.is_dual, "file_path": task.file_path, "output_path": task.output_path, "status": task.status, "progress": task.progress, "retry_count": task.retry_count, "error_msg": task.error_msg, "created_at": task.created_at.isoformat() if isinstance(task.created_at, datetime) else task.created_at, "updated_at": task.updated_at.isoformat() if isinstance(task.updated_at, datetime) else task.updated_at, } class AtomicInteger: def __init__(self, value=0): self._value = int(value) self._lock = threading.Lock() def inc(self, d=1): with self._lock: self._value += int(d) return self._value def dec(self, d=1): return self.inc(-d) @property def value(self): with self._lock: return self._value @value.setter def value(self, v): with self._lock: self._value = int(v) return self._value