]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/image-utils.ts
Update PR template and contributing guide
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
index 9285c12fc59aa383de2b4135b1933347ff67cf59..122fb009d1950b10f644e26d0e42463d255668eb 100644 (file)
@@ -1,16 +1,21 @@
 import { copy, readFile, remove, rename } from 'fs-extra'
 import * as Jimp from 'jimp'
-import { extname } from 'path'
+import { v4 as uuidv4 } from 'uuid'
+import { getLowercaseExtension } from './core-utils'
 import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
 import { logger } from './logger'
 
+function generateImageFilename (extension = '.jpg') {
+  return uuidv4() + extension
+}
+
 async function processImage (
   path: string,
   destination: string,
   newSize: { width: number, height: number },
   keepOriginal = false
 ) {
-  const extension = extname(path)
+  const extension = getLowercaseExtension(path)
 
   if (path === destination) {
     throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
@@ -31,6 +36,7 @@ async function processImage (
 // ---------------------------------------------------------------------------
 
 export {
+  generateImageFilename,
   processImage
 }
 
@@ -55,7 +61,8 @@ async function jimpProcessor (path: string, destination: string, newSize: { widt
   await remove(destination)
 
   // Optimization if the source file has the appropriate size
-  if (await skipProcessing({ jimpInstance, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt: extname(destination) })) {
+  const outputExt = getLowercaseExtension(destination)
+  if (skipProcessing({ jimpInstance, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) {
     return copy(path, destination)
   }