diff options
Diffstat (limited to 'packages/tests/src/server-helpers/image.ts')
-rw-r--r-- | packages/tests/src/server-helpers/image.ts | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/packages/tests/src/server-helpers/image.ts b/packages/tests/src/server-helpers/image.ts new file mode 100644 index 000000000..34675d385 --- /dev/null +++ b/packages/tests/src/server-helpers/image.ts | |||
@@ -0,0 +1,97 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { remove } from 'fs-extra/esm' | ||
5 | import { readFile } from 'fs/promises' | ||
6 | import { join } from 'path' | ||
7 | import { buildAbsoluteFixturePath, root } from '@peertube/peertube-node-utils' | ||
8 | import { execPromise } from '@peertube/peertube-server/server/helpers/core-utils.js' | ||
9 | import { processImage } from '@peertube/peertube-server/server/helpers/image-utils.js' | ||
10 | |||
11 | async function checkBuffers (path1: string, path2: string, equals: boolean) { | ||
12 | const [ buf1, buf2 ] = await Promise.all([ | ||
13 | readFile(path1), | ||
14 | readFile(path2) | ||
15 | ]) | ||
16 | |||
17 | if (equals) { | ||
18 | expect(buf1.equals(buf2)).to.be.true | ||
19 | } else { | ||
20 | expect(buf1.equals(buf2)).to.be.false | ||
21 | } | ||
22 | } | ||
23 | |||
24 | async function hasTitleExif (path: string) { | ||
25 | const result = JSON.parse(await execPromise(`exiftool -json ${path}`)) | ||
26 | |||
27 | return result[0]?.Title === 'should be removed' | ||
28 | } | ||
29 | |||
30 | describe('Image helpers', function () { | ||
31 | const imageDestDir = join(root(), 'test-images') | ||
32 | |||
33 | const imageDestJPG = join(imageDestDir, 'test.jpg') | ||
34 | const imageDestPNG = join(imageDestDir, 'test.png') | ||
35 | |||
36 | const thumbnailSize = { width: 280, height: 157 } | ||
37 | |||
38 | it('Should skip processing if the source image is okay', async function () { | ||
39 | const input = buildAbsoluteFixturePath('custom-thumbnail.jpg') | ||
40 | await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true }) | ||
41 | |||
42 | await checkBuffers(input, imageDestJPG, true) | ||
43 | }) | ||
44 | |||
45 | it('Should not skip processing if the source image does not have the appropriate extension', async function () { | ||
46 | const input = buildAbsoluteFixturePath('custom-thumbnail.png') | ||
47 | await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true }) | ||
48 | |||
49 | await checkBuffers(input, imageDestJPG, false) | ||
50 | }) | ||
51 | |||
52 | it('Should not skip processing if the source image does not have the appropriate size', async function () { | ||
53 | const input = buildAbsoluteFixturePath('custom-preview.jpg') | ||
54 | await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true }) | ||
55 | |||
56 | await checkBuffers(input, imageDestJPG, false) | ||
57 | }) | ||
58 | |||
59 | it('Should not skip processing if the source image does not have the appropriate size', async function () { | ||
60 | const input = buildAbsoluteFixturePath('custom-thumbnail-big.jpg') | ||
61 | await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true }) | ||
62 | |||
63 | await checkBuffers(input, imageDestJPG, false) | ||
64 | }) | ||
65 | |||
66 | it('Should strip exif for a jpg file that can not be copied', async function () { | ||
67 | const input = buildAbsoluteFixturePath('exif.jpg') | ||
68 | expect(await hasTitleExif(input)).to.be.true | ||
69 | |||
70 | await processImage({ path: input, destination: imageDestJPG, newSize: { width: 100, height: 100 }, keepOriginal: true }) | ||
71 | await checkBuffers(input, imageDestJPG, false) | ||
72 | |||
73 | expect(await hasTitleExif(imageDestJPG)).to.be.false | ||
74 | }) | ||
75 | |||
76 | it('Should strip exif for a jpg file that could be copied', async function () { | ||
77 | const input = buildAbsoluteFixturePath('exif.jpg') | ||
78 | expect(await hasTitleExif(input)).to.be.true | ||
79 | |||
80 | await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true }) | ||
81 | await checkBuffers(input, imageDestJPG, false) | ||
82 | |||
83 | expect(await hasTitleExif(imageDestJPG)).to.be.false | ||
84 | }) | ||
85 | |||
86 | it('Should strip exif for png', async function () { | ||
87 | const input = buildAbsoluteFixturePath('exif.png') | ||
88 | expect(await hasTitleExif(input)).to.be.true | ||
89 | |||
90 | await processImage({ path: input, destination: imageDestPNG, newSize: thumbnailSize, keepOriginal: true }) | ||
91 | expect(await hasTitleExif(imageDestPNG)).to.be.false | ||
92 | }) | ||
93 | |||
94 | after(async function () { | ||
95 | await remove(imageDestDir) | ||
96 | }) | ||
97 | }) | ||