This commit is contained in:
quantulr
2022-10-16 16:49:59 +08:00
parent e35d4cb292
commit 8bd782ad48
2 changed files with 35 additions and 0 deletions

View File

@ -4,6 +4,8 @@ from fastapi import Depends, FastAPI
from .routers import image
# from . import models
from .sql.database import engine, Base
from fastapi.middleware.cors import CORSMiddleware
Base.metadata.create_all(bind=engine)
@ -13,6 +15,16 @@ Base.metadata.create_all(bind=engine)
# app = FastAPI(dependencies=[Depends(get_query_token)])
app = FastAPI()
origins = [
"*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(image.router)

View File

@ -50,6 +50,29 @@ def get_image(store_name: str, y: str, m: str, d: str, db: Session = Depends(get
return StreamingResponse(content=img_resp, media_type=media_type, headers={'Content-Disposition': f'inline; filename={ori_file_name}'})
@router.get("/image/thumbnail/{y}/{m}/{d}/{store_name}", tags=['图片'])
def get_image_thumb(store_name: str, y: str, m: str, d: str, db: Session = Depends(get_db)):
image_path = Path(Path.home()).joinpath(
'Pictures').joinpath('upload_images').joinpath(f'{y}/{m}/{d}').joinpath(store_name)
try:
img_pil = Image.open(image_path)
except:
raise HTTPException(status_code=404, detail="没有找到图片")
media_type = mimetypes.guess_type(store_name)[0]
img_pil_buffer = BytesIO()
print(img_pil.size[1])
img_pil.thumbnail((512, img_pil.size[1]))
img_pil.save(img_pil_buffer, format=img_pil.format)
img_pil.close()
img_resp = BytesIO(img_pil_buffer.getvalue())
try:
ori_file_name = crud.get_pic_by_path(
db, file_path=f'{y}/{m}/{d}/{store_name}').file_name
except:
raise HTTPException(status_code=500, detail="无法连接数据库")
return StreamingResponse(content=img_resp, media_type=media_type, headers={'Content-Disposition': f'inline; filename={ori_file_name}'})
@router.post("/image/upload", response_model=schemas.image.Image, tags=['图片'])
async def create_pic(pic: UploadFile, db: Session = Depends(get_db)):
allowFileType = ["image/jpeg", "image/gif", "image/png"]