Fix images size when downloading them
authorChocobozzz <me@florianbigard.com>
Fri, 16 Nov 2018 15:48:17 +0000 (16:48 +0100)
committerChocobozzz <me@florianbigard.com>
Fri, 16 Nov 2018 15:48:17 +0000 (16:48 +0100)
server/helpers/requests.ts
server/lib/activitypub/actor.ts
server/lib/activitypub/videos.ts
server/lib/job-queue/handlers/video-import.ts
server/tests/api/redundancy/redundancy.ts

index 51facc9e03a7c44e63a34332115c3c500a6fb994..805930a9ff38f9ee415d03c616fe3d412efc6a90 100644 (file)
@@ -2,6 +2,7 @@ import * as Bluebird from 'bluebird'
 import { createWriteStream } from 'fs-extra'
 import * as request from 'request'
 import { ACTIVITY_PUB } from '../initializers'
+import { processImage } from './image-utils'
 
 function doRequest <T> (
   requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }
@@ -27,9 +28,18 @@ function doRequestAndSaveToFile (requestOptions: request.CoreOptions & request.U
   })
 }
 
+async function downloadImage (url: string, destPath: string, size: { width: number, height: number }) {
+  const tmpPath = destPath + '.tmp'
+
+  await doRequestAndSaveToFile({ method: 'GET', uri: url }, tmpPath)
+
+  await processImage({ path: tmpPath }, destPath, size)
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   doRequest,
-  doRequestAndSaveToFile
+  doRequestAndSaveToFile,
+  downloadImage
 }
index b16a00669df3fd3c7b09cc62276513135438a647..218dbc6a7f9df0cd9afbd366c483e82aaab85a7d 100644 (file)
@@ -11,9 +11,9 @@ import { isActivityPubUrlValid } from '../../helpers/custom-validators/activityp
 import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils'
 import { logger } from '../../helpers/logger'
 import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto'
-import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
+import { doRequest, doRequestAndSaveToFile, downloadImage } from '../../helpers/requests'
 import { getUrlFromWebfinger } from '../../helpers/webfinger'
