Upload compression fixes: animated WebP/AVIF, SVG, size, unreadable files #3611

Open
lambadalambda wants to merge 6 commits from fix/animated-webp-upload into develop

Fixes in resizeImage (image compression before upload):

  • Animated WebP and AVIF kept their first frame only. Animated pictures are now found from the bytes (WebP VP8X flag, AVIF avis brand, PNG acTL) and uploaded as they are. The type is not enough: the browser takes it from the file name.
  • SVG was rasterized. It is now uploaded as it is.
  • The re-encoded file was used even when bigger. Now only when smaller. Note: the original then keeps its EXIF, which the canvas used to remove.
  • A picture the browser cannot decode (HEIC in Chrome) made the upload hang. It now goes up as it is.

Testing

  • New animated_image.spec.js and media_upload.spec.js
  • unit-ci, ci-biome, ci-stylelint, build
Fixes in `resizeImage` (image compression before upload): - Animated WebP and AVIF kept their first frame only. Animated pictures are now found from the bytes (WebP VP8X flag, AVIF `avis` brand, PNG `acTL`) and uploaded as they are. The type is not enough: the browser takes it from the file name. - SVG was rasterized. It is now uploaded as it is. - The re-encoded file was used even when bigger. Now only when smaller. Note: the original then keeps its EXIF, which the canvas used to remove. - A picture the browser cannot decode (HEIC in Chrome) made the upload hang. It now goes up as it is. ## Testing - New `animated_image.spec.js` and `media_upload.spec.js` - unit-ci, ci-biome, ci-stylelint, build
fix(upload): keep animated WebP and AVIF animated when compressing
All checks were successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/test-e2e Pipeline was successful
8a3f702ff0
Author
Owner

/oc review

/oc review
opencode-fix-bot left a comment

Summary

This PR stops the upload compressor from flattening animated WebP and AVIF files by sniffing animation markers from file bytes instead of trusting the MIME type. The WebP VP8X animation-flag check and the AVIF avis-brand scan both match their container specs, the old PNG acTL scan is preserved, and the new test/unit/specs/services/animated_image.spec.js covers animated/still/truncated cases for all three formats. Full unit suite passes here (51 files, 527 passed, 2 skipped) and biome check is clean.

Findings

  • P2: PNG detection is still MIME-gated, so a misnamed animated PNG is still flattened. isAnimated in src/services/animated_image/animated_image.js:46 only runs the acTL byte scan when file.type === 'image/png', while WebP and AVIF are detected purely from bytes. The PR description itself notes the browser takes the type from the file name, so an animated PNG uploaded with a wrong extension (e.g. .jpg) skips every check, hits the canvas path in resizeImage, and is uploaded as a still — the exact data loss this PR eliminates for the other two formats. Smallest fix: drop the MIME gate and always run the byte scan, e.g. (await isAnimatedPng(file)), since isAnimatedPng already reads the content itself.

Merge readiness

No P0/P1 issues found; the WebP and AVIF paths are correct and well tested. Rating: 4/5 (capped by the P2 above).

opencode-agent:job=180:generation=1

### Summary This PR stops the upload compressor from flattening animated WebP and AVIF files by sniffing animation markers from file bytes instead of trusting the MIME type. The WebP VP8X animation-flag check and the AVIF `avis`-brand scan both match their container specs, the old PNG `acTL` scan is preserved, and the new `test/unit/specs/services/animated_image.spec.js` covers animated/still/truncated cases for all three formats. Full unit suite passes here (51 files, 527 passed, 2 skipped) and `biome check` is clean. ### Findings - **P2: PNG detection is still MIME-gated, so a misnamed animated PNG is still flattened.** `isAnimated` in `src/services/animated_image/animated_image.js:46` only runs the `acTL` byte scan when `file.type === 'image/png'`, while WebP and AVIF are detected purely from bytes. The PR description itself notes the browser takes the type from the file name, so an animated PNG uploaded with a wrong extension (e.g. `.jpg`) skips every check, hits the canvas path in `resizeImage`, and is uploaded as a still — the exact data loss this PR eliminates for the other two formats. Smallest fix: drop the MIME gate and always run the byte scan, e.g. `(await isAnimatedPng(file))`, since `isAnimatedPng` already reads the content itself. ### Merge readiness No P0/P1 issues found; the WebP and AVIF paths are correct and well tested. Rating: **4/5** (capped by the P2 above). opencode-agent:job=180:generation=1
fix(upload): do not hang on pictures the browser cannot read
All checks were successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/test-e2e Pipeline was successful
04d72573a5
lambadalambda changed title from Keep animated WebP and AVIF animated when compressing uploads to Upload compression fixes: animated WebP/AVIF, SVG, size, unreadable files 2026-11-10 12:45:18 +00:00
Author
Owner

