X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fimage-utils.ts;h=da3285b13da7c3cfc902abfcfeab2f46564ab913;hb=a8a63227781c6815532cb7a68699b08fdb0368be;hp=3eaa674ed98e4bf67fa0db0026045a36885a045f;hpb=9d0b856e930ee1c676d16a56408a3e4a18f8f978;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts index 3eaa674ed..da3285b13 100644 --- a/server/helpers/image-utils.ts +++ b/server/helpers/image-utils.ts @@ -1,13 +1,28 @@ import 'multer' import * as sharp from 'sharp' -import { remove } from 'fs-extra' +import { move, remove } from 'fs-extra' async function processImage ( physicalFile: { path: string }, destination: string, newSize: { width: number, height: number } ) { - await sharp(physicalFile.path) + if (physicalFile.path === destination) { + throw new Error('Sharp needs an input path different that the output path.') + } + + const sharpInstance = sharp(physicalFile.path) + const metadata = await sharpInstance.metadata() + + // No need to resize + if (metadata.width === newSize.width && metadata.height === newSize.height) { + await move(physicalFile.path, destination, { overwrite: true }) + return + } + + await remove(destination) + + await sharpInstance .resize(newSize.width, newSize.height) .toFile(destination)