diff options
author | Chocobozzz <me@florianbigard.com> | 2018-11-19 11:24:31 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-11-19 14:34:36 +0100 |
commit | a8a63227781c6815532cb7a68699b08fdb0368be (patch) | |
tree | 6383e14156012b9366d0d1c2039262574fab2fc4 /server/helpers/image-utils.ts | |
parent | 9d0b856e930ee1c676d16a56408a3e4a18f8f978 (diff) | |
download | PeerTube-a8a63227781c6815532cb7a68699b08fdb0368be.tar.gz PeerTube-a8a63227781c6815532cb7a68699b08fdb0368be.tar.zst PeerTube-a8a63227781c6815532cb7a68699b08fdb0368be.zip |
Optimize image resizing
Diffstat (limited to 'server/helpers/image-utils.ts')
-rw-r--r-- | server/helpers/image-utils.ts | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts index 3eaa674ed..da3285b13 100644 --- a/server/helpers/image-utils.ts +++ b/server/helpers/image-utils.ts | |||
@@ -1,13 +1,28 @@ | |||
1 | import 'multer' | 1 | import 'multer' |
2 | import * as sharp from 'sharp' | 2 | import * as sharp from 'sharp' |
3 | import { remove } from 'fs-extra' | 3 | import { move, remove } from 'fs-extra' |
4 | 4 | ||
5 | async function processImage ( | 5 | async function processImage ( |
6 | physicalFile: { path: string }, | 6 | physicalFile: { path: string }, |
7 | destination: string, | 7 | destination: string, |
8 | newSize: { width: number, height: number } | 8 | newSize: { width: number, height: number } |
9 | ) { | 9 | ) { |
10 | await sharp(physicalFile.path) | 10 | if (physicalFile.path === destination) { |
11 | throw new Error('Sharp needs an input path different that the output path.') | ||
12 | } | ||
13 | |||
14 | const sharpInstance = sharp(physicalFile.path) | ||
15 | const metadata = await sharpInstance.metadata() | ||
16 | |||
17 | // No need to resize | ||
18 | if (metadata.width === newSize.width && metadata.height === newSize.height) { | ||
19 | await move(physicalFile.path, destination, { overwrite: true }) | ||
20 | return | ||
21 | } | ||
22 | |||
23 | await remove(destination) | ||
24 | |||
25 | await sharpInstance | ||
11 | .resize(newSize.width, newSize.height) | 26 | .resize(newSize.width, newSize.height) |
12 | .toFile(destination) | 27 | .toFile(destination) |
13 | 28 | ||