image.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  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'
  9. async function checkBuffers (path1: string, path2: string, equals: boolean) {
  10. const [ buf1, buf2 ] = await Promise.all([
  11. readFile(path1),
  12. readFile(path2)
  13. ])
  14. if (equals) {
  15. expect(buf1.equals(buf2)).to.be.true
  16. } else {
  17. expect(buf1.equals(buf2)).to.be.false
  18. }
  19. }
  20. async function hasTitleExif (path: string) {
  21. const result = JSON.parse(await execPromise(`exiftool -json ${path}`))
  22. return result[0]?.Title === 'should be removed'
  23. }
  24. describe('Image helpers', function () {
  25. const imageDestDir = join(root(), 'test-images')
  26. const imageDestJPG = join(imageDestDir, 'test.jpg')
  27. const imageDestPNG = join(imageDestDir, 'test.png')
  28. const thumbnailSize = { width: 223, height: 122 }
  29. it('Should skip processing if the source image is okay', async function () {
  30. const input = buildAbsoluteFixturePath('thumbnail.jpg')
  31. await processImage(input, imageDestJPG, thumbnailSize, true)
  32. await checkBuffers(input, imageDestJPG, true)
  33. })
  34. it('Should not skip processing if the source image does not have the appropriate extension', async function () {
  35. const input = buildAbsoluteFixturePath('thumbnail.png')
  36. await processImage(input, imageDestJPG, thumbnailSize, true)
  37. await checkBuffers(input, imageDestJPG, false)
  38. })
  39. it('Should not skip processing if the source image does not have the appropriate size', async function () {
  40. const input = buildAbsoluteFixturePath('preview.jpg')
  41. await processImage(input, imageDestJPG, thumbnailSize, true)
  42. await checkBuffers(input, imageDestJPG, false)
  43. })
  44. it('Should not skip processing if the source image does not have the appropriate size', async function () {
  45. const input = buildAbsoluteFixturePath('thumbnail-big.jpg')
  46. await processImage(input, imageDestJPG, thumbnailSize, true)
  47. await checkBuffers(input, imageDestJPG, false)
  48. })
  49. it('Should strip exif for a jpg file that can not be copied', async function () {
  50. const input = buildAbsoluteFixturePath('exif.jpg')
  51. expect(await hasTitleExif(input)).to.be.true
  52. await processImage(input, imageDestJPG, { width: 100, height: 100 }, true)
  53. await checkBuffers(input, imageDestJPG, false)
  54. expect(await hasTitleExif(imageDestJPG)).to.be.false
  55. })
  56. it('Should strip exif for a jpg file that could be copied', async function () {
  57. const input = buildAbsoluteFixturePath('exif.jpg')
  58. expect(await hasTitleExif(input)).to.be.true
  59. await processImage(input, imageDestJPG, thumbnailSize, true)
  60. await checkBuffers(input, imageDestJPG, false)
  61. expect(await hasTitleExif(imageDestJPG)).to.be.false
  62. })
  63. it('Should strip exif for png', async function () {
  64. const input = buildAbsoluteFixturePath('exif.png')
  65. expect(await hasTitleExif(input)).to.be.true
  66. await processImage(input, imageDestPNG, thumbnailSize, true)
  67. expect(await hasTitleExif(imageDestPNG)).to.be.false
  68. })
  69. after(async function () {
  70. await remove(imageDestDir)
  71. })
  72. })