Skip to content

fix: Compress and retry image upload on 413 error#7435

Open
LakshuCodes wants to merge 3 commits intoopenfoodfacts:developfrom
LakshuCodes:fix/image-upload-413-compress-retry
Open

fix: Compress and retry image upload on 413 error#7435
LakshuCodes wants to merge 3 commits intoopenfoodfacts:developfrom
LakshuCodes:fix/image-upload-413-compress-retry

Conversation

@LakshuCodes
Copy link
Contributor

Fixes #6874

Problem

When uploading a large image (>20MB), the server (nginx) returns a
413 Request Entity Too Large error, causing the upload to fail with
an exception.

Solution

On receiving a 413 error, the app now automatically retries the upload
with a compressed version of the image (80% JPEG quality, forceCompression: true)
using the existing cropIfNeeded() method.

Flow

  • Try normal upload
  • If 413 → compress image → retry upload
  • If success → done

Changes

  • packages/smooth_app/lib/background/background_task_image.dart
    • Changed final Status to Status (mutable)
    • Added 413 detection and compress-retry logic in upload()

@codecov-commenter
Copy link

codecov-commenter commented Mar 1, 2026

Codecov Report

❌ Patch coverage is 0% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 9.10%. Comparing base (4d9c7fc) to head (74f2c24).
⚠️ Report is 1353 commits behind head on develop.

Files with missing lines Patch % Lines
...ooth_app/lib/background/background_task_image.dart 0.00% 25 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           develop   #7435      +/-   ##
==========================================
- Coverage     9.54%   9.10%   -0.45%     
==========================================
  Files          325     625     +300     
  Lines        16411   36648   +20237     
==========================================
+ Hits          1567    3337    +1770     
- Misses       14844   33311   +18467     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@monsieurtanuki monsieurtanuki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @LakshuCodes!
Please have a look at my comments.
Besides, you're copy/pasting code, and it's not appropriate.
A (local) method called twice would be cleaner.
Something like Future<Status> addImage(int compressionPct, bool force).

Comment on lines -419 to -420
// The very same image was already uploaded and therefore was rejected.
// We just have to select this image, with no angle.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was that so obvious that you felt the urge to remove those comments?

image,
uriHelper: uriProductHelper,
);
if (status.status == 'status ok') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reuse a const.

Comment on lines +417 to +418
if (status.error?.contains('413') == true ||
status.error?.contains('Request Entity Too Large') == true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you managed to actually get a 413 error?
I assume the server would have directly thrown an exception, with a 413 status code, not a clean "I tried but that's not entirely correct" Status.
To be double-checked.

cropY1: cropY1,
cropX2: cropX2,
cropY2: cropY2,
compressQuality: 80, // was 100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that's the only change you make, right? From 100 to 80, forcing the compression.
Could work.
I imagined changing the image dimensions too, but in a first approach you're right.

uriHelper: uriProductHelper,
);
if (status.status == 'status ok') {
// successfully uploaded a new picture and set it as field+language
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove only obvious comments.

@LakshuCodes
Copy link
Contributor Author

Hii @monsieurtanuki , the CI checks are failing due to a 401 Unauthorized error at the Set up job step when downloading GitHub Actions (subosito/flutter-action and actions/labeler)
this happens before my code is reached. Could you please re-run the failed checks?
Also, I am sorry for removing the meaningful comments and adding unnecessary ones
I will be more careful about this from next time.

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

Projects

Status: 💬 To discuss and validate

Development

Successfully merging this pull request may close these issues.

_Exception: Exception: Could not upload picture: 413 / Request Entity Too Large

3 participants