Fix image upload bottleneck - #1039
Open
K-Zawis wants to merge 4 commits into
Open
Conversation
Initialize `MimeTypeResolver` statically once instead of re-instantiating it on every `lookupType` call. Simplify `lookupType` to directly pass `headerBytes` to `_resolver.lookup`, ensuring magic numbers are evaluated first regardless of whether the file path has an extension. Expose `magicNumbersMaxLength` getter to allow memory-safe, partial file stream reads when determining MIME types.
… and memory-safe fallbacks Integrate flutter_image_compress for hardware-accelerated image processing on Android, iOS, and macOS, drastically reducing execution time. Refactor image processing and MIME resolution pipeline: - Add `_resolveMimeType` to read header magic numbers via partial file streams without allocating full payloads in RAM. - Use `img.decodeImageFile` in pure-Dart `_fallbackProcessImage` isolate to stream directly from disk for Windows/Linux. - Guard `processFile()` to resolve missing MIME types upfront and prevent `LateInitializationError` on unsupported attachments. - Update Android `ndkVersion` in build config to resolve native C++ compilation warnings and plugin toolchain mismatches.
Guard against uninitialized video controllers, add try/catch for platform exceptions on corrupt files, and provide a fallback MIME type for video thumbnails.
K-Zawis
marked this pull request as draft
August 31, 2026 17:48
…mes on linux as well
K-Zawis
marked this pull request as ready for review
August 31, 2026 18:26
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR addresses a major performance bottleneck during file uploads where pure-Dart decoding caused multi-second UI freezes and high memory allocation that has been plaguing me during my use of Commet (as I mainly use it on mobile). It also patches several fatal crash vectors (
NullCheckError,LateInitializationError, and platform exceptions) in both the image and video processing pipelines.Key Changes
flutter_image_compressto utilize hardware-accelerated media pipelines on Android, iOS, and macOS._fallbackProcessImage) forLinux andWindows that streams image bytes directly from disk (img.decodeImageFile) instead of allocating full payloads in RAM, avoiding OOM spikes.processVideo()against uninitialized controllers/corrupted media, and replaced force-unwrap operators (!) in the image pipeline with safe null checks to prevent unhandledNullCheckErrors on unsupported file types._resolveMimeTypeto read header magic numbers via partial file streams, safely resolving missing MIME types upfront and preventingLateInitializationErrors.ndkVersionto28.2.13676358to resolve native C++ plugin toolchain mismatches.Performance Benchmarks
Tested on a physical Samsung Galaxy S22 Ultra (SM-S908B) processing a standard 12 MP photo (5.41 MB, 3000×4000 resolution):
package:image):4,754 msflutter_image_compress):289 ms(Note: Native C-codec execution via
flutter_image_compress_linuxwas also verified on Linux x86_64, providing a noticeably snappier frame response compared to pure-Dart isolate decoding.)