Add tests to gif resizer
This commit is contained in:
parent
6b67897e2e
commit
f619de0e43
|
@ -64,35 +64,14 @@ function convertWebPToJPG (path: string, destination: string): Promise<void> {
|
||||||
function processGIF (
|
function processGIF (
|
||||||
path: string,
|
path: string,
|
||||||
destination: string,
|
destination: string,
|
||||||
newSize: { width: number, height: number },
|
newSize: { width: number, height: number }
|
||||||
keepOriginal = false
|
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
return new Promise<void>(async (res, rej) => {
|
const command = ffmpeg(path)
|
||||||
if (path === destination) {
|
.fps(20)
|
||||||
throw new Error('FFmpeg needs an input path different that the output path.')
|
.size(`${newSize.width}x${newSize.height}`)
|
||||||
}
|
.output(destination)
|
||||||
|
|
||||||
logger.debug('Processing gif %s to %s.', path, destination)
|
return runCommand(command)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
|
async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { extname } from 'path'
|
|
||||||
import { remove, rename } from 'fs-extra'
|
import { remove, rename } from 'fs-extra'
|
||||||
|
import { extname } from 'path'
|
||||||
import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
|
import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
|
||||||
import { logger } from './logger'
|
import { logger } from './logger'
|
||||||
|
|
||||||
|
@ -13,17 +13,31 @@ async function processImage (
|
||||||
) {
|
) {
|
||||||
const extension = extname(path)
|
const extension = extname(path)
|
||||||
|
|
||||||
// Use FFmpeg to process GIF
|
|
||||||
if (extension === '.gif') {
|
|
||||||
return processGIF(path, destination, newSize, keepOriginal)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path === destination) {
|
if (path === destination) {
|
||||||
throw new Error('Jimp needs an input path different that the output path.')
|
throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('Processing image %s to %s.', path, destination)
|
logger.debug('Processing image %s to %s.', path, destination)
|
||||||
|
|
||||||
|
// Use FFmpeg to process GIF
|
||||||
|
if (extension === '.gif') {
|
||||||
|
await processGIF(path, destination, newSize)
|
||||||
|
} else {
|
||||||
|
await jimpProcessor(path, destination, newSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (keepOriginal !== true) await remove(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
export {
|
||||||
|
processImage
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }) {
|
||||||
let jimpInstance: any
|
let jimpInstance: any
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -44,12 +58,4 @@ async function processImage (
|
||||||
.resize(newSize.width, newSize.height)
|
.resize(newSize.width, newSize.height)
|
||||||
.quality(80)
|
.quality(80)
|
||||||
.writeAsync(destination)
|
.writeAsync(destination)
|
||||||
|
|
||||||
if (keepOriginal !== true) await remove(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
export {
|
|
||||||
processImage
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -602,8 +602,8 @@ describe('Test users', function () {
|
||||||
expect(user.account.description).to.be.null
|
expect(user.account.description).to.be.null
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should be able to update my avatar', async function () {
|
it('Should be able to update my avatar with a gif', async function () {
|
||||||
const fixture = 'avatar.png'
|
const fixture = 'avatar.gif'
|
||||||
|
|
||||||
await updateMyAvatar({
|
await updateMyAvatar({
|
||||||
url: server.url,
|
url: server.url,
|
||||||
|
@ -614,7 +614,24 @@ describe('Test users', function () {
|
||||||
const res = await getMyUserInformation(server.url, accessTokenUser)
|
const res = await getMyUserInformation(server.url, accessTokenUser)
|
||||||
const user = res.body
|
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 () {
|
it('Should be able to update my display name', async function () {
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 86 KiB |
Binary file not shown.
After Width: | Height: | Size: 46 KiB |
Loading…
Reference in New Issue