Skip to content

Add Server-Side File Size Validation for Image and Video Uploads #1

@YAXH64

Description

@YAXH64

📌 Overview

Currently, DeepSentry accepts uploaded image and video files without any file size restrictions. All uploaded files are fully loaded into memory (RAM) before processing and inference.

While this works well for small and medium-sized files during local testing, extremely large uploads may lead to:

  • High RAM consumption
  • Slow processing times
  • Server instability
  • Increased API response latency
  • Possible crashes on low-resource systems

This becomes especially important when deploying the application publicly or handling multiple concurrent users.


⚠️ Current Behavior

At the moment:

  • Any supported file size can be uploaded
  • Videos are read entirely into memory
  • No validation occurs before processing
  • Users do not receive a clear error for oversized uploads

Example scenario:

  • A user uploads a 2 GB ".mp4" file
  • The backend attempts to load/process it
  • RAM usage spikes dramatically
  • Inference becomes unstable or crashes

✅ Proposed Solution

Implement server-side upload size validation before inference begins.

Suggested Limits

Media Type| Recommended Limit
Images| 10 MB
Videos| 100 MB

These limits should remain configurable through environment variables or constants for easier deployment customization.


🔧 Suggested Implementation

  1. Validate File Size Before Processing

Check upload size immediately after receiving the file.

Example logic:

  • Read file size
  • Compare against allowed maximum
  • Reject oversized files before OpenCV/PIL processing

  1. Return Proper HTTP Error Responses

If file size exceeds the allowed limit:

  • Return HTTP "413 Payload Too Large"
  • Include a user-friendly JSON response

Example:

{
"detail": "Uploaded file exceeds maximum allowed size."
}


  1. Add Configurable Limits

Move hardcoded values into constants or environment variables.

Example:

MAX_IMAGE_SIZE_MB = 10
MAX_VIDEO_SIZE_MB = 100

or:

MAX_IMAGE_SIZE_MB=10
MAX_VIDEO_SIZE_MB=100

This improves flexibility across:

  • local development
  • cloud deployment
  • low-memory VPS servers

📂 Files Likely Affected

  • "main.py"
  • "processor.py"

Potentially:

  • frontend validation in "index.html"

💡 Optional Enhancements

Future improvements could include:

Frontend Validation

Display file size errors before upload begins.

Streaming Uploads

Avoid loading entire videos into memory.

Progress Indicators

Show upload and processing progress for large files.

Dynamic Limits

Different size limits based on deployment environment.


🎯 Benefits

Implementing upload size validation would improve:

  • Stability
  • Security
  • Scalability
  • Resource management
  • Production readiness
  • User experience

It would also prevent accidental server overload from extremely large uploads.


🏷️ Suggested Labels

  • enhancement
  • security
  • backend
  • performance
  • good first issue

🚀 Expected Outcome

After implementation:

  • Oversized files are rejected safely
  • RAM usage remains controlled
  • API becomes more production-ready
  • Users receive clear validation feedback
  • Deployment reliability improves significantly

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions