]>
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 ( | |
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 | ||
32 | export { | |
33 | processImage | |
34 | } |