]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/video-import.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-import.ts
index 732773efd55d182b211ee67230c9f5c7313e4e0e..50e159245f0362f055eb045b0c4a0e5abbf5b1b8 100644 (file)
@@ -6,8 +6,7 @@ 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 { PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE, VIDEO_IMPORT_TIMEOUT } from '../../../initializers'
-import { downloadImage } from '../../../helpers/requests'
+import { VIDEO_IMPORT_TIMEOUT } from '../../../initializers/constants'
 import { VideoState } from '../../../../shared'
 import { JobQueue } from '../index'
 import { federateVideoIfNeeded } from '../../activitypub'
@@ -17,6 +16,10 @@ import { getSecureTorrentName } from '../../../helpers/utils'
 import { move, remove, stat } from 'fs-extra'
 import { Notifier } from '../../notifier'
 import { CONFIG } from '../../../initializers/config'
+import { sequelizeTypescript } from '../../../initializers/database'
+import { ThumbnailModel } from '../../../models/video/thumbnail'
+import { createVideoMiniatureFromUrl, generateVideoMiniature } from '../../thumbnail'
+import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type'
 
 type VideoImportYoutubeDLPayload = {
   type: 'youtube-dl'
@@ -145,25 +148,19 @@ async function processFile (downloader: () => Promise<string>, videoImport: Vide
     tempVideoPath = null // This path is not used anymore
 
     // Process thumbnail
-    if (options.downloadThumbnail) {
-      if (options.thumbnailUrl) {
-        await downloadImage(options.thumbnailUrl, CONFIG.STORAGE.THUMBNAILS_DIR, videoImport.Video.getThumbnailName(), THUMBNAILS_SIZE)
-      } else {
-        await videoImport.Video.createThumbnail(videoFile)
-      }
-    } else if (options.generateThumbnail) {
-      await videoImport.Video.createThumbnail(videoFile)
+    let thumbnailModel: ThumbnailModel
+    if (options.downloadThumbnail && options.thumbnailUrl) {
+      thumbnailModel = await createVideoMiniatureFromUrl(options.thumbnailUrl, videoImport.Video, ThumbnailType.MINIATURE)
+    } else if (options.generateThumbnail || options.downloadThumbnail) {
+      thumbnailModel = await generateVideoMiniature(videoImport.Video, videoFile, ThumbnailType.MINIATURE)
     }
 
     // Process preview
-    if (options.downloadPreview) {
-      if (options.thumbnailUrl) {
-        await downloadImage(options.thumbnailUrl, CONFIG.STORAGE.PREVIEWS_DIR, videoImport.Video.getPreviewName(), PREVIEWS_SIZE)
-      } else {
-        await videoImport.Video.createPreview(videoFile)
-      }
-    } else if (options.generatePreview) {
-      await videoImport.Video.createPreview(videoFile)
+    let previewModel: ThumbnailModel
+    if (options.downloadPreview && options.thumbnailUrl) {
+      previewModel = await createVideoMiniatureFromUrl(options.thumbnailUrl, videoImport.Video, ThumbnailType.PREVIEW)
+    } else if (options.generatePreview || options.downloadPreview) {
+      previewModel = await generateVideoMiniature(videoImport.Video, videoFile, ThumbnailType.PREVIEW)
     }
 
     // Create torrent
@@ -183,6 +180,9 @@ async function processFile (downloader: () => Promise<string>, videoImport: Vide
       video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED
       await video.save({ transaction: t })
 
+      if (thumbnailModel) await video.addAndSaveThumbnail(thumbnailModel, t)
+      if (previewModel) await video.addAndSaveThumbnail(previewModel, t)
+
       // Now we can federate the video (reload from database, we need more attributes)
       const videoForFederation = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
       await federateVideoIfNeeded(videoForFederation, true, t)
@@ -209,6 +209,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: Vide
     if (videoImportUpdated.Video.state === VideoState.TO_TRANSCODE) {
       // Put uuid because we don't have id auto incremented for now
       const dataInput = {
+        type: 'optimize' as 'optimize',
         videoUUID: videoImportUpdated.Video.uuid,
         isNewVideo: true
       }