aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-11-25 09:50:12 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-11-25 10:07:51 +0100
commitf619de0e435f7ac3abad2ec772397486358b56e7 (patch)
tree991040ca204e95a3322e02fdc9867a8f6b69c1e8
parent6b67897e2eab96978daee40aeaf716835856d65d (diff)
downloadPeerTube-f619de0e435f7ac3abad2ec772397486358b56e7.tar.gz
PeerTube-f619de0e435f7ac3abad2ec772397486358b56e7.tar.zst
PeerTube-f619de0e435f7ac3abad2ec772397486358b56e7.zip
Add tests to gif resizer
-rw-r--r--server/helpers/ffmpeg-utils.ts33
-rw-r--r--server/helpers/image-utils.ts34
-rw-r--r--server/tests/api/users/users.ts23
-rw-r--r--server/tests/fixtures/avatar-resized.gifbin0 -> 88318 bytes
-rw-r--r--server/tests/fixtures/avatar.gifbin0 -> 46917 bytes
5 files changed, 46 insertions, 44 deletions
diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts
index 69defccc4..9755dd67c 100644
--- a/server/helpers/ffmpeg-utils.ts
+++ b/server/helpers/ffmpeg-utils.ts
@@ -64,35 +64,14 @@ function convertWebPToJPG (path: string, destination: string): Promise<void> {
64function processGIF ( 64function processGIF (
65 path: string, 65 path: string,
66 destination: string, 66 destination: string,
67 newSize: { width: number, height: number }, 67 newSize: { width: number, height: number }
68 keepOriginal = false
69): Promise<void> { 68): Promise<void> {
70 return new Promise<void>(async (res, rej) => { 69 const command = ffmpeg(path)
71 if (path === destination) { 70 .fps(20)
72 throw new Error('FFmpeg needs an input path different that the output path.') 71 .size(`${newSize.width}x${newSize.height}`)
73 } 72 .output(destination)
74
75 logger.debug('Processing gif %s to %s.', path, destination)
76 73
77 try { 74 return runCommand(command)
78 const command = ffmpeg(path)
79 .fps(20)
80 .size(`${newSize.width}x${newSize.height}`)
81 .output(destination)
82
83 command.on('error', (err, stdout, stderr) => {
84 logger.error('Error in ffmpeg gif resizing process.', { stdout, stderr })
85 return rej(err)
86 })
87 .on('end', async () => {
88 if (keepOriginal !== true) await remove(path)
89 res()
90 })
91 .run()
92 } catch (err) {
93 return rej(err)
94 }
95 })
96} 75}
97 76
98async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { 77async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts
index fdf06e848..3ebf07305 100644
--- a/server/helpers/image-utils.ts
+++ b/server/helpers/image-utils.ts
@@ -1,5 +1,5 @@
1import { extname } from 'path'
2import { remove, rename } from 'fs-extra' 1import { remove, rename } from 'fs-extra'
2import { extname } from 'path'
3import { convertWebPToJPG, processGIF } from './ffmpeg-utils' 3import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
4import { logger } from './logger' 4import { logger } from './logger'
5 5
@@ -13,17 +13,31 @@ async function processImage (
13) { 13) {
14 const extension = extname(path) 14 const extension = extname(path)
15 15
16 if (path === destination) {
17 throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
18 }
19
20 logger.debug('Processing image %s to %s.', path, destination)
21
16 // Use FFmpeg to process GIF 22 // Use FFmpeg to process GIF
17 if (extension === '.gif') { 23 if (extension === '.gif') {
18 return processGIF(path, destination, newSize, keepOriginal) 24 await processGIF(path, destination, newSize)
25 } else {
26 await jimpProcessor(path, destination, newSize)
19 } 27 }
20 28
21 if (path === destination) { 29 if (keepOriginal !== true) await remove(path)
22 throw new Error('Jimp needs an input path different that the output path.') 30}
23 }
24 31
25 logger.debug('Processing image %s to %s.', path, destination) 32// ---------------------------------------------------------------------------
26 33
34export {
35 processImage
36}
37
38// ---------------------------------------------------------------------------
39
40async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }) {
27 let jimpInstance: any 41 let jimpInstance: any
28 42
29 try { 43 try {
@@ -44,12 +58,4 @@ async function processImage (
44 .resize(newSize.width, newSize.height) 58 .resize(newSize.width, newSize.height)
45 .quality(80) 59 .quality(80)
46 .writeAsync(destination) 60 .writeAsync(destination)
47
48 if (keepOriginal !== true) await remove(path)
49}
50
51// ---------------------------------------------------------------------------
52
53export {
54 processImage
55} 61}
diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts
index edb0b4bb3..fe83ca041 100644
--- a/server/tests/api/users/users.ts
+++ b/server/tests/api/users/users.ts
@@ -602,8 +602,8 @@ describe('Test users', function () {
602 expect(user.account.description).to.be.null 602 expect(user.account.description).to.be.null
603 }) 603 })
604 604
605 it('Should be able to update my avatar', async function () { 605 it('Should be able to update my avatar with a gif', async function () {
606 const fixture = 'avatar.png' 606 const fixture = 'avatar.gif'
607 607
608 await updateMyAvatar({ 608 await updateMyAvatar({
609 url: server.url, 609 url: server.url,
@@ -614,7 +614,24 @@ describe('Test users', function () {
614 const res = await getMyUserInformation(server.url, accessTokenUser) 614 const res = await getMyUserInformation(server.url, accessTokenUser)
615 const user = res.body 615 const user = res.body
616 616
617 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png') 617 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.gif')
618 })
619
620 it('Should be able to update my avatar with a gif, and then a png', async function () {
621 for (const extension of [ '.png', '.gif' ]) {
622 const fixture = 'avatar' + extension
623
624 await updateMyAvatar({
625 url: server.url,
626 accessToken: accessTokenUser,
627 fixture
628 })
629
630 const res = await getMyUserInformation(server.url, accessTokenUser)
631 const user = res.body
632
633 await testImage(server.url, 'avatar-resized', user.account.avatar.path, extension)
634 }
618 }) 635 })
619 636
620 it('Should be able to update my display name', async function () { 637 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
index 000000000..81a82189e
--- /dev/null
+++ b/server/tests/fixtures/avatar-resized.gif
Binary files differ
diff --git a/server/tests/fixtures/avatar.gif b/server/tests/fixtures/avatar.gif
new file mode 100644
index 000000000..f29707760
--- /dev/null
+++ b/server/tests/fixtures/avatar.gif
Binary files differ