Skip to content

Optimize FileUploader to avoid loading full stream into memory during upload #3692

Closed

Description

@coderabbitai

Issue Description

The current FileUploader implementation in packages/serverpod_client/lib/src/file_uploader.dart uses http.ByteStream(stream).toBytes() to convert the entire stream to bytes before uploading. While this fixes the immediate issue with stream reading by using an Uint8List for buffer, it still loads the complete file into memory before starting the upload.

Problem

For large file uploads, this approach can cause:

  • High memory usage
  • Potential out-of-memory errors on devices with limited RAM
  • Poor performance due to buffering overhead

Proposed Solution

Implement a streaming upload mechanism that can send data directly from the stream to the HTTP request without buffering the entire content in memory first. This would involve:

  • Using the stream directly in the HTTP request body
  • Ensuring proper chunked transfer encoding
  • Maintaining compatibility with the existing upload flow

Context

This issue was identified during the review of PR #3691 which fixed the immediate stream reading issue but introduced the memory buffering concern.

Related:

Reporter: @nielsenko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions