]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/image-utils.ts
Merge branch 'develop' into pr/1217
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
CommitLineData
ac81d1a0
C
1import 'multer'
2import * as sharp from 'sharp'
74577825
C
3import { readFile, remove } from 'fs-extra'
4import { logger } from './logger'
ac81d1a0
C
5
6async function processImage (
02988fdc 7 physicalFile: { path: string },
ac81d1a0
C
8 destination: string,
9 newSize: { width: number, height: number }
10) {
a8a63227
C
11 if (physicalFile.path === destination) {
12 throw new Error('Sharp needs an input path different that the output path.')
13 }
14
74577825 15 logger.debug('Processing image %s to %s.', physicalFile.path, destination)
a8a63227 16
74577825
C
17 // Avoid sharp cache
18 const buf = await readFile(physicalFile.path)
19 const sharpInstance = sharp(buf)
a8a63227
C
20
21 await remove(destination)
22
23 await sharpInstance
ac81d1a0
C
24 .resize(newSize.width, newSize.height)
25 .toFile(destination)
26
62689b94 27 await remove(physicalFile.path)
ac81d1a0
C
28}
29
30// ---------------------------------------------------------------------------
31
32export {
33 processImage
34}