feat: 修改 400问题
This commit is contained in:
+21
-2
@@ -82,6 +82,23 @@ def _is_image(url: str, ctype: str) -> bool:
|
||||
return bool(re.search(r"\.(jpe?g|png|webp|gif|bmp)(\?|$)", url, re.IGNORECASE))
|
||||
|
||||
|
||||
def _image_content_type(data: bytes, url: str, ctype: str) -> str | None:
|
||||
"""存储对象无扩展名且 MIME 为 octet-stream 时,按图片文件头识别格式。"""
|
||||
if ctype.split(";")[0].strip().lower().startswith("image/"):
|
||||
return ctype
|
||||
if data.startswith(b"\xff\xd8\xff"):
|
||||
return "image/jpeg"
|
||||
if data.startswith(b"\x89PNG\r\n\x1a\n"):
|
||||
return "image/png"
|
||||
if data.startswith((b"GIF87a", b"GIF89a")):
|
||||
return "image/gif"
|
||||
if data.startswith(b"RIFF") and data[8:12] == b"WEBP":
|
||||
return "image/webp"
|
||||
if data.startswith(b"BM"):
|
||||
return "image/bmp"
|
||||
return ctype if _is_image(url, ctype) else None
|
||||
|
||||
|
||||
def _download_disposition(filename: str) -> str:
|
||||
"""生成兼容中文文件名的下载响应头。
|
||||
|
||||
@@ -138,7 +155,8 @@ async def export_images(req: ExportImagesRequest):
|
||||
raise HTTPException(status_code=504, detail=str(exc)) from exc
|
||||
except Exception: # noqa: BLE001
|
||||
continue # 单张下载失败不中断整包
|
||||
if not _is_image(img.url, ctype):
|
||||
ctype = _image_content_type(data, img.url, ctype)
|
||||
if ctype is None:
|
||||
continue
|
||||
total_source_bytes += len(data)
|
||||
if total_source_bytes > settings.export_zip_max_bytes:
|
||||
@@ -192,7 +210,8 @@ async def export_image(img: ExportImageItem):
|
||||
raise HTTPException(status_code=504, detail=str(exc)) from exc
|
||||
except Exception as exc: # noqa: BLE001
|
||||
raise HTTPException(status_code=422, detail="图片下载失败") from exc
|
||||
if not _is_image(img.url, ctype):
|
||||
ctype = _image_content_type(data, img.url, ctype)
|
||||
if ctype is None:
|
||||
raise HTTPException(status_code=400, detail="该地址不是图片")
|
||||
|
||||
ext = _ext(img.url, ctype)
|
||||
|
||||
Reference in New Issue
Block a user