]>
Commit | Line | Data |
---|---|---|
ac81d1a0 C |
1 | import 'multer' |
2 | import * as sharp from 'sharp' | |
74577825 C |
3 | import { readFile, remove } from 'fs-extra' |
4 | import { logger } from './logger' | |
ac81d1a0 C |
5 | |
6 | async function processImage ( | |
2fb5b3a5 | 7 | path: string, |
ac81d1a0 | 8 | destination: string, |
e8bafea3 C |
9 | newSize: { width: number, height: number }, |
10 | keepOriginal = false | |
ac81d1a0 | 11 | ) { |
2fb5b3a5 | 12 | if (path === destination) { |
a8a63227 C |
13 | throw new Error('Sharp needs an input path different that the output path.') |
14 | } | |
15 | ||
2fb5b3a5 | 16 | logger.debug('Processing image %s to %s.', path, destination) |
a8a63227 | 17 | |
74577825 | 18 | // Avoid sharp cache |
2fb5b3a5 | 19 | const buf = await readFile(path) |
74577825 | 20 | const sharpInstance = sharp(buf) |
a8a63227 C |
21 | |
22 | await remove(destination) | |
23 | ||
24 | await sharpInstance | |
ac81d1a0 C |
25 | .resize(newSize.width, newSize.height) |
26 | .toFile(destination) | |
27 | ||
2fb5b3a5 | 28 | if (keepOriginal !== true) await remove(path) |
ac81d1a0 C |
29 | } |
30 | ||
31 | // --------------------------------------------------------------------------- | |
32 | ||
33 | export { | |
34 | processImage | |
35 | } |