-
Notifications
You must be signed in to change notification settings - Fork 9
Add file size validation to document upload endpoint #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ankit-mehta07
wants to merge
5
commits into
ProjectTech4DevAI:main
Choose a base branch
from
ankit-mehta07:enhancement/add-file-size-validation-document-upload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
828fa51
Add file size validation to document upload endpoint
ankit-mehta07 652adb9
Remove trailing slashes from API endpoints
ankit-mehta07 d2bfc46
Add project and organization foreign keys to async Job model
ankit-mehta07 1adc93c
Address review comments and update file size validation
ankit-mehta07 ab8c2e9
Update file size validation as per review
ankit-mehta07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
backend/app/alembic/versions/043_add_project_org_to_job_table.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Add project_id and organization_id to job table | ||
|
|
||
| Revision ID: 043 | ||
| Revises: 042 | ||
| Create Date: 2026-02-04 14:39:00.000000 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "043" | ||
| down_revision = "042" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| # Add organization_id column | ||
| op.add_column( | ||
| "job", | ||
| sa.Column( | ||
| "organization_id", | ||
| sa.Integer(), | ||
| nullable=True, | ||
| comment="Reference to the organization", | ||
| ), | ||
| ) | ||
|
|
||
| # Add project_id column | ||
| op.add_column( | ||
| "job", | ||
| sa.Column( | ||
| "project_id", | ||
| sa.Integer(), | ||
| nullable=True, | ||
| comment="Reference to the project", | ||
| ), | ||
| ) | ||
|
|
||
| # Create foreign key constraints | ||
| op.create_foreign_key( | ||
| "fk_job_organization_id", | ||
| "job", | ||
| "organization", | ||
| ["organization_id"], | ||
| ["id"], | ||
| ondelete="CASCADE", | ||
| ) | ||
|
|
||
| op.create_foreign_key( | ||
| "fk_job_project_id", | ||
| "job", | ||
| "project", | ||
| ["project_id"], | ||
| ["id"], | ||
| ondelete="CASCADE", | ||
| ) | ||
|
|
||
| # Create indexes | ||
| op.create_index( | ||
| "ix_job_organization_id", | ||
| "job", | ||
| ["organization_id"], | ||
| unique=False, | ||
| ) | ||
|
|
||
| op.create_index( | ||
| "ix_job_project_id", | ||
| "job", | ||
| ["project_id"], | ||
| unique=False, | ||
| ) | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| # Drop indexes | ||
| op.drop_index("ix_job_project_id", table_name="job") | ||
| op.drop_index("ix_job_organization_id", table_name="job") | ||
|
|
||
| # Drop foreign key constraints | ||
| op.drop_constraint("fk_job_project_id", "job", type_="foreignkey") | ||
| op.drop_constraint("fk_job_organization_id", "job", type_="foreignkey") | ||
|
|
||
| # Drop columns | ||
| op.drop_column("job", "project_id") | ||
| op.drop_column("job", "organization_id") | ||
| # ### end Alembic commands ### | ||
34 changes: 34 additions & 0 deletions
34
backend/app/alembic/versions/044_optimize_conversation_query.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| """add composite index for conversation query optimization | ||
|
|
||
| Revision ID: 044 | ||
| Revises: 043 | ||
| Create Date: 2026-02-04 15:24:00.000000 | ||
|
|
||
| """ | ||
| from alembic import op | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "044" | ||
| down_revision = "043" | ||
| branch_labels = None | ||
| depends_on = None | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| # Create composite index to optimize the get_conversation_by_ancestor_id query | ||
| # This query filters by: ancestor_response_id, project_id, is_deleted | ||
| # and orders by: inserted_at DESC | ||
| op.create_index( | ||
| "ix_openai_conversation_ancestor_project_active_time", | ||
| "openai_conversation", | ||
| ["ancestor_response_id", "project_id", "is_deleted", "inserted_at"], | ||
| unique=False, | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_index( | ||
| "ix_openai_conversation_ancestor_project_active_time", | ||
| table_name="openai_conversation", | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """Validation utilities for document uploads.""" | ||
|
|
||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| from fastapi import HTTPException, UploadFile | ||
|
|
||
| from app.core.config import settings | ||
| from app.utils import mask_string | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Maximum file size for document uploads (in bytes) | ||
| # Default: 512 MB, configurable via settings | ||
| MAX_DOCUMENT_SIZE = settings.MAX_DOCUMENT_UPLOAD_SIZE_MB * 1024 * 1024 | ||
|
|
||
|
|
||
| async def validate_document_file(file: UploadFile) -> int: | ||
| """ | ||
| Validate document file size. | ||
|
|
||
| Args: | ||
| file: The uploaded file | ||
|
|
||
| Returns: | ||
| File size in bytes if valid | ||
| """ | ||
|
|
||
| # Get file size by seeking to end | ||
| file.file.seek(0, 2) | ||
| file_size = file.file.tell() | ||
| file.file.seek(0) | ||
|
|
||
| if file_size > MAX_DOCUMENT_SIZE: | ||
| raise HTTPException( | ||
| status_code=413, | ||
| detail=f"File too large. Maximum size: {MAX_DOCUMENT_SIZE / (1024 * 1024):.0f}MB", | ||
| ) | ||
|
|
||
| if file_size == 0: | ||
| raise HTTPException( | ||
| status_code=422, | ||
| detail="Empty file uploaded" | ||
| ) | ||
|
|
||
| logger.info( | ||
| f"[validate_document_file] Document file validated: {mask_string(file.filename)} ({file_size} bytes)" | ||
| ) | ||
| return file_size |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.