filehandler.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import time
  5. from typing import Union
  6. from fastapi import APIRouter, UploadFile, File, Depends
  7. from common.common import upload_oss_file, delete_oss_file
  8. from core.config import settings
  9. from models.user import Student, Teacher, SysUser
  10. from schemas.base import DetailMixin
  11. from schemas.common import DeleteFile, UploadBase64File
  12. from utils.depends import get_current_user
  13. router = APIRouter()
  14. @router.post("/upfile",
  15. response_model=DetailMixin,
  16. response_model_exclude_none=True,
  17. summary="通用文件上传")
  18. async def upload_file(datafile: UploadFile = File(..., description="文件"),
  19. current_user: Union[SysUser, Teacher,
  20. Student] = Depends(get_current_user)):
  21. ok, url = await upload_oss_file(
  22. datafile, content_type={"Content-Type": datafile.content_type})
  23. if ok:
  24. return {"data": url}
  25. else:
  26. return {"errcode": 400, "mess": url}
  27. @router.post("/upfile/base64",
  28. response_model=DetailMixin,
  29. response_model_exclude_none=True,
  30. summary="通用文件上传(base64)")
  31. async def upload_base64_file(
  32. datafile: UploadBase64File,
  33. current_user: Union[SysUser, Teacher, Student] = Depends(get_current_user)):
  34. content_type = datafile.content[:22].split(";")[0].split(":")[-1]
  35. ok, url = await upload_oss_file(datafile,
  36. content_type={"Content-Type": content_type})
  37. if ok:
  38. return {"data": url}
  39. else:
  40. return {"errcode": 400, "mess": url}
  41. @router.post("/delfile",
  42. response_model=DetailMixin,
  43. response_model_exclude_none=True,
  44. tags=["通用接口"],
  45. summary="通用文件删除")
  46. async def delete_file(info: DeleteFile,
  47. current_user: Union[SysUser, Teacher,
  48. Student] = Depends(get_current_user)):
  49. ok, url = await delete_oss_file(info.path)
  50. if ok:
  51. return {"data": None}
  52. else:
  53. return {"errcode": 400, "mess": url}