use mimetypes.guess_type
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from time import time
|
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(
|
image_path = Path(Path.home()).joinpath(
|
||||||
'Pictures').joinpath('upload_images')
|
'Pictures').joinpath('upload_images')
|
||||||
try:
|
try:
|
||||||
# img_file = open(image_path.joinpath(file_path), mode='rb')
|
img_file = open(image_path.joinpath(file_path), mode='rb')
|
||||||
img_pil = Image.open(image_path.joinpath(file_path))
|
|
||||||
except:
|
except:
|
||||||
raise HTTPException(status_code=404, detail="没有找到图片")
|
raise HTTPException(status_code=404, detail="没有找到图片")
|
||||||
|
media_type = mimetypes.guess_type(file_path)[0]
|
||||||
img_buffer = BytesIO()
|
img_resp = BytesIO(img_file.read())
|
||||||
img_pil.save(img_buffer, format=img_pil.format)
|
img_file.close()
|
||||||
media_type = f'image/{img_pil.format.lower()}'
|
|
||||||
img_resp = BytesIO(img_buffer.getvalue())
|
|
||||||
img_buffer.close()
|
|
||||||
img_pil.close()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ori_file_name = crud.get_pic_by_path(db, file_path=file_path).file_name
|
ori_file_name = crud.get_pic_by_path(db, file_path=file_path).file_name
|
||||||
except:
|
except:
|
||||||
|
Reference in New Issue
Block a user