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