AbstractMultipartParser class abstract

Abstract base class for parsing multipart HTTP requests with support for disk-backed file storage.

AbstractMultipartParser extends MultipartParser and provides an enhanced framework for handling multipart form-data requests, particularly large file uploads that may exceed in-memory limits. This class defines the strategy for:

  1. Determining memory thresholds for parts and switching to disk-based storage when necessary.
  2. Preserving or discarding the original client-submitted filename for temporary files.
  3. Configuring the temporary directory where large uploads are persisted.
  4. Creating FilePart instances for parts that exceed memory thresholds.

This class is intended for framework-level implementations that need fine-grained control over multipart processing. It is not meant for direct use by application code, but rather serves as a base for concrete parser implementations.

Core Responsibilities:

  • Parse multipart streams and create Part instances for each section.
  • Identify which parts are files and which are simple form fields.
  • Manage temporary storage efficiently for large uploads.
  • Provide hooks for configuring memory thresholds, file preservation, and storage paths.

Key Features:

  • Memory Thresholds: The method getFileSizeThreshold determines the maximum size a part can occupy in memory before it is written to disk.
  • Preserve Original Filename: getPreserveFileName allows developers to preserve the original client filename when creating temporary files.
  • Temporary Directory Configuration: getTemporaryDirectory defines where disk-based parts are stored.
  • Automatic Disk Conversion: Parts exceeding the memory threshold are automatically converted to FilePart objects and stored in the temporary directory.
  • File Detection: The method isFilePart identifies parts that represent uploaded files based on the presence of a submitted filename.

Usage Example:

final parser = MyDiskMultipartParser();
final boundary = parser.extractBoundary(request.getContentType())!;
final parts = await parser.parse(request.getBody(), boundary, 'utf-8');
for (final part in parts) {
  if (parser.isFilePart(part)) {
    print('File uploaded: ${part.getSubmittedFileName()}, size: ${part.getSize()}');
  } else {
    print('Form field: ${part.getName()}, value: ${await part.getString()}');
  }
}

Design Considerations:

  • Disk-backed storage prevents large file uploads from exhausting server memory.
  • Temporary file creation respects user-configurable settings for file naming and storage location.
  • Implementations may override parse to customize how disk-backed parts are generated or how memory thresholds are applied.
  • Ensures a unified interface for multipart parsing regardless of whether parts are in memory or persisted on disk.

Important Notes:

  • Parts without a submitted filename are treated as simple form fields and remain in memory.
  • File parts exceeding the memory threshold are automatically persisted to disk.
  • Temporary files are created under a configurable directory and cleaned up by application-specific logic or server shutdown hooks.
  • Encoding of text-based fields is handled via the inherited EncodingDecoder.

This class provides a robust foundation for multipart parsing in web frameworks, balancing memory efficiency, disk storage, and convenience for accessing uploaded files and form fields.

Inheritance
Implementers

Constructors

AbstractMultipartParser()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

extractBoundary(String contentType) String?
Extracts the multipart boundary string from a Content-Type header.
inherited
getBufferSize() int
The buffer size to be used in streaming the request.
inherited
getEncodingDecoder() EncodingDecoder
Returns the EncodingDecoder used to decode byte content into strings.
inherited
getFileSizeThreshold() int
Returns the maximum in-memory size (in bytes) a file can occupy before being written to disk.
getLog() → Log
The logger to use for this parser.
inherited
getPreserveFileName() bool
Indicates whether the original filename of uploaded files should be preserved when creating temporary files.
getTemporaryDirectory() String
Returns the directory path where temporary files for multipart uploads should be stored.
isFilePart(Part part) bool
Determines whether the given part represents a file upload.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
parse(InputStream inputStream, String boundary, String charset, [int? contentLength]) Future<List<Part>>
Parses a multipart HTTP request body into individual Part instances.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited