Fix thumbnail processing
authorChocobozzz <me@florianbigard.com>
Tue, 4 Dec 2018 14:12:54 +0000 (15:12 +0100)
committerChocobozzz <me@florianbigard.com>
Tue, 4 Dec 2018 15:04:14 +0000 (16:04 +0100)
server/helpers/image-utils.ts
server/helpers/requests.ts
server/lib/activitypub/process/process-update.ts
server/lib/activitypub/videos.ts
server/lib/job-queue/handlers/activitypub-refresher.ts
server/tests/cli/index.ts

index da3285b13da7c3cfc902abfcfeab2f46564ab913..e43ea3f1dad03c62cf65457e40fe461b22c98227 100644 (file)
@@ -1,6 +1,7 @@
 import 'multer'
 import * as sharp from 'sharp'
-import { move, remove } from 'fs-extra'
+import { readFile, remove } from 'fs-extra'
+import { logger } from './logger'
 
 async function processImage (
   physicalFile: { path: string },
@@ -11,14 +12,11 @@ async function processImage (
     throw new Error('Sharp needs an input path different that the output path.')
   }
 
-  const sharpInstance = sharp(physicalFile.path)
-  const metadata = await sharpInstance.metadata()
+  logger.debug('Processing image %s to %s.', physicalFile.path, destination)
 
-  // No need to resize
-  if (metadata.width === newSize.width && metadata.height === newSize.height) {
-    await move(physicalFile.path, destination, { overwrite: true })
-    return
-  }
+  // Avoid sharp cache
+  const buf = await readFile(physicalFile.path)
+  const sharpInstance = sharp(buf)
 
   await remove(destination)
 
index 805930a9ff38f9ee415d03c616fe3d412efc6a90..5760ad1c1854dc617f164b00aa68fbd8dd5a9fe4 100644 (file)
@@ -3,6 +3,7 @@ import { createWriteStream } from 'fs-extra'
 import * as request from 'request'
 import { ACTIVITY_PUB } from '../initializers'
 import { processImage } from './image-utils'
+import { extname } from 'path'
 
 function doRequest <T> (
   requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }
@@ -29,8 +30,7 @@ function doRequestAndSaveToFile (requestOptions: request.CoreOptions & request.U
 }
 
 async function downloadImage (url: string, destPath: string, size: { width: number, height: number }) {
-  const tmpPath = destPath + '.tmp'
-
+  const tmpPath = destPath + '.tmp' + extname(destPath)
   await doRequestAndSaveToFile({ method: 'GET', uri: url }, tmpPath)
 
   await processImage({ path: tmpPath }, destPath, size)
index 03831a00e108947e060dabd80ad24c6a72e5ffdc..c6b42d8465eae5de78c861675e33e183017e9fd5 100644 (file)
@@ -51,7 +51,7 @@ async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate)
     return undefined
   }
 
-  const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id })
+  const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false })
   const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
 
   const updateOptions = {
index 998f903303137aa17040d6bb6c5cfc860a5a819f..a5d649391f6cd684e942bb735894bfc1989051f6 100644 (file)
@@ -158,25 +158,30 @@ async function syncVideoExternalAttributes (video: VideoModel, fetchedVideo: Vid
 async function getOrCreateVideoAndAccountAndChannel (options: {
   videoObject: VideoTorrentObject | string,
   syncParam?: SyncParam,
-  fetchType?: VideoFetchByUrlType
+  fetchType?: VideoFetchByUrlType,
+  allowRefresh?: boolean // true by default
 }) {
   // Default params
   const syncParam = options.syncParam || { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true, refreshVideo: false }
   const fetchType = options.fetchType || 'all'
+  const allowRefresh = options.allowRefresh !== false
 
   // Get video url
   const videoUrl = getAPUrl(options.videoObject)
 
   let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType)
   if (videoFromDatabase) {
-    const refreshOptions = {
-      video: videoFromDatabase,
-      fetchedType: fetchType,
-      syncParam
-    }
 
-    if (syncParam.refreshVideo === true) videoFromDatabase = await refreshVideoIfNeeded(refreshOptions)
-    else await JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', videoUrl: videoFromDatabase.url } })
+    if (allowRefresh === true) {
+      const refreshOptions = {
+        video: videoFromDatabase,
+        fetchedType: fetchType,
+        syncParam
+      }
+
+      if (syncParam.refreshVideo === true) videoFromDatabase = await refreshVideoIfNeeded(refreshOptions)
+      else await JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', videoUrl: videoFromDatabase.url } })
+    }
 
     return { video: videoFromDatabase }
   }
index 7752b3b4002d962dd55cb51ef47f9cac48fb33e0..671b0f48743ebf35627541cd1735f6f45ea3bc8b 100644 (file)
@@ -10,7 +10,8 @@ export type RefreshPayload = {
 
 async function refreshAPObject (job: Bull.Job) {
   const payload = job.data as RefreshPayload
-  logger.info('Processing AP refresher in job %d.', job.id)
+
+  logger.info('Processing AP refresher in job %d for video %s.', job.id, payload.videoUrl)
 
   if (payload.type === 'video') return refreshAPVideo(payload.videoUrl)
 }
index 6201314ce65721ea76cb22e05a7b79e8b2a72b9f..c6b7ec0789dff08d2ea7753196d303925b580c86 100644 (file)
@@ -1,6 +1,7 @@
 // Order of the tests we want to execute
 import './create-import-video-file-job'
 import './create-transcoding-job'
+import './optimize-old-videos'
 import './peertube'
 import './reset-password'
 import './update-host'