]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Optimize image resizing
authorChocobozzz <me@florianbigard.com>
Mon, 19 Nov 2018 10:24:31 +0000 (11:24 +0100)
committerChocobozzz <me@florianbigard.com>
Mon, 19 Nov 2018 13:34:36 +0000 (14:34 +0100)
client/src/app/+verify-account/verify-account-email/verify-account-email.component.ts
server/helpers/image-utils.ts
server/lib/activitypub/videos.ts

index 26b3bf4b1dcc473f4bfea1e47763e8f08c6eaf2d..e4a5522c8e8ad970e7f393725e03dccea4bcbbb1 100644 (file)
@@ -25,7 +25,6 @@ export class VerifyAccountEmailComponent implements OnInit {
   }
 
   ngOnInit () {
-
     this.userId = this.route.snapshot.queryParams['userId']
     this.verificationString = this.route.snapshot.queryParams['verificationString']
 
index 3eaa674ed98e4bf67fa0db0026045a36885a045f..da3285b13da7c3cfc902abfcfeab2f46564ab913 100644 (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)
 
index 80de92f244d6c1ceecf1d3067eaa80b2345fdc3e..6ff9baefe37bffaf6f94f99bff836a9c96d34ac6 100644 (file)
@@ -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 {