Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85951105
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/changelog.d/animated-webp-upload.fix b/changelog.d/animated-webp-upload.fix
index c5306a3484..320486b42f 100644
--- a/changelog.d/animated-webp-upload.fix
+++ b/changelog.d/animated-webp-upload.fix
@@ -1 +1 @@
-Animated WebP and AVIF pictures keep their animation when image compression is on
+Animated WebP, AVIF and PNG pictures keep their animation when image compression is on, also with a wrong file extension
diff --git a/src/services/animated_image/animated_image.js b/src/services/animated_image/animated_image.js
index 7062645c85..87c44ae8b3 100644
--- a/src/services/animated_image/animated_image.js
+++ b/src/services/animated_image/animated_image.js
@@ -1,46 +1,52 @@
// Whether a picture is animated, read from its bytes: the browser takes the
// type from the file name, and an animated WebP named .gif is common.
const ascii = (bytes, start, end) =>
String.fromCharCode(...bytes.subarray(start, end))
const head = async (file) =>
new Uint8Array(await file.slice(0, 64).arrayBuffer())
// a VP8X chunk with the animation flag
export const isAnimatedWebP = async (file) => {
const bytes = await head(file)
return (
ascii(bytes, 0, 4) === 'RIFF' &&
ascii(bytes, 8, 16) === 'WEBPVP8X' &&
(bytes[20] & 0x02) !== 0
)
}
// the avis brand in the ftyp box: the major brand, or a compatible brand
// after the minor version. A box size of 0 goes to the end of the file.
export const isAnimatedAvif = async (file) => {
const bytes = await head(file)
if (ascii(bytes, 4, 8) !== 'ftyp') return false
const size = new DataView(bytes.buffer).getUint32(0)
const end = size === 0 ? bytes.length : Math.min(size, bytes.length)
if (ascii(bytes, 8, 12) === 'avis') return true
for (let i = 16; i + 4 <= end; i += 4) {
if (ascii(bytes, i, i + 4) === 'avis') return true
}
return false
}
-// an acTL chunk
+// an acTL chunk before the first IDAT, as APNG needs; the chunks are
+// walked, so the letters inside other data do not count
export const isAnimatedPng = async (file) => {
+ if (ascii(await head(file), 0, 8) !== '\x89PNG\r\n\x1a\n') return false
const bytes = new Uint8Array(await file.arrayBuffer())
- for (let i = 0; i < bytes.length - 3; i++) {
- if (ascii(bytes, i, i + 4) === 'acTL') return true
+ const view = new DataView(bytes.buffer)
+ // a chunk: length, type, data, CRC
+ for (let pos = 8; pos + 8 <= bytes.length; pos += 12 + view.getUint32(pos)) {
+ const type = ascii(bytes, pos + 4, pos + 8)
+ if (type === 'acTL') return true
+ if (type === 'IDAT') return false
}
return false
}
export const isAnimated = async (file) =>
(await isAnimatedWebP(file)) ||
(await isAnimatedAvif(file)) ||
- (file.type === 'image/png' && (await isAnimatedPng(file)))
+ (await isAnimatedPng(file))
diff --git a/test/unit/specs/services/animated_image.spec.js b/test/unit/specs/services/animated_image.spec.js
index c5066e4e9f..e1d0da4155 100644
--- a/test/unit/specs/services/animated_image.spec.js
+++ b/test/unit/specs/services/animated_image.spec.js
@@ -1,66 +1,90 @@
import {
isAnimated,
isAnimatedAvif,
isAnimatedPng,
isAnimatedWebP,
} from 'src/services/animated_image/animated_image.js'
const bytes = (...parts) =>
new Uint8Array(
parts.flatMap((p) =>
typeof p === 'string' ? [...p].map((c) => c.charCodeAt(0)) : p,
),
)
const file = (data, type = 'image/png') => new File([data], 'f', { type })
// RIFF header and VP8X chunk with the given flags
const vp8x = (flags) =>
file(bytes('RIFF', [0, 0, 0, 0], 'WEBPVP8X', [10, 0, 0, 0, flags, 0, 0, 0]))
// an ftyp box: major brand, minor version, compatible brands
const ftyp = (size, major, ...compatible) =>
file(bytes([0, 0, 0, size], 'ftyp', major, [0, 0, 0, 0], ...compatible))
+// a PNG of the given chunks: a type, or [type, data]; the CRCs are not checked
+const png = (...chunks) =>
+ file(
+ bytes(
+ [0x89],
+ 'PNG\r\n\x1a\n',
+ ...chunks.flatMap((chunk) => {
+ const [type, data = bytes()] = [chunk].flat()
+ const n = data.length
+ return [
+ [n >>> 24, (n >> 16) & 255, (n >> 8) & 255, n & 255],
+ type,
+ [...data],
+ [0, 0, 0, 0],
+ ]
+ }),
+ ),
+ )
+
describe('animated pictures', () => {
it('finds the animation flag of a WebP', async () => {
expect(await isAnimatedWebP(vp8x(0x02))).to.equal(true)
expect(await isAnimatedWebP(vp8x(0x12))).to.equal(true)
expect(await isAnimatedWebP(vp8x(0x10))).to.equal(false)
const still = file(bytes('RIFF', [0, 0, 0, 0], 'WEBPVP8 '))
expect(await isAnimatedWebP(still)).to.equal(false)
expect(await isAnimatedWebP(file(''))).to.equal(false)
})
it('finds the avis brand of an AVIF', async () => {
expect(await isAnimatedAvif(ftyp(24, 'avis', 'avif', 'mif1'))).to.equal(
true,
)
expect(
await isAnimatedAvif(ftyp(28, 'avif', 'mif1', 'miaf', 'avis')),
).to.equal(true)
expect(await isAnimatedAvif(ftyp(0, 'avif', 'mif1', 'avis'))).to.equal(true)
expect(await isAnimatedAvif(ftyp(24, 'avif', 'mif1', 'miaf'))).to.equal(
false,
)
// a brand after the end of the box is not one
expect(await isAnimatedAvif(ftyp(20, 'avif', 'mif1', 'avis'))).to.equal(
false,
)
})
- it('finds the acTL chunk of a PNG', async () => {
- expect(await isAnimatedPng(file(bytes([0x89], 'PNG', 'acTL')))).to.equal(
- true,
- )
- expect(await isAnimatedPng(file(bytes([0x89], 'PNG', 'IDAT')))).to.equal(
+ it('finds an acTL chunk before the image data of a PNG', async () => {
+ expect(await isAnimatedPng(png('IHDR', 'acTL', 'IDAT'))).to.equal(true)
+ expect(await isAnimatedPng(png('IHDR', 'IDAT'))).to.equal(false)
+ // APNG needs acTL before IDAT, and only chunk types count
+ expect(await isAnimatedPng(png('IHDR', 'IDAT', 'acTL'))).to.equal(false)
+ expect(await isAnimatedPng(png(['IHDR', bytes('acTL')], 'IDAT'))).to.equal(
false,
)
+ expect(await isAnimatedPng(file(bytes('acTL')))).to.equal(false)
+ expect(await isAnimatedPng(file(''))).to.equal(false)
})
it('reads the bytes whatever the type says', async () => {
const webp = bytes('RIFF', [0, 0, 0, 0], 'WEBPVP8X', [10, 0, 0, 0, 2])
expect(await isAnimated(file(webp, 'image/gif'))).to.equal(true)
expect(await isAnimated(file(webp, 'image/jpeg'))).to.equal(true)
- expect(await isAnimated(file(bytes('acTL'), 'image/png'))).to.equal(true)
+ const apng = await png('IHDR', 'acTL', 'IDAT').arrayBuffer()
+ expect(await isAnimated(file(apng, 'image/jpeg'))).to.equal(true)
+ expect(await isAnimated(file(apng, 'image/gif'))).to.equal(true)
expect(await isAnimated(file(bytes('x'), 'image/jpeg'))).to.equal(false)
})
})
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Nov 19, 5:51 AM (1 d, 22 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1841006
Default Alt Text
(6 KB)
Attached To
Mode
rPUFE pleroma-fe-upstream
Attached
Detach File
Event Timeline