2025-05-26 13:12:17 +08:00
|
|
|
|
import hashlib
|
2025-03-04 13:49:00 +08:00
|
|
|
|
import os
|
2025-09-01 22:37:03 +08:00
|
|
|
|
import time
|
|
|
|
|
|
|
2025-02-27 19:35:25 +08:00
|
|
|
|
from src.utils.logging_config import logger
|
2024-07-14 18:31:23 +08:00
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2024-07-14 18:31:23 +08:00
|
|
|
|
def is_text_pdf(pdf_path):
|
2024-07-16 18:14:27 +08:00
|
|
|
|
import fitz
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2024-07-14 18:31:23 +08:00
|
|
|
|
doc = fitz.open(pdf_path)
|
2025-03-11 18:10:31 +08:00
|
|
|
|
total_pages = len(doc)
|
|
|
|
|
|
if total_pages == 0:
|
|
|
|
|
|
return False
|
2025-05-23 15:30:14 +08:00
|
|
|
|
|
2025-03-11 18:10:31 +08:00
|
|
|
|
text_pages = 0
|
|
|
|
|
|
for page_num in range(total_pages):
|
2024-07-14 18:31:23 +08:00
|
|
|
|
page = doc.load_page(page_num)
|
|
|
|
|
|
text = page.get_text()
|
|
|
|
|
|
if text.strip(): # 检查是否有文本内容
|
2025-03-11 18:10:31 +08:00
|
|
|
|
text_pages += 1
|
2025-05-23 15:30:14 +08:00
|
|
|
|
|
2025-03-11 18:10:31 +08:00
|
|
|
|
# 计算有文本内容的页面比例
|
|
|
|
|
|
text_ratio = text_pages / total_pages
|
|
|
|
|
|
# 如果超过50%的页面有文本内容,则认为是文本PDF
|
|
|
|
|
|
return text_ratio > 0.5
|
2024-07-14 23:59:52 +08:00
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-11-29 17:49:42 +08:00
|
|
|
|
def hashstr(input_string, length=None, with_salt=False, salt=None):
|
2025-05-26 13:12:17 +08:00
|
|
|
|
"""生成字符串的哈希值
|
|
|
|
|
|
Args:
|
|
|
|
|
|
input_string: 输入字符串
|
|
|
|
|
|
length: 截取长度,默认为None,表示不截取
|
|
|
|
|
|
with_salt: 是否加盐,默认为False
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 尝试直接编码
|
2025-09-01 22:37:03 +08:00
|
|
|
|
encoded_string = str(input_string).encode("utf-8")
|
2025-05-26 13:12:17 +08:00
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
|
|
# 如果编码失败,替换无效字符
|
2025-09-01 22:37:03 +08:00
|
|
|
|
encoded_string = str(input_string).encode("utf-8", errors="replace")
|
2025-05-26 13:12:17 +08:00
|
|
|
|
|
2024-07-28 16:16:52 +08:00
|
|
|
|
if with_salt:
|
2025-11-29 17:49:42 +08:00
|
|
|
|
if not salt:
|
|
|
|
|
|
salt = str(time.time())
|
2025-09-01 22:37:03 +08:00
|
|
|
|
encoded_string = (encoded_string.decode("utf-8") + salt).encode("utf-8")
|
2024-07-28 16:16:52 +08:00
|
|
|
|
|
2025-05-26 13:12:17 +08:00
|
|
|
|
hash = hashlib.md5(encoded_string).hexdigest()
|
|
|
|
|
|
if length:
|
|
|
|
|
|
return hash[:length]
|
|
|
|
|
|
return hash
|
2025-03-04 13:49:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_docker_safe_url(base_url):
|
2025-07-26 03:36:54 +08:00
|
|
|
|
if not base_url:
|
|
|
|
|
|
return base_url
|
|
|
|
|
|
|
2025-03-04 13:49:00 +08:00
|
|
|
|
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")
|
2025-05-24 11:29:45 +08:00
|
|
|
|
return base_url
|