Skip to content

file

This module defines a router for the api that is dedicated to file related responsibilities.

upload_file async

upload_file(file: UploadFile, current_user: Annotated[models.User, Depends(oauth2.get_current_user)], minio_client: Annotated[files.Minio, Depends(files.get_minio_client)], celery_client: Annotated[celery.Celery, Depends(celery.get_celery_client)])

Endpoint for uploading files.

Parameters:

Name Type Description Default
file UploadFile

The file to be uploaded.

required
current_user User

The current authenticated user.

required
minio_client Minio

The MinIO client for interacting with the object storage.

required
celery_client Celery

The Celery client for queuing tasks.

required

Returns:

Type Description
dict

A dictionary containing file metadata and status.

Raises:

Type Description
HTTPException

If the file type is not supported or if there are errors during file upload or queuing.

Source code in api/routers/file.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@router.post("/", response_model=models.UploadedFile, status_code=status.HTTP_201_CREATED)
async def upload_file(
    file: UploadFile,
    current_user: Annotated[models.User, Depends(oauth2.get_current_user)],
    minio_client: Annotated[files.Minio, Depends(files.get_minio_client)],
    celery_client: Annotated[celery.Celery, Depends(celery.get_celery_client)],
):
    """
    Endpoint for uploading files.

    Parameters
    ----------
    file : UploadFile
        The file to be uploaded.
    current_user : models.User
        The current authenticated user.
    minio_client : files.Minio
        The MinIO client for interacting with the object storage.
    celery_client : celery.Celery
        The Celery client for queuing tasks.

    Returns
    -------
    dict
        A dictionary containing file metadata and status.

    Raises
    ------
    HTTPException
        If the file type is not supported or if there are errors during file upload or queuing.
    """
    if file.content_type != "text/plain":
        raise HTTPException(
            status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
            detail=f"File type of {file.content_type} is not a supported media type of text/plain",
        )

    file_size = await files.get_file_size(file)

    try:
        minio_client.put_object(files.BUCKET, f"{current_user.id}/{file.filename}", file.file, file_size)
    except S3Error as e:
        logging.exception()
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Error uploading file.",
        ) from e

    try:
        # send file meta-data and user id to task queue
        task = celery_client.send_task(
            name="process_file",
            args=[current_user.id, file.filename, file.content_type],
        )
    except TaskError as e:
        logging.exception()
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Error queueing file process",
        ) from e

    return {
        "filename": file.filename,
        "content_type": file.content_type,
        "size": file_size,
        "status": task.status,
    }