Skip to content

File Processor

processor

File processing and storage utilities.

This module handles saving resume files to both the database and S3-compatible storage (Garage).

Functions

save_file async
save_file(user_id, file, file_name, db)

Save a resume file to database and S3 storage.

Creates a database record for the resume and uploads the file bytes to S3-compatible storage (Garage). The file path follows the pattern: resumes/{user_id}/{file_name}.

Parameters:

Name Type Description Default
user_id str

ID of the user uploading the file.

required
file bytes

File contents as bytes.

required
file_name str

Original filename.

required
db AsyncSession

Database session (will be closed after use).

required

Returns:

Type Description
bool

True if successful.

Raises:

Type Description
ProcessingException

If database save fails (integrity error or other).

Source code in bitonicai/internal/processors/processor.py
async def save_file(user_id: str, file: bytes, file_name: str, db: AsyncSession) -> bool:
    """Save a resume file to database and S3 storage.

    Creates a database record for the resume and uploads the file bytes
    to S3-compatible storage (Garage). The file path follows the pattern:
    `resumes/{user_id}/{file_name}`.

    Args:
        user_id: ID of the user uploading the file.
        file: File contents as bytes.
        file_name: Original filename.
        db: Database session (will be closed after use).

    Returns:
        True if successful.

    Raises:
        ProcessingException: If database save fails (integrity error or other).
    """
    try:
        db.add(
            Resume(
                user_id=user_id,
                file_name=file_name,
                title=file_name,
                file_path=f"resumes/{user_id}/{file_name}",
            )
        )
        await db.commit()
    except IntegrityError as e:
        await db.rollback()
        raise ProcessingException(f"File already exists: {str(e)}", 501)
    except Exception as e:
        await db.rollback()
        raise ProcessingException(f"Failed to save file: {str(e)}", 502)
    finally:
        await db.close()
    s3_tool = S3Tool()
    s3_tool.upload_file(file, garage_settings.bucket_name, f"resumes/{user_id}/{file_name}")
    s3_tool.close()
    return True