]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/helpers/image.ts
Remove exif tags when processing images
[github/Chocobozzz/PeerTube.git] / server / tests / helpers / image.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
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'
10
11 async function checkBuffers (path1: string, path2: string, equals: boolean) {
12 const [ buf1, buf2 ] = await Promise.all([
13 readFile(path1),
14 readFile(path2)
15 ])
16
17 if (equals) {
18 expect(buf1.equals(buf2)).to.be.true
19 } else {
20 expect(buf1.equals(buf2)).to.be.false
21 }
22 }
23
24 async function hasTitleExif (path: string) {
25 const result = JSON.parse(await execPromise(`exiftool -json ${path}`))
26
27 return result[0]?.Title === 'should be removed'
28 }
29
30 describe('Image helpers', function () {
31 const imageDestDir = join(root(), 'test-images')
32
33 const imageDestJPG = join(imageDestDir, 'test.jpg')
34 const imageDestPNG = join(imageDestDir, 'test.png')
35
36 const thumbnailSize = { width: 223, height: 122 }
37
38 it('Should skip processing if the source image is okay', async function () {
39 const input = buildAbsoluteFixturePath('thumbnail.jpg')
40 await processImage(input, imageDestJPG, thumbnailSize, true)
41
42 await checkBuffers(input, imageDestJPG, true)
43 })
44
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(input, imageDestJPG, thumbnailSize, true)
48
49 await checkBuffers(input, imageDestJPG, false)
50 })
51
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(input, imageDestJPG, thumbnailSize, true)
55
56 await checkBuffers(input, imageDestJPG, false)
57 })
58
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(input, imageDestJPG, thumbnailSize, true)
62
63 await checkBuffers(input, imageDestJPG, false)
64 })
65
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
69
70 await processImage(input, imageDestJPG, { width: 100, height: 100 }, true)
71 await checkBuffers(input, imageDestJPG, false)
72
73 expect(await hasTitleExif(imageDestJPG)).to.be.false
74 })
75
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
79
80 await processImage(input, imageDestJPG, thumbnailSize, true)
81 await checkBuffers(input, imageDestJPG, false)
82
83 expect(await hasTitleExif(imageDestJPG)).to.be.false
84 })
85
86 it('Should strip exif for png', async function () {
87 const input = buildAbsoluteFixturePath('exif.png')
88 expect(await hasTitleExif(input)).to.be.true
89
90 await processImage(input, imageDestPNG, thumbnailSize, true)
91 expect(await hasTitleExif(imageDestPNG)).to.be.false
92 })
93
94 after(async function () {
95 await remove(imageDestDir)
96 })
97 })