aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/image-utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/image-utils.ts')
-rw-r--r--server/helpers/image-utils.ts50
1 files changed, 44 insertions, 6 deletions
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts
index b174ae436..9d0c09051 100644
--- a/server/helpers/image-utils.ts
+++ b/server/helpers/image-utils.ts
@@ -1,9 +1,12 @@
1import { copy, readFile, remove, rename } from 'fs-extra' 1import { copy, readFile, remove, rename } from 'fs-extra'
2import Jimp, { read } from 'jimp' 2import Jimp, { read as jimpRead } from 'jimp'
3import { join } from 'path'
3import { getLowercaseExtension } from '@shared/core-utils' 4import { getLowercaseExtension } from '@shared/core-utils'
4import { buildUUID } from '@shared/extra-utils' 5import { buildUUID } from '@shared/extra-utils'
5import { convertWebPToJPG, processGIF } from './ffmpeg-utils' 6import { convertWebPToJPG, generateThumbnailFromVideo, processGIF } from './ffmpeg/ffmpeg-images'
6import { logger } from './logger' 7import { logger, loggerTagsFactory } from './logger'
8
9const lTags = loggerTagsFactory('image-utils')
7 10
8function generateImageFilename (extension = '.jpg') { 11function generateImageFilename (extension = '.jpg') {
9 return buildUUID() + extension 12 return buildUUID() + extension
@@ -33,11 +36,46 @@ async function processImage (
33 if (keepOriginal !== true) await remove(path) 36 if (keepOriginal !== true) await remove(path)
34} 37}
35 38
39async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
40 const pendingImageName = 'pending-' + imageName
41 const pendingImagePath = join(folder, pendingImageName)
42
43 try {
44 await generateThumbnailFromVideo(fromPath, folder, imageName)
45
46 const destination = join(folder, imageName)
47 await processImage(pendingImagePath, destination, size)
48 } catch (err) {
49 logger.error('Cannot generate image from video %s.', fromPath, { err, ...lTags() })
50
51 try {
52 await remove(pendingImagePath)
53 } catch (err) {
54 logger.debug('Cannot remove pending image path after generation error.', { err, ...lTags() })
55 }
56 }
57}
58
59async function getImageSize (path: string) {
60 const inputBuffer = await readFile(path)
61
62 const image = await jimpRead(inputBuffer)
63
64 return {
65 width: image.getWidth(),
66 height: image.getHeight()
67 }
68}
69
36// --------------------------------------------------------------------------- 70// ---------------------------------------------------------------------------
37 71
38export { 72export {
39 generateImageFilename, 73 generateImageFilename,
40 processImage 74 generateImageFromVideoFile,
75
76 processImage,
77
78 getImageSize
41} 79}
42 80
43// --------------------------------------------------------------------------- 81// ---------------------------------------------------------------------------
@@ -47,7 +85,7 @@ async function jimpProcessor (path: string, destination: string, newSize: { widt
47 const inputBuffer = await readFile(path) 85 const inputBuffer = await readFile(path)
48 86
49 try { 87 try {
50 sourceImage = await read(inputBuffer) 88 sourceImage = await jimpRead(inputBuffer)
51 } catch (err) { 89 } catch (err) {
52 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err }) 90 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
53 91
@@ -55,7 +93,7 @@ async function jimpProcessor (path: string, destination: string, newSize: { widt
55 await convertWebPToJPG(path, newName) 93 await convertWebPToJPG(path, newName)
56 await rename(newName, path) 94 await rename(newName, path)
57 95
58 sourceImage = await read(path) 96 sourceImage = await jimpRead(path)
59 } 97 }
60 98
61 await remove(destination) 99 await remove(destination)