innoai commited on
Commit
9be3968
·
verified ·
1 Parent(s): 815bc09

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +57 -11
main.py CHANGED
@@ -32,6 +32,12 @@ import urllib.parse
32
  from fastapi import FastAPI, File, Form, HTTPException, UploadFile
33
  from fastapi.responses import FileResponse, JSONResponse
34
 
 
 
 
 
 
 
35
  ###############################################################################
36
  # 1. Inkscape 检测
37
  ###############################################################################
@@ -73,21 +79,61 @@ def ensure_inkscape_available() -> tuple[bool, str]:
73
  return False, "未找到 Inkscape,请安装或手动加入 PATH"
74
 
75
  ###############################################################################
76
- # 2. URL 规范化(解决中文 / 空格 / () 等字符)
77
  ###############################################################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  def normalize_url(url: str) -> str:
79
  """
80
- 将包含非 ASCII 字符的 URL 转为合法百分号编码格式
 
 
 
 
 
 
81
  """
82
- parsed = urllib.parse.urlparse(url)
83
- # 域名 IDNA(如有中文)
84
- netloc = parsed.netloc.encode("idna").decode("ascii")
85
- # 路径 / 查询 / 片段 百分号转义
86
- path = urllib.parse.quote(parsed.path, safe="/")
87
- params = urllib.parse.quote(parsed.params, safe=":&=")
88
- query = urllib.parse.quote_plus(parsed.query, safe="=&")
89
- fragment = urllib.parse.quote(parsed.fragment, safe="")
90
- return urllib.parse.urlunparse((parsed.scheme, netloc, path, params, query, fragment))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  ###############################################################################
93
  # 3. 下载并嵌入外链图片
 
32
  from fastapi import FastAPI, File, Form, HTTPException, UploadFile
33
  from fastapi.responses import FileResponse, JSONResponse
34
 
35
+ # 导入字体安装函数
36
+ from install_fonts import install_fonts_from_repository
37
+
38
+ # 安装字体
39
+ install_fonts_from_repository()
40
+
41
  ###############################################################################
42
  # 1. Inkscape 检测
43
  ###############################################################################
 
79
  return False, "未找到 Inkscape,请安装或手动加入 PATH"
80
 
81
  ###############################################################################
82
+ # 2. URL 规范化:解决中文 / 空格 / () 等字符
83
  ###############################################################################
84
+ #!/usr/bin/env python3
85
+ # -*- coding: utf-8 -*-
86
+ """
87
+ URL 规范化工具函数
88
+ -----------------
89
+ 功能特点
90
+ 1. 支持中文域名及路径:自动在域名处使用 IDNA(Punycode),在路径等位置做 % 编码
91
+ 2. **幂等**:已处理过的 URL 再次调用本函数不会出现二次编码
92
+ 3. 兼容常见 URL 组件:scheme、netloc、path、params、query、fragment
93
+ """
94
+
95
+ import urllib.parse as _url
96
+
97
+
98
  def normalize_url(url: str) -> str:
99
  """
100
+ 将包含中文或其它非 ASCII 字符的 URL 规范化为合法、安全的形式
101
+
102
+ 参数:
103
+ url (str): 原始 URL,可能含中文、空格、圆括号等字符
104
+
105
+ 返回:
106
+ str: 规范化后的 URL,可多次调用而结果保持一致
107
  """
108
+ # 解析 URL 各组件
109
+ parsed = _url.urlparse(url)
110
+
111
+ # -------------------------- 1) 处理域名 --------------------------
112
+ netloc = parsed.netloc
113
+ try:
114
+ # netloc 已是 ASCII(含 Punycode)则无需转换
115
+ netloc.encode("ascii")
116
+ except UnicodeEncodeError:
117
+ # 含非 ASCII 字符时才按 IDNA 转为 Punycode
118
+ netloc = netloc.encode("idna").decode("ascii")
119
+
120
+ # -------------------------- 2) 处理路径等 ------------------------
121
+ # 先 unquote 再 quote,可避免二次编码
122
+ path = _url.quote(_url.unquote(parsed.path), safe="/")
123
+ params = _url.quote(_url.unquote(parsed.params), safe=":&=")
124
+ # query 使用 quote_plus 处理空格(+),同时保留 & =
125
+ query = _url.quote_plus(_url.unquote_plus(parsed.query), safe="=&")
126
+ fragment = _url.quote(_url.unquote(parsed.fragment), safe="")
127
+
128
+ # -------------------------- 3) 重新组装 -------------------------
129
+ return _url.urlunparse((
130
+ parsed.scheme,
131
+ netloc,
132
+ path,
133
+ params,
134
+ query,
135
+ fragment,
136
+ ))
137
 
138
  ###############################################################################
139
  # 3. 下载并嵌入外链图片