]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add tests to gif resizer
authorChocobozzz <me@florianbigard.com>
Wed, 25 Nov 2020 08:50:12 +0000 (09:50 +0100)
committerChocobozzz <chocobozzz@cpy.re>
Wed, 25 Nov 2020 09:07:51 +0000 (10:07 +0100)
server/helpers/ffmpeg-utils.ts
server/helpers/image-utils.ts
server/tests/api/users/users.ts
server/tests/fixtures/avatar-resized.gif [new file with mode: 0644]
server/tests/fixtures/avatar.gif [new file with mode: 0644]

index 69defccc4c6dd6b19e353fcb23e9c7a05e5a7bb4..9755dd67c7dbf87af7a2d2d90cd69fc1901cc7c9 100644 (file)
@@ -64,35 +64,14 @@ function convertWebPToJPG (path: string, destination: string): Promise<void> {
 function processGIF (
   path: string,
   destination: string,
-  newSize: { width: number, height: number },
-  keepOriginal = false
+  newSize: { width: number, height: number }
 ): Promise<void> {
-  return new Promise<void>(async (res, rej) => {
-    if (path === destination) {
-      throw new Error('FFmpeg needs an input path different that the output path.')
-    }
-
-    logger.debug('Processing gif %s to %s.', path, destination)
+  const command = ffmpeg(path)
+    .fps(20)
+    .size(`${newSize.width}x${newSize.height}`)
+    .output(destination)
 
-    try {
-      const command = ffmpeg(path)
-        .fps(20)
-        .size(`${newSize.width}x${newSize.height}`)
-        .output(destination)
-
-      command.on('error', (err, stdout, stderr) => {
-        logger.error('Error in ffmpeg gif resizing process.', { stdout, stderr })
-        return rej(err)
-      })
-      .on('end', async () => {
-        if (keepOriginal !== true) await remove(path)
-        res()
-      })
-      .run()
-    } catch (err) {
-      return rej(err)
-    }
-  })
+  return runCommand(command)
 }
 
 async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
index fdf06e848b86a294fa662d23751c699b519260c1..3ebf073058bd1f85e00abc49d5a59748ed4c81a3 100644 (file)
@@ -1,5 +1,5 @@
-import { extname } from 'path'
 import { remove, rename } from 'fs-extra'
+import { extname } from 'path'
 import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
 import { logger } from './logger'
 
@@ -13,17 +13,31 @@ async function processImage (
 ) {
   const extension = extname(path)
 
+  if (path === destination) {
+    throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
+  }
+
+  logger.debug('Processing image %s to %s.', path, destination)
+
   // Use FFmpeg to process GIF
   if (extension === '.gif') {
-    return processGIF(path, destination, newSize, keepOriginal)
+    await processGIF(path, destination, newSize)
+  } else {
+    await jimpProcessor(path, destination, newSize)
   }
 
-  if (path === destination) {
-    throw new Error('Jimp needs an input path different that the output path.')
-  }
+  if (keepOriginal !== true) await remove(path)
+}
 
-  logger.debug('Processing image %s to %s.', path, destination)
+// ---------------------------------------------------------------------------
 
+export {
+  processImage
+}
+
+// ---------------------------------------------------------------------------
+
+async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }) {
   let jimpInstance: any
 
   try {
@@ -44,12 +58,4 @@ async function processImage (
     .resize(newSize.width, newSize.height)
     .quality(80)
     .writeAsync(destination)
-
-  if (keepOriginal !== true) await remove(path)
-}
-
-// ---------------------------------------------------------------------------
-
-export {
-  processImage
 }
index edb0b4bb3d4a8f15669992f363d7c5390227ef33..fe83ca041028c98eeb6ea9153becc11a2e90def1 100644 (file)
@@ -602,8 +602,8 @@ describe('Test users', function () {
       expect(user.account.description).to.be.null
     })
 
-    it('Should be able to update my avatar', async function () {
-      const fixture = 'avatar.png'
+    it('Should be able to update my avatar with a gif', async function () {
+      const fixture = 'avatar.gif'
 
       await updateMyAvatar({
         url: server.url,
@@ -614,7 +614,24 @@ describe('Test users', function () {
       const res = await getMyUserInformation(server.url, accessTokenUser)
       const user = res.body
 
-      await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
+      await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.gif')
+    })
+
+    it('Should be able to update my avatar with a gif, and then a png', async function () {
+      for (const extension of [ '.png', '.gif' ]) {
+        const fixture = 'avatar' + extension
+
+        await updateMyAvatar({
+          url: server.url,
+          accessToken: accessTokenUser,
+          fixture
+        })
+
+        const res = await getMyUserInformation(server.url, accessTokenUser)
+        const user = res.body
+
+        await testImage(server.url, 'avatar-resized', user.account.avatar.path, extension)
+      }
     })
 
     it('Should be able to update my display name', async function () {
diff --git a/server/tests/fixtures/avatar-resized.gif b/server/tests/fixtures/avatar-resized.gif
new file mode 100644 (file)
index 0000000..81a8218
Binary files /dev/null and b/server/tests/fixtures/avatar-resized.gif differ
diff --git a/server/tests/fixtures/avatar.gif b/server/tests/fixtures/avatar.gif
new file mode 100644 (file)
index 0000000..f297077
Binary files /dev/null and b/server/tests/fixtures/avatar.gif differ