use mimetypes.guess_type

This commit is contained in:
cxc
2022-09-27 13:29:43 +08:00
parent a5dffc2bf8
commit 3b68184dfc

View File

@ -1,5 +1,6 @@
from io import BytesIO
import mimetypes
import os
from pathlib import Path
from time import time
@ -36,18 +37,12 @@ def get_image(file_path: str, db: Session = Depends(get_db)):
image_path = Path(Path.home()).joinpath(
'Pictures').joinpath('upload_images')
try:
# img_file = open(image_path.joinpath(file_path), mode='rb')
img_pil = Image.open(image_path.joinpath(file_path))
img_file = open(image_path.joinpath(file_path), mode='rb')
except:
raise HTTPException(status_code=404, detail="没有找到图片")
img_buffer = BytesIO()
img_pil.save(img_buffer, format=img_pil.format)
media_type = f'image/{img_pil.format.lower()}'
img_resp = BytesIO(img_buffer.getvalue())
img_buffer.close()
img_pil.close()
media_type = mimetypes.guess_type(file_path)[0]
img_resp = BytesIO(img_file.read())
img_file.close()
try:
ori_file_name = crud.get_pic_by_path(db, file_path=file_path).file_name
except: