Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

817
Visualizações
Reuse function that validates file size [fastapi]

I'm very new to FastAPI. I want to validate the file type and the file size of an uploaded file and raise Exception if it's above the size and doesn't match the type. This file will be uploaded to S3 This is what my code looks like

@router.post("/upload/", status_code=200, description="***** Upload customer document asset to S3 *****")
async def upload(
        document_type: DocumentEnum,
        customer_id: UUID,
        current_user=Depends(get_current_user),
        fileobject: UploadFile = File(...)
):
    # delete the file from memory and rollover to disk to save unnecessary memory space
    fileobject.file.rollover()
    fileobject.file.flush()

    valid_types = [
        'image/png',
        'image/jpeg',
        'image/bmp',
        'application/pdf'
    ]
    await validate_file(fileobject, 5000000, valid_types)

    # .... Proceed to upload file 

My validate_file function looks like this

async def validate_file(file: UploadFile, max_size: int = None, mime_types: list = None):
    """
    Validate a file by checking the size and mime types a.k.a file types
    """
    if mime_types and file.content_type not in mime_types:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="You can only upload pdf and image for document"
        )
    if max_size:
        size = await file.read()
        if len(size) > max_size:
            raise HTTPException(
                status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
                detail="File size is too big. Limit is 5mb"
            )

    return file

Now when the file gets uploaded to S3, it's always 0 bytes in size. However, if I exclude the file size checking from validate_file function, then the original file gets uploaded and there's no problem. If the validate_file function is like this, then it gets uploaded fine

async def validate_file(file: UploadFile, max_size: int = None, mime_types: list = None):
    """
    Validate a file by checking the size and mime types a.k.a file types
    """
    if mime_types and file.content_type not in mime_types:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="You can only upload pdf and image for document"
        )
            )

    return file

I've no idea why this happens. Thank you in advance for your help.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

When you call read on a file, the current file pointer will be at the end of what you read. When you (or the library) call read the second time, the internal file pointer will already be at the end of the file.

You can use await file.seek(0) to put the file pointer at the start of the file, so that the next read will read the same content again:

if max_size:
    size = await file.read()

    if len(size) > max_size:
        raise HTTPException(
            status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
            detail="File size is too big. Limit is 5mb"
        )

    await file.seek(0)

return file

You might also want to explicitly resolve the mime type of the file instead of trusting what the user says the file is - you can use mimetypes.guess_type or something similar to do that.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda