X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fimage-utils.ts;h=bd81aa3bad774d134df064c70c899cafd4a7b9f6;hb=93905586ee198d4ac0a0fd5141a9fcbb10a94652;hp=3eaa674ed98e4bf67fa0db0026045a36885a045f;hpb=62689b942b71cd1dd0d050c6ed05f884a0b325c2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts index 3eaa674ed..bd81aa3ba 100644 --- a/server/helpers/image-utils.ts +++ b/server/helpers/image-utils.ts @@ -1,17 +1,31 @@ import 'multer' import * as sharp from 'sharp' -import { remove } from 'fs-extra' +import { readFile, remove } from 'fs-extra' +import { logger } from './logger' async function processImage ( - physicalFile: { path: string }, + path: string, destination: string, - newSize: { width: number, height: number } + newSize: { width: number, height: number }, + keepOriginal = false ) { - await sharp(physicalFile.path) + if (path === destination) { + throw new Error('Sharp needs an input path different that the output path.') + } + + logger.debug('Processing image %s to %s.', path, destination) + + // Avoid sharp cache + const buf = await readFile(path) + const sharpInstance = sharp(buf) + + await remove(destination) + + await sharpInstance .resize(newSize.width, newSize.height) .toFile(destination) - await remove(physicalFile.path) + if (keepOriginal !== true) await remove(path) } // ---------------------------------------------------------------------------