@cliven/mddocx 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
Binary file
|
|
@@ -566,8 +566,40 @@ def add_empty_para(doc):
|
|
|
566
566
|
# ============================================================
|
|
567
567
|
|
|
568
568
|
def download_image(url):
|
|
569
|
-
"""
|
|
569
|
+
"""下载或复制图片到临时文件,返回路径。失败返回 None。
|
|
570
|
+
|
|
571
|
+
支持:
|
|
572
|
+
- HTTP/HTTPS URL:requests 下载
|
|
573
|
+
- 本地文件路径:直接复制到临时文件
|
|
574
|
+
- base64 data URI:解码保存
|
|
575
|
+
"""
|
|
570
576
|
import requests
|
|
577
|
+
import shutil
|
|
578
|
+
|
|
579
|
+
# 1. 本地文件路径(相对于工作目录的路径)
|
|
580
|
+
if os.path.exists(url):
|
|
581
|
+
suffix = os.path.splitext(url)[1] or '.png'
|
|
582
|
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
|
583
|
+
shutil.copy2(url, tmp.name)
|
|
584
|
+
tmp.close()
|
|
585
|
+
return tmp.name
|
|
586
|
+
|
|
587
|
+
# 2. base64 data URI
|
|
588
|
+
if url.startswith('data:'):
|
|
589
|
+
import base64
|
|
590
|
+
# 格式: data:image/png;base64,xxxx 或 data:image/png,xxxx
|
|
591
|
+
header, data = url.split(',', 1)
|
|
592
|
+
is_base64 = ';base64' in header
|
|
593
|
+
suffix = '.png'
|
|
594
|
+
mime_match = re.match(r'data:image/(\w+)', header)
|
|
595
|
+
if mime_match:
|
|
596
|
+
suffix = '.' + mime_match.group(1)
|
|
597
|
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
|
598
|
+
tmp.write(base64.b64decode(data) if is_base64 else data.encode())
|
|
599
|
+
tmp.close()
|
|
600
|
+
return tmp.name
|
|
601
|
+
|
|
602
|
+
# 3. HTTP/HTTPS URL
|
|
571
603
|
headers = {'User-Agent': 'Mozilla/5.0 (compatible; mddoc-converter/1.0)'}
|
|
572
604
|
try:
|
|
573
605
|
resp = requests.get(url, headers=headers, timeout=30)
|