From a8a63227781c6815532cb7a68699b08fdb0368be Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 19 Nov 2018 11:24:31 +0100 Subject: [PATCH] Optimize image resizing --- .../verify-account-email.component.ts | 1 - server/helpers/image-utils.ts | 19 +++++++++++++++++-- server/lib/activitypub/videos.ts | 10 ++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/client/src/app/+verify-account/verify-account-email/verify-account-email.component.ts b/client/src/app/+verify-account/verify-account-email/verify-account-email.component.ts index 26b3bf4b1..e4a5522c8 100644 --- a/client/src/app/+verify-account/verify-account-email/verify-account-email.component.ts +++ b/client/src/app/+verify-account/verify-account-email/verify-account-email.component.ts @@ -25,7 +25,6 @@ export class VerifyAccountEmailComponent implements OnInit { } ngOnInit () { - this.userId = this.route.snapshot.queryParams['userId'] this.verificationString = this.route.snapshot.queryParams['verificationString'] 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 @@ 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) diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index 80de92f24..6ff9baefe 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -242,10 +242,6 @@ async function updateVideoFromAP (options: { if (options.updateViews === true) options.video.set('views', videoData.views) await options.video.save(sequelizeOptions) - // Don't block on request - generateThumbnailFromUrl(options.video, options.videoObject.icon) - .catch(err => logger.warn('Cannot generate thumbnail of %s.', options.videoObject.id, { err })) - { const videoFileAttributes = videoFileActivityUrlToDBAttributes(options.video, options.videoObject) const newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a)) @@ -293,6 +289,12 @@ async function updateVideoFromAP (options: { logger.debug('Cannot update the remote video.', { err }) throw err } + + try { + await generateThumbnailFromUrl(options.video, options.videoObject.icon) + } catch (err) { + logger.warn('Cannot generate thumbnail of %s.', options.videoObject.id, { err }) + } } export { -- 2.41.0