ForcePilot/backend/package/yuxi/utils/__init__.py
Wenjie Zhang 410dd47c14 refactor: 将后端代码迁移至 backend 目录
- 将 server/, src/, scripts/, test/ 等目录移动到 backend/ 目录下
- 使用 git rename 保留文件历史记录
- 更新 docker-compose.yml 和 api.Dockerfile 配置

WIP: 项目结构重构进行中
2026-03-24 11:08:12 +08:00

66 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import hashlib
import os
import time
import uuid
from yuxi.utils.logging_config import logger
def is_text_pdf(pdf_path):
import fitz
doc = fitz.open(pdf_path)
total_pages = len(doc)
if total_pages == 0:
return False
text_pages = 0
for page_num in range(total_pages):
page = doc.load_page(page_num)
text = page.get_text()
if text.strip(): # 检查是否有文本内容
text_pages += 1
# 计算有文本内容的页面比例
text_ratio = text_pages / total_pages
# 如果超过50%的页面有文本内容则认为是文本PDF
return text_ratio > 0.5
def hashstr(input_string, length=None, with_salt=False, salt=None):
"""生成字符串的哈希值
Args:
input_string: 输入字符串
length: 截取长度默认为None表示不截取
with_salt: 是否加盐默认为False
"""
try:
# 尝试直接编码
encoded_string = str(input_string).encode("utf-8")
except UnicodeEncodeError:
# 如果编码失败,替换无效字符
encoded_string = str(input_string).encode("utf-8", errors="replace")
if with_salt:
if not salt:
# 使用时间戳+随机数的组合作为salt确保唯一性
salt = f"{time.time()}_{uuid.uuid4().hex[:8]}"
encoded_string = (encoded_string.decode("utf-8") + salt).encode("utf-8")
hash = hashlib.sha256(encoded_string).hexdigest()
if length:
return hash[:length]
return hash
def get_docker_safe_url(base_url):
if not base_url:
return base_url
if os.getenv("RUNNING_IN_DOCKER") == "true":
# 替换所有可能的本地地址形式
base_url = base_url.replace("http://localhost", "http://host.docker.internal")
base_url = base_url.replace("http://127.0.0.1", "http://host.docker.internal")
logger.info(f"Running in docker, using {base_url} as base url")
return base_url