filehandler.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. from typing import Union
  5. from fastapi import APIRouter, UploadFile, File, Depends
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from common.common import upload_oss_file, delete_oss_file
  8. from core.config import settings
  9. from crud.marktask import crud_task
  10. from crud.paper import crud_paper
  11. from models.user import Student, Teacher, Admin
  12. from schemas.base import DetailMixin
  13. from schemas.common import DeleteFile, UploadBase64File
  14. from utils.depends import get_current_user, get_async_db
  15. router = APIRouter()
  16. @router.post("/upfile",
  17. response_model=DetailMixin,
  18. response_model_exclude_none=True,
  19. summary="通用文件上传")
  20. async def upload_file(datafile: UploadFile = File(..., description="文件"),
  21. current_user: Union[Admin, Teacher, Student] = Depends(get_current_user)):
  22. ok, url = await upload_oss_file(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(datafile: UploadBase64File,
  32. current_user: Union[Admin, Teacher,
  33. Student] = Depends(get_current_user)):
  34. content_type = datafile.content[:22].split(";")[0].split(":")[-1]
  35. ok, url = await upload_oss_file(datafile, content_type={"Content-Type": content_type})
  36. if ok:
  37. return {"data": url}
  38. else:
  39. return {"errcode": 400, "mess": url}
  40. @router.post("/delfile",
  41. response_model=DetailMixin,
  42. response_model_exclude_none=True,
  43. tags=["通用接口"],
  44. summary="通用文件删除")
  45. async def delete_file(info: DeleteFile,
  46. current_user: Union[Admin, Teacher, Student] = Depends(get_current_user)):
  47. ok, url = await delete_oss_file(info.path)
  48. if ok:
  49. return {"data": None}
  50. else:
  51. return {"errcode": 400, "mess": url}
  52. @router.post("/uppapers", summary="试卷上传")
  53. async def upload_file(datafile: UploadFile = File(..., description="文件"),
  54. db: AsyncSession = Depends(get_async_db)):
  55. content = await datafile.read()
  56. filename = datafile.filename
  57. tid = filename.split("_")[0]
  58. task = await crud_task.find_one(db, filters={"id": tid})
  59. if task:
  60. pid = task.pid
  61. paper = await crud_paper.find_one(db, filters={"id": pid})
  62. if paper:
  63. pno = paper.pno
  64. root = os.path.join(settings.PAPERS_PATH, f"T{tid}-{pno}")
  65. if not os.path.exists(root):
  66. os.makedirs(root)
  67. filepath = os.path.join(root, filename)
  68. with open(filepath, "wb") as f:
  69. f.write(content)
  70. return {"data": filepath}
  71. return {"errcode": 400, "mess": "上传失败!"}