/oc review

/oc review
opencode-fix-bot left a comment

Summary

This PR stops the upload compressor from flattening animated WebP and AVIF files by sniffing animation markers from file bytes instead of trusting the MIME type, and additionally fixes SVG rasterization, keeps the original when compression is not smaller, and resolves the hang on undecodable files. I verified the WebP VP8X flag check and the AVIF avis-brand scan against their container layouts, including boundary cases (brand outside the ftyp box ignored, truncated files safely return false), and re-checked the surrounding resizeImage paths. biome check is clean on all touched files. The browser unit suite could not run here (vitest --run --browser.headless fails: Playwright chromium is missing system shared libraries), so I re-verified the detection logic with a direct Node harness covering the spec cases instead.

Findings

  • P2: Animated PNG with a wrong file type is still flattened. isAnimated in src/services/animated_image/animated_image.js:46 only runs the acTL byte scan when file.type === 'image/png', while WebP and AVIF are detected purely from bytes. The PR description itself notes the browser takes the type from the file name, so an animated PNG uploaded with a wrong extension (e.g. photo.jpg, typed image/jpeg) skips every animation check, hits the canvas path in resizeImage, and is uploaded as a still frame. That is the exact animation-loss data loss this PR eliminates for the other two formats, and the existing reads the bytes whatever the type says test covers misnamed WebP but not misnamed PNG. Smallest fix: drop the MIME gate and always run the byte scan, since isAnimatedPng already reads the content itself:
export const isAnimated = async (file) =>
  (await isAnimatedWebP(file)) ||
  (await isAnimatedAvif(file)) ||
  (await isAnimatedPng(file))

Merge readiness

No P0/P1 issues found; the WebP/AVIF paths, the SVG bypass, the onerror fallback, and the keep-original-if-larger logic are all correct. Rating: 4/5 (capped by the P2 above).

opencode-agent:job=181:generation=1

