(background_tasks: BackgroundTasks, db: Session = Depends(get_db), file: UploadFile = File(...))
| 59 | |
| 60 | @application.post("/") |
| 61 | async def upload_files(background_tasks: BackgroundTasks, db: Session = Depends(get_db), file: UploadFile = File(...)): |
| 62 | filename = file.filename |
| 63 | title = filename.split('.')[0] |
| 64 | ext = filename.split('.')[1] |
| 65 | |
| 66 | # check if uploading file existed already |
| 67 | db_title = crud.get_audio_by_title(db, title=title) |
| 68 | if db_title: |
| 69 | raise HTTPException(status_code=400, detail="Audio already uploaded") # 将传过来的文件保存 |
| 70 | |
| 71 | # save file |
| 72 | out_file_path = audio_raw_path / file.filename |
| 73 | async with aiofiles.open(out_file_path, 'wb') as out_file: |
| 74 | content = await file.read() # async read |
| 75 | size = "{}MB".format(round(len(content)/(1024*1024),3)) |
| 76 | await out_file.write(content) # async write |
| 77 | data = {"title": title, |
| 78 | "size": size, |
| 79 | "extension": ext |
| 80 | } |
| 81 | #create audio info |
| 82 | result = crud.create_audio_info(db = db, data = data) |
| 83 | # background tasks for ffmpeg file |
| 84 | background_tasks.add_task(bg_task, out_file_path, title, db) |
| 85 | |
| 86 | return result |
| 87 | |
| 88 | |
| 89 | @application.get("/", response_model=schemas.ReadAudioData) |
nothing calls this directly
no outgoing calls
no test coverage detected