Support APNGs in Sticker Creator
This commit is contained in:
parent
6b3d5c19b3
commit
bdd71e4898
20 changed files with 542 additions and 62 deletions
57
ts/test/util/getAnimatedPngDataIfExists_test.ts
Normal file
57
ts/test/util/getAnimatedPngDataIfExists_test.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { assert } from 'chai';
|
||||
|
||||
import { getAnimatedPngDataIfExists } from '../../util/getAnimatedPngDataIfExists';
|
||||
|
||||
describe('getAnimatedPngDataIfExists', () => {
|
||||
const fixture = (filename: string): Promise<Buffer> => {
|
||||
const fixturePath = path.join(
|
||||
__dirname,
|
||||
'..',
|
||||
'..',
|
||||
'..',
|
||||
'fixtures',
|
||||
filename
|
||||
);
|
||||
return fs.promises.readFile(fixturePath);
|
||||
};
|
||||
|
||||
it('returns null for empty buffers', () => {
|
||||
assert.isNull(getAnimatedPngDataIfExists(Buffer.alloc(0)));
|
||||
});
|
||||
|
||||
it('returns null for non-PNG files', async () => {
|
||||
await Promise.all(
|
||||
[
|
||||
'kitten-1-64-64.jpg',
|
||||
'512x515-thumbs-up-lincoln.webp',
|
||||
'giphy-GVNvOUpeYmI7e.gif',
|
||||
'pixabay-Soap-Bubble-7141.mp4',
|
||||
'lorem-ipsum.txt',
|
||||
].map(async filename => {
|
||||
assert.isNull(getAnimatedPngDataIfExists(await fixture(filename)));
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('returns null for non-animated PNG files', async () => {
|
||||
assert.isNull(
|
||||
getAnimatedPngDataIfExists(await fixture('20x200-yellow.png'))
|
||||
);
|
||||
});
|
||||
|
||||
it('returns data for animated PNG files', async () => {
|
||||
assert.deepEqual(
|
||||
getAnimatedPngDataIfExists(
|
||||
await fixture('Animated_PNG_example_bouncing_beach_ball.png')
|
||||
),
|
||||
{ numPlays: Infinity }
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
getAnimatedPngDataIfExists(await fixture('apng_with_2_plays.png')),
|
||||
{ numPlays: 2 }
|
||||
);
|
||||
});
|
||||
});
|
92
ts/test/util/sniffImageMimeType_test.ts
Normal file
92
ts/test/util/sniffImageMimeType_test.ts
Normal file
|
@ -0,0 +1,92 @@
|
|||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { assert } from 'chai';
|
||||
import {
|
||||
IMAGE_BMP,
|
||||
IMAGE_GIF,
|
||||
IMAGE_ICO,
|
||||
IMAGE_JPEG,
|
||||
IMAGE_PNG,
|
||||
IMAGE_WEBP,
|
||||
} from '../../types/MIME';
|
||||
|
||||
import { sniffImageMimeType } from '../../util/sniffImageMimeType';
|
||||
|
||||
describe('sniffImageMimeType', () => {
|
||||
const fixture = (filename: string): Promise<Buffer> => {
|
||||
const fixturePath = path.join(
|
||||
__dirname,
|
||||
'..',
|
||||
'..',
|
||||
'..',
|
||||
'fixtures',
|
||||
filename
|
||||
);
|
||||
return fs.promises.readFile(fixturePath);
|
||||
};
|
||||
|
||||
it('returns undefined for empty buffers', () => {
|
||||
assert.isUndefined(sniffImageMimeType(new Uint8Array()));
|
||||
});
|
||||
|
||||
it('returns undefined for non-image files', async () => {
|
||||
await Promise.all(
|
||||
['pixabay-Soap-Bubble-7141.mp4', 'lorem-ipsum.txt'].map(
|
||||
async filename => {
|
||||
assert.isUndefined(sniffImageMimeType(await fixture(filename)));
|
||||
}
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it('sniffs ICO files', async () => {
|
||||
assert.strictEqual(
|
||||
sniffImageMimeType(await fixture('kitten-1-64-64.ico')),
|
||||
IMAGE_ICO
|
||||
);
|
||||
});
|
||||
|
||||
it('sniffs BMP files', async () => {
|
||||
assert.strictEqual(sniffImageMimeType(await fixture('2x2.bmp')), IMAGE_BMP);
|
||||
});
|
||||
|
||||
it('sniffs GIF files', async () => {
|
||||
assert.strictEqual(
|
||||
sniffImageMimeType(await fixture('giphy-GVNvOUpeYmI7e.gif')),
|
||||
IMAGE_GIF
|
||||
);
|
||||
});
|
||||
|
||||
it('sniffs WEBP files', async () => {
|
||||
assert.strictEqual(
|
||||
sniffImageMimeType(await fixture('512x515-thumbs-up-lincoln.webp')),
|
||||
IMAGE_WEBP
|
||||
);
|
||||
});
|
||||
|
||||
it('sniffs PNG files', async () => {
|
||||
await Promise.all(
|
||||
[
|
||||
'freepngs-2cd43b_bed7d1327e88454487397574d87b64dc_mv2.png',
|
||||
'Animated_PNG_example_bouncing_beach_ball.png',
|
||||
].map(async filename => {
|
||||
assert.strictEqual(
|
||||
sniffImageMimeType(await fixture(filename)),
|
||||
IMAGE_PNG
|
||||
);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('sniffs JPEG files', async () => {
|
||||
assert.strictEqual(
|
||||
sniffImageMimeType(await fixture('kitten-1-64-64.jpg')),
|
||||
IMAGE_JPEG
|
||||
);
|
||||
});
|
||||
|
||||
it('handles ArrayBuffers', async () => {
|
||||
const arrayBuffer = (await fixture('kitten-1-64-64.jpg')).buffer;
|
||||
assert.strictEqual(sniffImageMimeType(arrayBuffer), IMAGE_JPEG);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue