1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 02:09:37 +02:00

Optimize image resizing

This commit is contained in:
Chocobozzz 2018-11-19 11:24:31 +01:00
parent 9d0b856e93
commit a8a6322778
No known key found for this signature in database
GPG key ID: 583A612D890159BE
3 changed files with 23 additions and 7 deletions

View file

@ -1,13 +1,28 @@
import 'multer'
import * as sharp from 'sharp'
import { remove } from 'fs-extra'
import { move, remove } from 'fs-extra'
async function processImage (
physicalFile: { path: string },
destination: string,
newSize: { width: number, height: number }
) {
await sharp(physicalFile.path)
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)