]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/video-import.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-import.ts
index 1e5e52b58f5106d3ba99d68f1b9db7afaba99a5a..4ce1a6c30d9b52d22dc0e1a6fdeaacee353cfe7a 100644 (file)
@@ -1,12 +1,16 @@
-import * as Bull from 'bull'
+import { Job } from 'bull'
 import { move, remove, stat } from 'fs-extra'
-import { extname } from 'path'
+import { getLowercaseExtension } from '@server/helpers/core-utils'
 import { retryTransactionWrapper } from '@server/helpers/database-utils'
+import { YoutubeDLWrapper } from '@server/helpers/youtube-dl'
 import { isPostImportVideoAccepted } from '@server/lib/moderation'
+import { generateWebTorrentVideoFilename } from '@server/lib/paths'
 import { Hooks } from '@server/lib/plugins/hooks'
+import { ServerConfigManager } from '@server/lib/server-config-manager'
 import { isAbleToUploadVideo } from '@server/lib/user'
-import { addOptimizeOrMergeAudioJob } from '@server/lib/video'
-import { getVideoFilePath } from '@server/lib/video-paths'
+import { addMoveToObjectStorageJob, addOptimizeOrMergeAudioJob } from '@server/lib/video'
+import { VideoPathManager } from '@server/lib/video-path-manager'
+import { buildNextVideoState } from '@server/lib/video-state'
 import { ThumbnailModel } from '@server/models/video/thumbnail'
 import { MVideoImportDefault, MVideoImportDefaultFiles, MVideoImportVideo } from '@server/types/models/video/video-import'
 import {
@@ -23,8 +27,6 @@ import { getDurationFromVideoFile, getVideoFileFPS, getVideoFileResolution } fro
 import { logger } from '../../../helpers/logger'
 import { getSecureTorrentName } from '../../../helpers/utils'
 import { createTorrentAndSetInfoHash, downloadWebTorrentVideo } from '../../../helpers/webtorrent'
-import { downloadYoutubeDLVideo } from '../../../helpers/youtube-dl'
-import { CONFIG } from '../../../initializers/config'
 import { VIDEO_IMPORT_TIMEOUT } from '../../../initializers/constants'
 import { sequelizeTypescript } from '../../../initializers/database'
 import { VideoModel } from '../../../models/video/video'
@@ -35,7 +37,7 @@ import { federateVideoIfNeeded } from '../../activitypub/videos'
 import { Notifier } from '../../notifier'
 import { generateVideoMiniature } from '../../thumbnail'
 
-async function processVideoImport (job: Bull.Job) {
+async function processVideoImport (job: Job) {
   const payload = job.data as VideoImportPayload
 
   if (payload.type === 'youtube-dl') return processYoutubeDLImport(job, payload)
@@ -50,7 +52,7 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function processTorrentImport (job: Bull.Job, payload: VideoImportTorrentPayload) {
+async function processTorrentImport (job: Job, payload: VideoImportTorrentPayload) {
   logger.info('Processing torrent video import in job %d.', job.id)
 
   const videoImport = await getVideoImportOrDie(payload.videoImportId)
@@ -61,12 +63,12 @@ async function processTorrentImport (job: Bull.Job, payload: VideoImportTorrentP
   }
   const target = {
     torrentName: videoImport.torrentName ? getSecureTorrentName(videoImport.torrentName) : undefined,
-    magnetUri: videoImport.magnetUri
+    uri: videoImport.magnetUri
   }
   return processFile(() => downloadWebTorrentVideo(target, VIDEO_IMPORT_TIMEOUT), videoImport, options)
 }
 
-async function processYoutubeDLImport (job: Bull.Job, payload: VideoImportYoutubeDLPayload) {
+async function processYoutubeDLImport (job: Job, payload: VideoImportYoutubeDLPayload) {
   logger.info('Processing youtubeDL video import in job %d.', job.id)
 
   const videoImport = await getVideoImportOrDie(payload.videoImportId)
@@ -75,8 +77,10 @@ async function processYoutubeDLImport (job: Bull.Job, payload: VideoImportYoutub
     videoImportId: videoImport.id
   }
 
+  const youtubeDL = new YoutubeDLWrapper(videoImport.targetUrl, ServerConfigManager.Instance.getEnabledResolutions('vod'))
+
   return processFile(
-    () => downloadYoutubeDLVideo(videoImport.targetUrl, payload.fileExt, VIDEO_IMPORT_TIMEOUT),
+    () => youtubeDL.downloadVideo(payload.fileExt, VIDEO_IMPORT_TIMEOUT),
     videoImport,
     options
   )
@@ -97,7 +101,6 @@ type ProcessFileOptions = {
 }
 async function processFile (downloader: () => Promise<string>, videoImport: MVideoImportDefault, options: ProcessFileOptions) {
   let tempVideoPath: string
-  let videoDestFile: string
   let videoFile: VideoFileModel
 
   try {
@@ -111,15 +114,17 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
       throw new Error('The user video quota is exceeded with this video to import.')
     }
 
-    const { videoFileResolution } = await getVideoFileResolution(tempVideoPath)
+    const { resolution } = await getVideoFileResolution(tempVideoPath)
     const fps = await getVideoFileFPS(tempVideoPath)
     const duration = await getDurationFromVideoFile(tempVideoPath)
 
     // Prepare video file object for creation in database
+    const fileExt = getLowercaseExtension(tempVideoPath)
     const videoFileData = {
-      extname: extname(tempVideoPath),
-      resolution: videoFileResolution,
+      extname: fileExt,
+      resolution,
       size: stats.size,
+      filename: generateWebTorrentVideoFilename(resolution, fileExt),
       fps,
       videoId: videoImport.videoId
     }
@@ -154,7 +159,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
     const videoImportWithFiles: MVideoImportDefaultFiles = Object.assign(videoImport, { Video: videoWithFiles })
 
     // Move file
-    videoDestFile = getVideoFilePath(videoImportWithFiles.Video, videoFile)
+    const videoDestFile = VideoPathManager.Instance.getFSVideoFileOutputPath(videoImportWithFiles.Video, videoFile)
     await move(tempVideoPath, videoDestFile)
     tempVideoPath = null // This path is not used anymore
 
@@ -162,7 +167,11 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
     let thumbnailModel: MThumbnail
     let thumbnailSave: object
     if (!videoImportWithFiles.Video.getMiniature()) {
-      thumbnailModel = await generateVideoMiniature(videoImportWithFiles.Video, videoFile, ThumbnailType.MINIATURE)
+      thumbnailModel = await generateVideoMiniature({
+        video: videoImportWithFiles.Video,
+        videoFile,
+        type: ThumbnailType.MINIATURE
+      })
       thumbnailSave = thumbnailModel.toJSON()
     }
 
@@ -170,7 +179,11 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
     let previewModel: MThumbnail
     let previewSave: object
     if (!videoImportWithFiles.Video.getPreview()) {
-      previewModel = await generateVideoMiniature(videoImportWithFiles.Video, videoFile, ThumbnailType.PREVIEW)
+      previewModel = await generateVideoMiniature({
+        video: videoImportWithFiles.Video,
+        videoFile,
+        type: ThumbnailType.PREVIEW
+      })
       previewSave = previewModel.toJSON()
     }
 
@@ -191,7 +204,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
 
         // Update video DB object
         video.duration = duration
-        video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED
+        video.state = buildNextVideoState(video.state)
         await video.save({ transaction: t })
 
         if (thumbnailModel) await video.addAndSaveThumbnail(thumbnailModel, t)
@@ -222,7 +235,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
       })
     })
 
-    Notifier.Instance.notifyOnFinishedVideoImport(videoImportUpdated, true)
+    Notifier.Instance.notifyOnFinishedVideoImport({ videoImport: videoImportUpdated, success: true })
 
     if (video.isBlacklisted()) {
       const videoBlacklist = Object.assign(video.VideoBlacklist, { Video: video })
@@ -232,6 +245,10 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
       Notifier.Instance.notifyOnNewVideoIfNeeded(video)
     }
 
+    if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
+      return addMoveToObjectStorageJob(videoImportUpdated.Video)
+    }
+
     // Create transcoding jobs?
     if (video.state === VideoState.TO_TRANSCODE) {
       await addOptimizeOrMergeAudioJob(videoImportUpdated.Video, videoFile, videoImport.User)
@@ -250,7 +267,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
     }
     await videoImport.save()
 
-    Notifier.Instance.notifyOnFinishedVideoImport(videoImport, false)
+    Notifier.Instance.notifyOnFinishedVideoImport({ videoImport, success: false })
 
     throw err
   }