"""文件 IO 与截图镜像辅助函数。 从 douyin/core/underlying_operations.py 顶部抽离的模块级函数, 为多个模块提供共用的安全文件替换和截图镜像能力。 外部 import 路径已通过 underlying_operations 重新导出,保持向后兼容。 """ from __future__ import annotations import logging import os import time as time_module from typing import Optional # 外部完整版 fudai 逻辑所在的工作树路径,用于镜像 screenshot 给那边使用。 # 仅在显式设置 EXTERNAL_DOUYIN_FUDAI_DIR 时启用。 _EXTERNAL_DOUYIN_FUDAI_DIR = os.environ.get("EXTERNAL_DOUYIN_FUDAI_DIR", "").strip() _EXTERNAL_AXF_SCREENSHOT = ( os.path.join(_EXTERNAL_DOUYIN_FUDAI_DIR, "pic", "screenshot.png") if _EXTERNAL_DOUYIN_FUDAI_DIR else "" ) def _verify_png_integrity(path: str) -> bool: """验证 PNG 文件头尾完整性,防止读取半写文件""" try: with open(path, "rb") as f: header = f.read(8) if len(header) < 8 or header[:4] != b"\x89PNG": return False f.seek(-12, 2) # IEND chunk 至少 12 字节 tail = f.read(12) return b"IEND" in tail except Exception: return False def _safe_replace_file(tmp_file: str, out_file: str, max_retries: int = 2) -> bool: """安全地替换文件,处理 Windows 文件锁定问题。 优先 os.replace 原子替换;失败时回退到 shutil.copy2,避免长时间等待。 对 PNG 文件会先校验完整性,避免发布半写文件。 Args: tmp_file: 临时文件路径 out_file: 目标文件路径 max_retries: 最大重试次数(已优化为 2 次) Returns: True if successful, False otherwise """ import shutil if not os.path.exists(tmp_file): return False # PNG 完整性校验 if tmp_file.lower().endswith(".png"): if not _verify_png_integrity(tmp_file): logging.debug("PNG 文件不完整,跳过替换: %s", tmp_file) return False # 方法 1:原子替换 try: os.replace(tmp_file, out_file) return True except (OSError, PermissionError): pass # 方法 2:shutil.copy2(保留元数据) try: shutil.copy2(tmp_file, out_file) try: os.remove(tmp_file) except Exception: pass return True except Exception: pass # 方法 3:手动复制(最后手段) try: with open(tmp_file, "rb") as src, open(out_file, "wb") as dst: while True: chunk = src.read(1048576) # 1MB chunks if not chunk: break dst.write(chunk) try: os.remove(tmp_file) except Exception: pass return True except Exception: return False def _ensure_parent_dir(path: str) -> None: """确保 path 的父目录存在。""" try: parent = os.path.dirname(path) if parent: os.makedirs(parent, exist_ok=True) except Exception: logging.exception("无法创建父目录: %s", path) def _mirror_screenshot_to_external(src_file: str) -> None: """将当前项目下的 screenshot.png 同步一份到外部 axf 目录,便于完整版代码复用。 若外部目录/路径不存在或复制失败,会静默忽略,不影响主流程。 可通过环境变量 DISABLE_SCREENSHOT_MIRROR=1 禁用此功能以提升性能。 """ if os.environ.get("DISABLE_SCREENSHOT_MIRROR", "0") == "1": return mirror_start = time_module.time() try: external_path = _EXTERNAL_AXF_SCREENSHOT if not external_path: return external_dir = os.path.dirname(external_path) if not external_dir: return external_base = os.environ.get("EXTERNAL_DOUYIN_FUDAI_DIR", "") is_network_path = False if external_base: if external_base.startswith("\\\\") or external_base.startswith("//"): is_network_path = True logging.warning( "[截图] 检测到网络路径,镜像可能较慢: %s", external_base ) elif ":" in external_base and len(external_base) > 3: logging.debug("[截图] 镜像目标路径: %s", external_path) os.makedirs(external_dir, exist_ok=True) if not os.path.exists(src_file): return tmp = external_path + ".tmp" import shutil copy_start = time_module.time() shutil.copyfile(src_file, tmp) copy_dur = (time_module.time() - copy_start) * 1000.0 if copy_dur > 50.0 or is_network_path: logging.info( "[截图] 镜像复制耗时(ms)=%.1f src=%s dst=%s (网络路径=%s)", copy_dur, src_file, tmp, is_network_path, ) replace_start = time_module.time() _safe_replace_file(tmp, external_path) replace_dur = (time_module.time() - replace_start) * 1000.0 if replace_dur > 50.0 or is_network_path: logging.info( "[截图] 镜像文件替换耗时(ms)=%.1f (网络路径=%s)", replace_dur, is_network_path, ) mirror_total_dur = (time_module.time() - mirror_start) * 1000.0 if mirror_total_dur > 100.0 or is_network_path: logging.info( "[截图] 镜像总耗时(ms)=%.1f path=%s (网络路径=%s)", mirror_total_dur, external_path, is_network_path, ) except Exception: # 任何错误都不应该影响截图主流程 pass __all__ = [ "_verify_png_integrity", "_safe_replace_file", "_ensure_parent_dir", "_mirror_screenshot_to_external", "_EXTERNAL_DOUYIN_FUDAI_DIR", "_EXTERNAL_AXF_SCREENSHOT", ]