### Summary This PR stops the upload compressor from flattening animated WebP and AVIF files by sniffing animation markers from file bytes instead of trusting the MIME type, and additionally fixes SVG rasterization, keeps the original when compression is not smaller, and resolves the hang on undecodable files. I verified the WebP VP8X flag check and the AVIF `avis`-brand scan against their container layouts, including boundary cases (brand outside the `ftyp` box ignored, truncated files safely return false), and re-checked the surrounding `resizeImage` paths. `biome check` is clean on all touched files. The browser unit suite could not run here (`vitest --run --browser.headless` fails: Playwright chromium is missing system shared libraries), so I re-verified the detection logic with a direct Node harness covering the spec cases instead. ### Findings - **P2: Animated PNG with a wrong file type is still flattened.** `isAnimated` in `src/services/animated_image/animated_image.js:46` only runs the `acTL` byte scan when `file.type === 'image/png'`, while WebP and AVIF are detected purely from bytes. The PR description itself notes the browser takes the type from the file name, so an animated PNG uploaded with a wrong extension (e.g. `photo.jpg`, typed `image/jpeg`) skips every animation check, hits the canvas path in `resizeImage`, and is uploaded as a still frame. That is the exact animation-loss data loss this PR eliminates for the other two formats, and the existing `reads the bytes whatever the type says` test covers misnamed WebP but not misnamed PNG. Smallest fix: drop the MIME gate and always run the byte scan, since `isAnimatedPng` already reads the content itself: ```js export const isAnimated = async (file) => (await isAnimatedWebP(file)) || (await isAnimatedAvif(file)) || (await isAnimatedPng(file)) ``` ### Merge readiness No P0/P1 issues found; the WebP/AVIF paths, the SVG bypass, the `onerror` fallback, and the keep-original-if-larger logic are all correct. Rating: **4/5** (capped by the P2 above). opencode-agent:job=181:generation=1
hj requested changes 2026-11-10 13:54:29 +00:00
@ -0,0 +1,33 @@
// A PNG that compresses well, like a photo: noise, which PNG keeps byte for
// byte and WebP or JPEG make much smaller. The same pixels every time.
export const photo = (width, height, name = 'photo.png') =>
new Promise((resolve) => {
Member

nit: instead of Promise((resolve) => {}) you can just do async () => {}

nit: instead of `Promise((resolve) => {})` you can just do `async () => {}`
@ -0,0 +9,4 @@
const pixels = context.createImageData(width, height)
let seed = 1
for (let i = 0; i < pixels.data.length; i++) {
seed = (seed * 1103515245 + 12345) & 0x7fffffff
Member

would be nice to have explanation for these magic numbers

would be nice to have explanation for these magic numbers
Member

I agree with OC that there should be consistency with checking for every type, either add mime check for the rest or remove one from apng

I agree with OC that there should be consistency with checking for every type, either add mime check for the rest or remove one from apng
test: explain the photo fixture's generator, use async
All checks were successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/test-e2e Pipeline was successful
bb82f19fd4
Author
Owner

Addressed:

  • PNG is now found from the bytes too, without the type check. To keep that cheap and exact for every upload, it checks the PNG signature in the first bytes and then walks the chunks for an acTL before the first IDAT (as APNG requires), instead of scanning the whole file for the letters. Tests for a misnamed APNG and for acTL only inside chunk data.
  • photo.js: the generator is explained (the LCG constants of the C standard's rand() example) and now uses Math.imul, so it is exact 32-bit math; photo and flat are async, with only toBlob wrapped in a Promise.
Addressed: - PNG is now found from the bytes too, without the type check. To keep that cheap and exact for every upload, it checks the PNG signature in the first bytes and then walks the chunks for an `acTL` before the first `IDAT` (as APNG requires), instead of scanning the whole file for the letters. Tests for a misnamed APNG and for `acTL` only inside chunk data. - `photo.js`: the generator is explained (the LCG constants of the C standard's `rand()` example) and now uses `Math.imul`, so it is exact 32-bit math; `photo` and `flat` are `async`, with only `toBlob` wrapped in a Promise.
Author
Owner

/oc review

/oc review
opencode-fix-bot left a comment

Summary

This PR keeps animated pictures intact during upload compression by sniffing animation markers from file bytes instead of trusting file.type, and it fixes three more resizeImage defects: SVG rasterization, keeping a larger re-encode, and the hang on files the browser cannot decode. I checked the complete base-to-head diff at bb82f19 and re-verified the detection routines with a Node harness covering the spec cases plus truncated and empty inputs.

Verification

  • src/services/animated_image/animated_image.js: WebP VP8X animation flag at byte 20, AVIF avis major or compatible brand bounded by the ftyp size, and PNG signature plus chunk walk for acTL before the first IDAT all match their container layouts. Truncated and empty files safely return false.
  • src/components/media_upload/media_upload.js: animated files bypass the canvas, SVG and GIF bypass by type, onerror uploads the original instead of hanging, and toBlob output is used only when it is smaller.
  • Prior findings are addressed in the current code: PNG animation is now detected from bytes with no MIME gate, including the misnamed-APNG case, and test/fixtures/photo.js now explains the LCG constants and uses Math.imul with async helpers.

Findings

No open defects. No P0, P1, or P2 findings remain.

Merge readiness

No merge blockers found. Rating: 5/5.

opencode-agent:job=183:generation=1

### Summary This PR keeps animated pictures intact during upload compression by sniffing animation markers from file bytes instead of trusting `file.type`, and it fixes three more `resizeImage` defects: SVG rasterization, keeping a larger re-encode, and the hang on files the browser cannot decode. I checked the complete base-to-head diff at `bb82f19` and re-verified the detection routines with a Node harness covering the spec cases plus truncated and empty inputs. ### Verification - `src/services/animated_image/animated_image.js`: WebP VP8X animation flag at byte 20, AVIF `avis` major or compatible brand bounded by the `ftyp` size, and PNG signature plus chunk walk for `acTL` before the first `IDAT` all match their container layouts. Truncated and empty files safely return false. - `src/components/media_upload/media_upload.js`: animated files bypass the canvas, SVG and GIF bypass by type, `onerror` uploads the original instead of hanging, and `toBlob` output is used only when it is smaller. - Prior findings are addressed in the current code: PNG animation is now detected from bytes with no MIME gate, including the misnamed-APNG case, and `test/fixtures/photo.js` now explains the LCG constants and uses `Math.imul` with `async` helpers. ### Findings No open defects. No P0, P1, or P2 findings remain. ### Merge readiness No merge blockers found. Rating: **5/5**. opencode-agent:job=183:generation=1
All checks were successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/test-e2e Pipeline was successful
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin fix/animated-webp-upload:fix/animated-webp-upload
git switch fix/animated-webp-upload

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch develop
git merge --no-ff fix/animated-webp-upload
git switch fix/animated-webp-upload
git rebase develop
git switch develop
git merge --ff-only fix/animated-webp-upload
git switch fix/animated-webp-upload
git rebase develop
git switch develop
git merge --no-ff fix/animated-webp-upload
git switch develop
git merge --squash fix/animated-webp-upload
git switch develop
git merge --ff-only fix/animated-webp-upload
git switch develop
git merge fix/animated-webp-upload
git push origin develop
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
pleroma/pleroma-fe!3611
No description provided.