delete image

This commit is contained in:
quantulr
2022-10-19 23:23:24 +08:00
parent 89ed38ff0f
commit c552efe9e2
2 changed files with 21 additions and 0 deletions

View File

@ -50,6 +50,19 @@ 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.delete("/image/delete/{pic_id}", tags=['图片'])
def delete_image(pic_id: int, db: Session = Depends(get_db)):
image_path = crud.get_pic(db, pic_id=pic_id).file_path
try:
db_pic = crud.delete_pic(db, pic_id=pic_id)
except:
raise HTTPException(status_code=500, detail="无法连接数据库")
store_path = Path(Path.home()).joinpath(
'Pictures').joinpath('upload_images').joinpath(image_path)
store_path.unlink()
return db_pic
@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(

View File

@ -21,3 +21,11 @@ def create_pic(db: Session, pic: schemas.image.ImageCreate):
db.add(db_pic)
db.commit()
return db_pic
def delete_pic(db: Session, pic_id: int):
db_pic = db.query(models.image.Image).filter(
models.image.Image.id == pic_id).first()
db.delete(db_pic)
db.commit()
return db_pic