1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { readFile, remove } from 'fs-extra'
5 import { join } from 'path'
6 import { execPromise } from '@server/helpers/core-utils'
7 import { buildAbsoluteFixturePath, root } from '@shared/core-utils'
8 import { processImage } from '../../../server/helpers/image-utils'
10 async function checkBuffers (path1: string, path2: string, equals: boolean) {
11 const [ buf1, buf2 ] = await Promise.all([
17 expect(buf1.equals(buf2)).to.be.true
19 expect(buf1.equals(buf2)).to.be.false
23 async function hasTitleExif (path: string) {
24 const result = JSON.parse(await execPromise(`exiftool -json ${path}`))
26 return result[0]?.Title === 'should be removed'
29 describe('Image helpers', function () {
30 const imageDestDir = join(root(), 'test-images')
32 const imageDestJPG = join(imageDestDir, 'test.jpg')
33 const imageDestPNG = join(imageDestDir, 'test.png')
35 const thumbnailSize = { width: 223, height: 122 }
37 it('Should skip processing if the source image is okay', async function () {
38 const input = buildAbsoluteFixturePath('thumbnail.jpg')
39 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
41 await checkBuffers(input, imageDestJPG, true)
44 it('Should not skip processing if the source image does not have the appropriate extension', async function () {
45 const input = buildAbsoluteFixturePath('thumbnail.png')
46 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
48 await checkBuffers(input, imageDestJPG, false)
51 it('Should not skip processing if the source image does not have the appropriate size', async function () {
52 const input = buildAbsoluteFixturePath('preview.jpg')
53 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
55 await checkBuffers(input, imageDestJPG, false)
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')
60 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
62 await checkBuffers(input, imageDestJPG, false)
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
69 await processImage({ path: input, destination: imageDestJPG, newSize: { width: 100, height: 100 }, keepOriginal: true })
70 await checkBuffers(input, imageDestJPG, false)
72 expect(await hasTitleExif(imageDestJPG)).to.be.false
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
79 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
80 await checkBuffers(input, imageDestJPG, false)
82 expect(await hasTitleExif(imageDestJPG)).to.be.false
85 it('Should strip exif for png', async function () {
86 const input = buildAbsoluteFixturePath('exif.png')
87 expect(await hasTitleExif(input)).to.be.true
89 await processImage({ path: input, destination: imageDestPNG, newSize: thumbnailSize, keepOriginal: true })
90 expect(await hasTitleExif(imageDestPNG)).to.be.false
93 after(async function () {
94 await remove(imageDestDir)