]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/video-file-import.ts
Use bullmq job dependency
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file-import.ts
index 4d199f24739bad97cf328020517fc8713edb2f9e..d950f64072e20db266a2989641322358db7e9bef 100644 (file)
@@ -1,43 +1,38 @@
-import * as Bull from 'bull'
+import { Job } from 'bullmq'
 import { copy, stat } from 'fs-extra'
-import { getLowercaseExtension } from '@server/helpers/core-utils'
 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
-import { generateWebTorrentVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
-import { UserModel } from '@server/models/user/user'
+import { CONFIG } from '@server/initializers/config'
+import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
+import { generateWebTorrentVideoFilename } from '@server/lib/paths'
+import { buildMoveToObjectStorageJob } from '@server/lib/video'
+import { VideoPathManager } from '@server/lib/video-path-manager'
+import { VideoModel } from '@server/models/video/video'
+import { VideoFileModel } from '@server/models/video/video-file'
 import { MVideoFullLight } from '@server/types/models'
-import { VideoFileImportPayload } from '@shared/models'
-import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
+import { getLowercaseExtension } from '@shared/core-utils'
+import { VideoFileImportPayload, VideoStorage } from '@shared/models'
+import { getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg'
 import { logger } from '../../../helpers/logger'
-import { VideoModel } from '../../../models/video/video'
-import { VideoFileModel } from '../../../models/video/video-file'
-import { onNewWebTorrentFileResolution } from './video-transcoding'
+import { JobQueue } from '../job-queue'
 
-async function processVideoFileImport (job: Bull.Job) {
+async function processVideoFileImport (job: Job) {
   const payload = job.data as VideoFileImportPayload
-  logger.info('Processing video file import in job %d.', job.id)
+  logger.info('Processing video file import in job %s.', job.id)
 
-  const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
+  const video = await VideoModel.loadFull(payload.videoUUID)
   // No video, maybe deleted?
   if (!video) {
     logger.info('Do not process job %d, video does not exist.', job.id)
     return undefined
   }
 
-  const data = await getVideoFileResolution(payload.filePath)
-
   await updateVideoFile(video, payload.filePath)
 
-  const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
-
-  const newResolutionPayload = {
-    type: 'new-resolution-to-webtorrent' as 'new-resolution-to-webtorrent',
-    videoUUID: video.uuid,
-    resolution: data.videoFileResolution,
-    isPortraitMode: data.isPortraitMode,
-    copyCodecs: false,
-    isNewVideo: false
+  if (CONFIG.OBJECT_STORAGE.ENABLED) {
+    await JobQueue.Instance.createJob(await buildMoveToObjectStorageJob({ video, previousVideoState: video.state }))
+  } else {
+    await federateVideoIfNeeded(video, false)
   }
-  await onNewWebTorrentFileResolution(video, user, newResolutionPayload)
 
   return video
 }
@@ -51,17 +46,17 @@ export {
 // ---------------------------------------------------------------------------
 
 async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
-  const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
+  const { resolution } = await getVideoStreamDimensionsInfo(inputFilePath)
   const { size } = await stat(inputFilePath)
-  const fps = await getVideoFileFPS(inputFilePath)
+  const fps = await getVideoStreamFPS(inputFilePath)
 
   const fileExt = getLowercaseExtension(inputFilePath)
 
-  const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution)
+  const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)
 
   if (currentVideoFile) {
     // Remove old file and old torrent
-    await video.removeFileAndTorrent(currentVideoFile)
+    await video.removeWebTorrentFile(currentVideoFile)
     // Remove the old video file from the array
     video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
 
@@ -69,15 +64,16 @@ async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
   }
 
   const newVideoFile = new VideoFileModel({
-    resolution: videoFileResolution,
+    resolution,
     extname: fileExt,
-    filename: generateWebTorrentVideoFilename(videoFileResolution, fileExt),
+    filename: generateWebTorrentVideoFilename(resolution, fileExt),
+    storage: VideoStorage.FILE_SYSTEM,
     size,
     fps,
     videoId: video.id
   })
 
-  const outputPath = getVideoFilePath(video, newVideoFile)
+  const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
   await copy(inputFilePath, outputPath)
 
   video.VideoFiles.push(newVideoFile)