-import { CONFIG, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../initializers'
+import { AVATARS_SIZE, CONFIG, IMAGE_MIMETYPE_EXT, PREVIEWS_SIZE, sequelizeTypescript } from '../../initializers'
 import { AccountModel } from '../../models/account/account'
 import { ActorModel } from '../../models/activitypub/actor'
 import { AvatarModel } from '../../models/avatar/avatar'
@@ -180,10 +180,7 @@ async function fetchAvatarIfExists (actorJSON: ActivityPubActor) {
     const avatarName = uuidv4() + extension
     const destPath = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
 
-    await doRequestAndSaveToFile({
-      method: 'GET',
-      uri: actorJSON.icon.url
-    }, destPath)
+    await downloadImage(actorJSON.icon.url, destPath, AVATARS_SIZE)
 
     return avatarName
   }
index 5bd03c8c6d154ac46402e23d5d295452ca3e34eb..80de92f244d6c1ceecf1d3067eaa80b2345fdc3e 100644 (file)
@@ -10,8 +10,8 @@ import { sanitizeAndCheckVideoTorrentObject } from '../../helpers/custom-validat
 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
 import { resetSequelizeInstance, retryTransactionWrapper } from '../../helpers/database-utils'
 import { logger } from '../../helpers/logger'
-import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
-import { ACTIVITY_PUB, CONFIG, REMOTE_SCHEME, sequelizeTypescript, VIDEO_MIMETYPE_EXT } from '../../initializers'
+import { doRequest, downloadImage } from '../../helpers/requests'
+import { ACTIVITY_PUB, CONFIG, REMOTE_SCHEME, sequelizeTypescript, THUMBNAILS_SIZE, VIDEO_MIMETYPE_EXT } from '../../initializers'
 import { ActorModel } from '../../models/activitypub/actor'
 import { TagModel } from '../../models/video/tag'
 import { VideoModel } from '../../models/video/video'
@@ -97,11 +97,7 @@ function generateThumbnailFromUrl (video: VideoModel, icon: ActivityIconObject)
   const thumbnailName = video.getThumbnailName()
   const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
 
-  const options = {
-    method: 'GET',
-    uri: icon.url
-  }
-  return doRequestAndSaveToFile(options, thumbnailPath)
+  return downloadImage(icon.url, thumbnailPath, THUMBNAILS_SIZE)
 }
 
 function getOrCreateVideoChannelFromVideoObject (videoObject: VideoTorrentObject) {
index e3f2a276cfbadd622a96215a007cbc9e52e76e93..4de901c0c1f55236e7ff1018955e3aeb5e532cdc 100644 (file)
@@ -6,8 +6,8 @@ import { VideoImportState } from '../../../../shared/models/videos'
 import { getDurationFromVideoFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
 import { extname, join } from 'path'
 import { VideoFileModel } from '../../../models/video/video-file'
-import { CONFIG, sequelizeTypescript, VIDEO_IMPORT_TIMEOUT } from '../../../initializers'
-import { doRequestAndSaveToFile } from '../../../helpers/requests'
+import { CONFIG, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE, VIDEO_IMPORT_TIMEOUT } from '../../../initializers'
+import { doRequestAndSaveToFile, downloadImage } from '../../../helpers/requests'
 import { VideoState } from '../../../../shared'
 import { JobQueue } from '../index'
 import { federateVideoIfNeeded } from '../../activitypub'
@@ -133,7 +133,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: Vide
       videoId: videoImport.videoId
     }
     videoFile = new VideoFileModel(videoFileData)
-    // Import if the import fails, to clean files
+    // To clean files if the import fails
     videoImport.Video.VideoFiles = [ videoFile ]
 
     // Move file
@@ -145,7 +145,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: Vide
     if (options.downloadThumbnail) {
       if (options.thumbnailUrl) {
         const destThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoImport.Video.getThumbnailName())
-        await doRequestAndSaveToFile({ method: 'GET', uri: options.thumbnailUrl }, destThumbnailPath)
+        await downloadImage(options.thumbnailUrl, destThumbnailPath, THUMBNAILS_SIZE)
       } else {
         await videoImport.Video.createThumbnail(videoFile)
       }
@@ -157,7 +157,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: Vide
     if (options.downloadPreview) {
       if (options.thumbnailUrl) {
         const destPreviewPath = join(CONFIG.STORAGE.PREVIEWS_DIR, videoImport.Video.getPreviewName())
-        await doRequestAndSaveToFile({ method: 'GET', uri: options.thumbnailUrl }, destPreviewPath)
+        await downloadImage(options.thumbnailUrl, destPreviewPath, PREVIEWS_SIZE)
       } else {
         await videoImport.Video.createPreview(videoFile)
       }
index 47f4e59fc19343d078a7773d778e1a55b1131300..a8a2f305fd5a91c2b2c3413f11e7838faa5c0521 100644 (file)
@@ -17,7 +17,7 @@ import {
   viewVideo,
   wait,
   waitUntilLog,
-  checkVideoFilesWereRemoved, removeVideo
+  checkVideoFilesWereRemoved, removeVideo, getVideoWithToken
 } from '../../utils'
 import { waitJobs } from '../../utils/server/jobs'
 import * as magnetUtil from 'magnet-uri'
@@ -93,7 +93,8 @@ async function check1WebSeed (strategy: VideoRedundancyStrategy, videoUUID?: str
 
   for (const server of servers) {
     {
-      const res = await getVideo(server.url, videoUUID)
+      // With token to avoid issues with video follow constraints
+      const res = await getVideoWithToken(server.url, server.accessToken, videoUUID)
 
       const video: VideoDetails = res.body
       for (const f of video.files) {