innoai commited on
Commit
cb90605
·
verified ·
1 Parent(s): 16ce5e7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +38 -36
main.py CHANGED
@@ -136,56 +136,58 @@ def normalize_url(url: str) -> str:
136
  ))
137
 
138
  ###############################################################################
139
- # 3. 下载并嵌入外链图片
140
  ###############################################################################
141
  IMG_TAG_PATTERN = re.compile(
142
  r'<image[^>]+(?:href|xlink:href)\s*=\s*["\']([^"\']+)["\']',
143
- re.I,
144
  )
145
 
146
- def download_and_encode(url: str) -> str:
147
- """下载远程图片并转为 data URI(Base64)"""
148
- try:
149
- safe_url = normalize_url(url) # ⭐ 关键改动
150
- req = urllib.request.Request(safe_url, headers={"User-Agent": "Mozilla/5.0"})
151
- with urllib.request.urlopen(req, timeout=20) as resp:
152
- data = resp.read()
153
- mime, _ = mimetypes.guess_type(safe_url)
154
- mime = mime or "image/png"
155
- # b64 = base64.b64encode(data).decode("ascii")
156
- b64 = base64.b64encode(data).decode() # 新的,与 Gradio 版本一致
157
- return f"data:{mime};base64,{b64}"
158
- except Exception as e:
159
- print(f"[WARN] 下载外链图片失败 {url}: {e}")
160
- return url # 失败则返回原链接
161
-
162
  def embed_external_images(svg_path: str) -> str:
163
- """把 SVG 中的远程图片下载并嵌入(Base64),返回新的临时 SVG 路径"""
164
  with open(svg_path, "r", encoding="utf-8") as f:
165
  svg_text = f.read()
166
- if "http://" not in svg_text and "https://" not in svg_text:
167
- return svg_path # 无外链直接返回
168
 
169
- def replacer(match: re.Match) -> str:
170
- url = match.group(1)
171
- if url.startswith(("http://", "https://")):
172
- data_uri = download_and_encode(url)
173
- return match.group(0).replace(url, data_uri)
174
- return match.group(0)
175
-
176
- new_svg = IMG_TAG_PATTERN.sub(replacer, svg_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
- # 若缺失 xlink 命名空间,则补上
179
- if "xmlns:xlink" not in new_svg:
180
- new_svg = new_svg.replace(
181
  "<svg", '<svg xmlns:xlink="http://www.w3.org/1999/xlink"', 1
182
  )
183
 
 
184
  tmp_dir = tempfile.mkdtemp()
185
- new_path = os.path.join(tmp_dir, Path(svg_path).name)
186
- with open(new_path, "w", encoding="utf-8") as f:
187
- f.write(new_svg)
188
- return new_path
189
 
190
  ###############################################################################
191
  # 4. 核心转换函数
 
136
  ))
137
 
138
  ###############################################################################
139
+ # 3. 预处理:下载并嵌入远程图片
140
  ###############################################################################
141
  IMG_TAG_PATTERN = re.compile(
142
  r'<image[^>]+(?:href|xlink:href)\s*=\s*["\']([^"\']+)["\']',
143
+ re.I
144
  )
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  def embed_external_images(svg_path: str) -> str:
147
+ """把 svg 中的外链图片转为 data URI,返回新的 svg 路径"""
148
  with open(svg_path, "r", encoding="utf-8") as f:
149
  svg_text = f.read()
 
 
150
 
151
+ # 若无 http/https 外链直接返回
152
+ if "http://" not in svg_text and "https://" not in svg_text:
153
+ return svg_path
154
+
155
+ def download_and_encode(url: str) -> str:
156
+ """下载图片并转 data URI;失败则返回原 URL"""
157
+ try:
158
+ safe_url = normalize_url(url) # ⭐ 关键改动
159
+ req = urllib.request.Request(
160
+ safe_url,
161
+ headers={"User-Agent": "Mozilla/5.0"}
162
+ )
163
+ with urllib.request.urlopen(req, timeout=20) as resp:
164
+ data = resp.read()
165
+ mime, _ = mimetypes.guess_type(safe_url)
166
+ mime = mime or "image/png"
167
+ b64 = base64.b64encode(data).decode()
168
+ return f"data:{mime};base64,{b64}"
169
+ except Exception as e:
170
+ print(f"[WARN] 下载 {url} 失败:{e}")
171
+ return url # 失败则保留原链接
172
+
173
+ # 批量替换
174
+ new_svg_text = IMG_TAG_PATTERN.sub(
175
+ lambda m: m.group(0).replace(m.group(1), download_and_encode(m.group(1))),
176
+ svg_text
177
+ )
178
 
179
+ # 若缺少 xlink 命名空间则补上
180
+ if "xmlns:xlink" not in new_svg_text:
181
+ new_svg_text = new_svg_text.replace(
182
  "<svg", '<svg xmlns:xlink="http://www.w3.org/1999/xlink"', 1
183
  )
184
 
185
+ # 写入临时文件
186
  tmp_dir = tempfile.mkdtemp()
187
+ new_svg_path = os.path.join(tmp_dir, Path(svg_path).name)
188
+ with open(new_svg_path, "w", encoding="utf-8") as f:
189
+ f.write(new_svg_text)
190
+ return new_svg_path
191
 
192
  ###############################################################################
193
  # 4. 核心转换函数