]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/image-utils.ts
Proxy youtube-dl format command too
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
... / ...
CommitLineData
1import 'multer'
2import * as sharp from 'sharp'
3import { readFile, remove } from 'fs-extra'
4import { logger } from './logger'
5
6async function processImage (
7 path: string,
8 destination: string,
9 newSize: { width: number, height: number },
10 keepOriginal = false
11) {
12 if (path === destination) {
13 throw new Error('Sharp needs an input path different that the output path.')
14 }
15
16 logger.debug('Processing image %s to %s.', path, destination)
17
18 // Avoid sharp cache
19 const buf = await readFile(path)
20 const sharpInstance = sharp(buf)
21
22 await remove(destination)
23
24 await sharpInstance
25 .resize(newSize.width, newSize.height)
26 .toFile(destination)
27
28 if (keepOriginal !== true) await remove(path)
29}
30
31// ---------------------------------------------------------------------------
32
33export {
34 processImage
35}