]>
Commit | Line | Data |
---|---|---|
1664bc60 C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
3 | import 'mocha' | |
4c7e60bc | 4 | import { expect } from 'chai' |
1664bc60 C |
5 | import { readFile, remove } from 'fs-extra' |
6 | import { join } from 'path' | |
0c058f25 | 7 | import { execPromise } from '@server/helpers/core-utils' |
c55e3d72 | 8 | import { buildAbsoluteFixturePath, root } from '@shared/core-utils' |
1664bc60 | 9 | import { processImage } from '../../../server/helpers/image-utils' |
1664bc60 C |
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 | ||
0c058f25 C |
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 | ||
1664bc60 C |
30 | describe('Image helpers', function () { |
31 | const imageDestDir = join(root(), 'test-images') | |
0c058f25 C |
32 | |
33 | const imageDestJPG = join(imageDestDir, 'test.jpg') | |
34 | const imageDestPNG = join(imageDestDir, 'test.png') | |
35 | ||
1664bc60 C |
36 | const thumbnailSize = { width: 223, height: 122 } |
37 | ||
38 | it('Should skip processing if the source image is okay', async function () { | |
39 | const input = buildAbsoluteFixturePath('thumbnail.jpg') | |
0c058f25 | 40 | await processImage(input, imageDestJPG, thumbnailSize, true) |
1664bc60 | 41 | |
0c058f25 | 42 | await checkBuffers(input, imageDestJPG, true) |
1664bc60 C |
43 | }) |
44 | ||
45 | it('Should not skip processing if the source image does not have the appropriate extension', async function () { | |
46 | const input = buildAbsoluteFixturePath('thumbnail.png') | |
0c058f25 | 47 | await processImage(input, imageDestJPG, thumbnailSize, true) |
1664bc60 | 48 | |
0c058f25 | 49 | await checkBuffers(input, imageDestJPG, false) |
1664bc60 C |
50 | }) |
51 | ||
52 | it('Should not skip processing if the source image does not have the appropriate size', async function () { | |
53 | const input = buildAbsoluteFixturePath('preview.jpg') | |
0c058f25 | 54 | await processImage(input, imageDestJPG, thumbnailSize, true) |
1664bc60 | 55 | |
0c058f25 | 56 | await checkBuffers(input, imageDestJPG, false) |
1664bc60 C |
57 | }) |
58 | ||
59 | it('Should not skip processing if the source image does not have the appropriate size', async function () { | |
60 | const input = buildAbsoluteFixturePath('thumbnail-big.jpg') | |
0c058f25 C |
61 | await processImage(input, imageDestJPG, thumbnailSize, 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(input, imageDestJPG, { width: 100, height: 100 }, 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(input, imageDestJPG, thumbnailSize, 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 | |
1664bc60 | 89 | |
0c058f25 C |
90 | await processImage(input, imageDestPNG, thumbnailSize, true) |
91 | expect(await hasTitleExif(imageDestPNG)).to.be.false | |
1664bc60 C |
92 | }) |
93 | ||
94 | after(async function () { | |
0c058f25 | 95 | await remove(imageDestDir) |
1664bc60 C |
96 | }) |
97 | }) |