Coverage for mpcforces_extractor\api\routes\file_upload.py: 47%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-06 21:34 +0100

1import os 

2from fastapi import APIRouter, UploadFile, Form 

3from mpcforces_extractor.api.config import UPLOAD_FOLDER, OUTPUT_FOLDER 

4 

5router = APIRouter() 

6 

7 

8@router.post("/upload-chunk") 

9async def upload_chunk( 

10 file: UploadFile, filename: str = Form(...), offset: int = Form(...) 

11): 

12 """ 

13 Upload a chunk of a file 

14 """ 

15 file_path = os.path.join(UPLOAD_FOLDER, filename) 

16 

17 # check if the file exists, if so, delete it 

18 if offset == 0: 

19 if os.path.exists(file_path): 

20 os.remove(file_path) 

21 

22 # Create the upload directory if it doesn't exist 

23 os.makedirs(UPLOAD_FOLDER, exist_ok=True) 

24 

25 # Open the file in append mode to write the chunk at the correct offset 

26 with open(file_path, "ab") as f: 

27 f.seek(offset) 

28 content = await file.read() 

29 f.write(content) 

30 

31 return {"message": "Chunk uploaded successfully!"} 

32 

33 

34@router.get("/get-output-folder") 

35async def get_output_folder(): 

36 """ 

37 Get the output folder path 

38 """ 

39 return {"output_folder": OUTPUT_FOLDER}