aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/helpers/image.ts
blob: 530c9bacd962025f6756c6086b6e8bf2f68bb16f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { readFile, remove } from 'fs-extra'
import { join } from 'path'
import { execPromise } from '@server/helpers/core-utils'
import { buildAbsoluteFixturePath, root } from '@shared/core-utils'
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
  }
}

async function hasTitleExif (path: string) {
  const result = JSON.parse(await execPromise(`exiftool -json ${path}`))

  return result[0]?.Title === 'should be removed'
}

describe('Image helpers', function () {
  const imageDestDir = join(root(), 'test-images')

  const imageDestJPG = join(imageDestDir, 'test.jpg')
  const imageDestPNG = join(imageDestDir, 'test.png')

  const thumbnailSize = { width: 280, height: 157 }

  it('Should skip processing if the source image is okay', async function () {
    const input = buildAbsoluteFixturePath('thumbnail.jpg')
    await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })

    await checkBuffers(input, imageDestJPG, true)
  })

  it('Should not skip processing if the source image does not have the appropriate extension', async function () {
    const input = buildAbsoluteFixturePath('thumbnail.png')
    await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })

    await checkBuffers(input, imageDestJPG, false)
  })

  it('Should not skip processing if the source image does not have the appropriate size', async function () {
    const input = buildAbsoluteFixturePath('preview.jpg')
    await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true })

    await checkBuffers(input, imageDestJPG, false)
  })

  it('Should not skip processing if the source image does not have the appropriate size', async function () {
    const input = buildAbsoluteFixturePath('thumbnail-big.jpg')
    await processImage({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: 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({ path: input, destination: imageDestJPG, newSize: { width: 100, height: 100 }, keepOriginal: 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({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: 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

    await processImage({ path: input, destination: imageDestPNG, newSize: thumbnailSize, keepOriginal: true })
    expect(await hasTitleExif(imageDestPNG)).to.be.false
  })

  after(async function () {
    await remove(imageDestDir)
  })
})