Explorar el Código

优化日志查询和ip查询

liuchengsen hace 4 semanas
padre
commit
2771a41565
Se han modificado 3 ficheros con 15 adiciones y 17 borrados
  1. 3 4
      data-pipeline/docx_ingest.py
  2. 11 12
      data-pipeline/extract_images.py
  3. 1 1
      static/index.html

+ 3 - 4
data-pipeline/docx_ingest.py

@@ -97,15 +97,14 @@ def _extract_images(doc, drug_name):
     """从 DOCX 提取所有图片,保存到 IMAGES_DIR,返回 {image_rId: url} 映射"""
     os.makedirs(IMAGES_DIR, exist_ok=True)
     img_map = {}
+    # 用药名 hash 前 8 位做前缀,避免中文 URL 编码后 nginx 找不到文件
+    name_hash = hashlib.md5(drug_name.encode()).hexdigest()[:8]
     for rId, rel in doc.part.rels.items():
         if "image" in rel.reltype:
             ext = rel.target_ref.split('.')[-1]
             if ext.lower() not in ('jpg', 'jpeg', 'png', 'gif', 'bmp'):
                 ext = 'png'
-            # 用 rId hash 防止重复
-            # 清理药名中的特殊字符,避免 URL 编码问题
-            safe_name = re.sub(r'[\s()() ]+', '_', drug_name).strip('_')
-            fname = f"{safe_name}_{rId}.{ext}"
+            fname = f"{name_hash}_{rId}.{ext}"
             fpath = os.path.join(IMAGES_DIR, fname)
             if not os.path.exists(fpath):
                 with open(fpath, 'wb') as f:

+ 11 - 12
data-pipeline/extract_images.py

@@ -23,6 +23,8 @@ Author: auto-generated via Claude Code (2026-07-28)
 """
 from __future__ import annotations
 
+import hashlib
+
 import json
 import os
 import re
@@ -81,13 +83,10 @@ find_docx_files = docx_ingest.find_docx_files
 # ---------------------------------------------------------------------------
 IMG_TAG_RE = re.compile(r'<img\s+src="([^"]+)"[^>]*/?>', re.IGNORECASE)
 
-# Characters that need sanitising for filesystem-safe drug names
-_RE_UNSAFE = re.compile(r'[\s()() /\\:?*"<>|]+')
-
 
-def _safe_drug_name(name: str) -> str:
-    """Return a filesystem-safe version of *name* (used as image filename prefix)."""
-    return _RE_UNSAFE.sub("_", name).strip("_")
+def _hash_prefix(name: str) -> str:
+    """Return 8-char MD5 hex for *name* (used as image filename prefix / fallback match)."""
+    return hashlib.md5(name.encode()).hexdigest()[:8]
 
 
 def _collect_images_from_sections(
@@ -107,20 +106,20 @@ def _collect_images_from_sections(
 
 
 def _find_orphan_images(
-    safe_name: str,
+    name_hash: str,
     images_dir: str,
     already_found: set[str],
 ) -> list[dict[str, str]]:
-    """Filesystem 兜底:找 &lt;img&gt; 标签中没有覆盖到的图片。
+    """Filesystem 兜底:找 <img> 标签中没有覆盖到的图片。
 
     Some images sit in the same paragraph as text (line 214 of docx_ingest.py
     has the ``if not p_text`` guard), so the inline parser does not emit an
-    ``&lt;img&gt;`` tag for them.  We catch those here by matching file names.
+    ``<img>`` tag for them.  We catch those here by matching file names.
     """
     if not os.path.isdir(images_dir):
         return []
 
-    prefix = safe_name + "_"
+    prefix = name_hash + "_"
     orphans: list[dict[str, str]] = []
     try:
         for fname in os.listdir(images_dir):
@@ -216,8 +215,8 @@ def main() -> None:
         found_filenames = {r["filename"] for r in section_images}
 
         # 4b – filesystem fallback for images that the inline parser missed
-        safe = _safe_drug_name(entry["name"])
-        orphans = _find_orphan_images(safe, IMAGES_DIR, found_filenames)
+        name_hash = _hash_prefix(entry["name"])
+        orphans = _find_orphan_images(name_hash, IMAGES_DIR, found_filenames)
         section_images.extend(orphans)
 
         if not section_images:

+ 1 - 1
static/index.html

@@ -228,7 +228,7 @@
   function md2html(h){
     if(!h)return'';h=esc(h);
   // 保留图片标签不转义
-  h=h.replace(/&lt;img\s+src="(.+?)"\s*\/?&gt;/g,'<img src="$1" style="max-width:100%">');
+  h=h.replace(/&lt;img\s+src="(.+?)"[^>]*\/?&gt;/g,'<img src="$1" style="max-width:100%">');
     h=h.replace(/\*\*(.+?)\*\*/g,'<strong>$1</strong>');
     h=h.replace(/⚠️?\s*([^\n<]+)/g,'<span class="warn">$1</span>');
     h=h.replace(/\[([^\]]+)\]\((https?:\/\/[^\s<>)]+)\)/g,'<a href="$2" target="_blank" rel="noopener" class="ext-link">$1</a>');