files.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import io
  2. import json
  3. import time
  4. from datetime import datetime, timedelta, timezone
  5. from app.common.security.auth import verify_users
  6. import app.crud as crud
  7. from fastapi import APIRouter, File, UploadFile, Form, Depends
  8. from sqlalchemy.orm import Session
  9. from fastapi.responses import StreamingResponse
  10. from utils.sx_time import sxtimeit
  11. from utils.sx_web import web_try
  12. from app.common.minio import minio_client
  13. from app import get_db, schemas
  14. router = APIRouter(
  15. prefix="/jpt/files",
  16. tags=["files-文件管理"],
  17. )
  18. @router.delete("/dag")
  19. @web_try()
  20. @sxtimeit
  21. def delete_dag_file(uri: str, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  22. res = crud.get_jm_homework_by_dag_url(db,uri)
  23. if len(res) == 0:
  24. minio_client.del_file(uri)
  25. else:
  26. raise Exception("该算子正在被作业使用,不可删除")
  27. @router.post("/upload_file")
  28. @web_try()
  29. @sxtimeit
  30. def upload_file(file: UploadFile = File(...), file_type: str=Form(...), token_data: schemas.TokenData = Depends(verify_users)):
  31. print("UploadFile-->",file.filename)
  32. file_name = str(int(time.time()))+'_'+file.filename
  33. url = minio_client.put_byte_file(f"{token_data.project_id}/{file_type}/"+file_name, file.file.read())
  34. return url
  35. @router.get("/directory")
  36. @web_try()
  37. @sxtimeit
  38. def get_directory(file_type: str, token_data: schemas.TokenData = Depends(verify_users)):
  39. files = minio_client.ls_file(f'{token_data.project_id}/{file_type}/')
  40. res = []
  41. td = timedelta(hours=8)
  42. tz = timezone(td)
  43. for file in files:
  44. timestamp = file.object_name.split('_',1)[0].split('/')[-1]
  45. dt = datetime.fromtimestamp(int(timestamp), tz)
  46. time_str = dt.strftime('%Y-%m-%d %H:%M:%S')
  47. dag_name = file.object_name.split('_',1)[1]
  48. res.append({'name':time_str+'-'+dag_name, 'uri':file.object_name,'timestamp':timestamp})
  49. res.sort(key=lambda x: x['timestamp'],reverse=True)
  50. return res
  51. @router.get("/dag_content")
  52. @sxtimeit
  53. @web_try()
  54. def get_dag_content(uri: str, token_data: schemas.TokenData = Depends(verify_users)):
  55. file = minio_client.get_file(uri)
  56. if len(file) == 0:
  57. raise Exception('No file found')
  58. res = json.loads(file)
  59. return res
  60. # 此接口置于末尾,勿乱动,乱动者砍死
  61. @router.get("/{uri:path}",name="path-convertor")
  62. @sxtimeit
  63. def get_file(uri: str):
  64. file = minio_client.get_file(uri)
  65. code = 200
  66. if len(file) == 0:
  67. code = 404
  68. response = StreamingResponse(io.BytesIO(file), status_code=code, media_type="application/octet-stream")
  69. # 在请求头进行配置
  70. response.headers["Content-Disposition"] = "attachment; filename="+uri
  71. return response