12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import os
- import time
- from typing import Union
- from fastapi import APIRouter, UploadFile, File, Depends
- from common.common import upload_oss_file, delete_oss_file
- from core.config import settings
- from models.user import Student, Teacher, SysUser
- from schemas.base import DetailMixin
- from schemas.common import DeleteFile, UploadBase64File
- from utils.depends import get_current_user
- router = APIRouter()
- @router.post("/upfile",
- response_model=DetailMixin,
- response_model_exclude_none=True,
- summary="通用文件上传")
- async def upload_file(datafile: UploadFile = File(..., description="文件"),
- current_user: Union[SysUser, Teacher,
- Student] = Depends(get_current_user)):
- ok, url = await upload_oss_file(
- datafile, content_type={"Content-Type": datafile.content_type})
- if ok:
- return {"data": url}
- else:
- return {"errcode": 400, "mess": url}
- @router.post("/upfile/base64",
- response_model=DetailMixin,
- response_model_exclude_none=True,
- summary="通用文件上传(base64)")
- async def upload_base64_file(
- datafile: UploadBase64File,
- current_user: Union[SysUser, Teacher, Student] = Depends(get_current_user)):
- content_type = datafile.content[:22].split(";")[0].split(":")[-1]
- ok, url = await upload_oss_file(datafile,
- content_type={"Content-Type": content_type})
- if ok:
- return {"data": url}
- else:
- return {"errcode": 400, "mess": url}
- @router.post("/delfile",
- response_model=DetailMixin,
- response_model_exclude_none=True,
- tags=["通用接口"],
- summary="通用文件删除")
- async def delete_file(info: DeleteFile,
- current_user: Union[SysUser, Teacher,
- Student] = Depends(get_current_user)):
- ok, url = await delete_oss_file(info.path)
- if ok:
- return {"data": None}
- else:
- return {"errcode": 400, "mess": url}
|