2021-02-16 04:19:09 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
|
|
|
|
|
|
|
import 'mocha'
|
2021-07-16 08:27:30 -04:00
|
|
|
import { expect } from 'chai'
|
2021-02-16 04:19:09 -05:00
|
|
|
import { readFile, remove } from 'fs-extra'
|
|
|
|
import { join } from 'path'
|
2022-03-07 11:16:54 -05:00
|
|
|
import { execPromise } from '@server/helpers/core-utils'
|
2021-12-17 05:58:15 -05:00
|
|
|
import { buildAbsoluteFixturePath, root } from '@shared/core-utils'
|
2021-02-16 04:19:09 -05:00
|
|
|
import { processImage } from '../../../server/helpers/image-utils'
|
|
|
|
|
|
|
|
async function checkBuffers (path1: string, path2: string, equals: boolean) {
|
|
|
|
const [ buf1, buf2 ] = await Promise.all([
|
|
|
|
readFile(path1),
|
|
|
|
readFile(path2)
|
|
|
|
])
|
|
|
|
|
|
|
|
if (equals) {
|
|
|
|
expect(buf1.equals(buf2)).to.be.true
|
|
|
|
} else {
|
|
|
|
expect(buf1.equals(buf2)).to.be.false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 11:16:54 -05:00
|
|
|
async function hasTitleExif (path: string) {
|
|
|
|
const result = JSON.parse(await execPromise(`exiftool -json ${path}`))
|
|
|
|
|
|
|
|
return result[0]?.Title === 'should be removed'
|
|
|
|
}
|
|
|
|
|
2021-02-16 04:19:09 -05:00
|
|
|
describe('Image helpers', function () {
|
|
|
|
const imageDestDir = join(root(), 'test-images')
|
2022-03-07 11:16:54 -05:00
|
|
|
|
|
|
|
const imageDestJPG = join(imageDestDir, 'test.jpg')
|
|
|
|
const imageDestPNG = join(imageDestDir, 'test.png')
|
|
|
|
|
2021-02-16 04:19:09 -05:00
|
|
|
const thumbnailSize = { width: 223, height: 122 }
|
|
|
|
|
|
|
|
it('Should skip processing if the source image is okay', async function () {
|
|
|
|
const input = buildAbsoluteFixturePath('thumbnail.jpg')
|
2022-03-07 11:16:54 -05:00
|
|
|
await processImage(input, imageDestJPG, thumbnailSize, true)
|
2021-02-16 04:19:09 -05:00
|
|
|
|
2022-03-07 11:16:54 -05:00
|
|
|
await checkBuffers(input, imageDestJPG, true)
|
2021-02-16 04:19:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not skip processing if the source image does not have the appropriate extension', async function () {
|
|
|
|
const input = buildAbsoluteFixturePath('thumbnail.png')
|
2022-03-07 11:16:54 -05:00
|
|
|
await processImage(input, imageDestJPG, thumbnailSize, true)
|
2021-02-16 04:19:09 -05:00
|
|
|
|
2022-03-07 11:16:54 -05:00
|
|
|
await checkBuffers(input, imageDestJPG, false)
|
2021-02-16 04:19:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not skip processing if the source image does not have the appropriate size', async function () {
|
|
|
|
const input = buildAbsoluteFixturePath('preview.jpg')
|
2022-03-07 11:16:54 -05:00
|
|
|
await processImage(input, imageDestJPG, thumbnailSize, true)
|
2021-02-16 04:19:09 -05:00
|
|
|
|
2022-03-07 11:16:54 -05:00
|
|
|
await checkBuffers(input, imageDestJPG, false)
|
2021-02-16 04:19:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not skip processing if the source image does not have the appropriate size', async function () {
|
|
|
|
const input = buildAbsoluteFixturePath('thumbnail-big.jpg')
|
2022-03-07 11:16:54 -05:00
|
|
|
await processImage(input, imageDestJPG, thumbnailSize, true)
|
|
|
|
|
|
|
|
await checkBuffers(input, imageDestJPG, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should strip exif for a jpg file that can not be copied', async function () {
|
|
|
|
const input = buildAbsoluteFixturePath('exif.jpg')
|
|
|
|
expect(await hasTitleExif(input)).to.be.true
|
|
|
|
|
|
|
|
await processImage(input, imageDestJPG, { width: 100, height: 100 }, true)
|
|
|
|
await checkBuffers(input, imageDestJPG, false)
|
|
|
|
|
|
|
|
expect(await hasTitleExif(imageDestJPG)).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should strip exif for a jpg file that could be copied', async function () {
|
|
|
|
const input = buildAbsoluteFixturePath('exif.jpg')
|
|
|
|
expect(await hasTitleExif(input)).to.be.true
|
|
|
|
|
|
|
|
await processImage(input, imageDestJPG, thumbnailSize, true)
|
|
|
|
await checkBuffers(input, imageDestJPG, false)
|
|
|
|
|
|
|
|
expect(await hasTitleExif(imageDestJPG)).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should strip exif for png', async function () {
|
|
|
|
const input = buildAbsoluteFixturePath('exif.png')
|
|
|
|
expect(await hasTitleExif(input)).to.be.true
|
2021-02-16 04:19:09 -05:00
|
|
|
|
2022-03-07 11:16:54 -05:00
|
|
|
await processImage(input, imageDestPNG, thumbnailSize, true)
|
|
|
|
expect(await hasTitleExif(imageDestPNG)).to.be.false
|
2021-02-16 04:19:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
after(async function () {
|
2022-03-07 11:16:54 -05:00
|
|
|
await remove(imageDestDir)
|
2021-02-16 04:19:09 -05:00
|
|
|
})
|
|
|
|
})
|