1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { readFile, remove } from 'fs-extra'
6 import { join } from 'path'
7 import { execPromise } from '@server/helpers/core-utils'
8 import { buildAbsoluteFixturePath, root } from '@shared/core-utils'
9 import { processImage } from '../../../server/helpers/image-utils'
11 async function checkBuffers (path1: string, path2: string, equals: boolean) {
12 const [ buf1, buf2 ] = await Promise.all([
18 expect(buf1.equals(buf2)).to.be.true
20 expect(buf1.equals(buf2)).to.be.false
24 async function hasTitleExif (path: string) {
25 const result = JSON.parse(await execPromise(`exiftool -json ${path}`))
27 return result[0]?.Title === 'should be removed'
30 describe('Image helpers', function () {
31 const imageDestDir = join(root(), 'test-images')
33 const imageDestJPG = join(imageDestDir, 'test.jpg')
34 const imageDestPNG = join(imageDestDir, 'test.png')
36 const thumbnailSize = { width: 223, height: 122 }
38 it('Should skip processing if the source image is okay', async function () {
39 const input = buildAbsoluteFixturePath('thumbnail.jpg')
40 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
42 await checkBuffers(input, imageDestJPG, true)
45 it('Should not skip processing if the source image does not have the appropriate extension', async function () {
46 const input = buildAbsoluteFixturePath('thumbnail.png')
47 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
49 await checkBuffers(input, imageDestJPG, false)
52 it('Should not skip processing if the source image does not have the appropriate size', async function () {
53 const input = buildAbsoluteFixturePath('preview.jpg')
54 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
56 await checkBuffers(input, imageDestJPG, false)
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')
61 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
63 await checkBuffers(input, imageDestJPG, false)
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
70 await processImage({ path: input, destination: imageDestJPG, newSize: { width: 100, height: 100 }, keepOriginal: true })
71 await checkBuffers(input, imageDestJPG, false)
73 expect(await hasTitleExif(imageDestJPG)).to.be.false
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
80 await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })
81 await checkBuffers(input, imageDestJPG, false)
83 expect(await hasTitleExif(imageDestJPG)).to.be.false
86 it('Should strip exif for png', async function () {
87 const input = buildAbsoluteFixturePath('exif.png')
88 expect(await hasTitleExif(input)).to.be.true
90 await processImage({ path: input, destination: imageDestPNG, newSize: thumbnailSize, keepOriginal: true })
91 expect(await hasTitleExif(imageDestPNG)).to.be.false
94 after(async function () {
95 await remove(imageDestDir)