PeerTube/server/helpers/image-utils.ts

37 lines
903 B
TypeScript
Raw Normal View History

import 'multer'
import * as sharp from 'sharp'
2018-11-19 04:24:31 -06:00
import { move, remove } from 'fs-extra'
async function processImage (
2018-02-15 11:40:24 -06:00
physicalFile: { path: string },
destination: string,
newSize: { width: number, height: number }
) {
2018-11-19 04:24:31 -06:00
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)
2018-08-27 09:23:34 -05:00
await remove(physicalFile.path)
}
// ---------------------------------------------------------------------------
export {
processImage
}