]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/video-file-import.ts
chore(refactor): remove shared folder dependencies to the server
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file-import.ts
index f9bc3137c4dff5b7cd9dffe8c8c437b34048e121..0d9e80cb86420f299469cd092120c3f6b08aaeee 100644 (file)
@@ -1,17 +1,20 @@
-import * as Bull from 'bull'
+import { Job } from 'bull'
+import { copy, stat } from 'fs-extra'
+import { getLowercaseExtension } from '@shared/core-utils'
+import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
+import { CONFIG } from '@server/initializers/config'
+import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
+import { generateWebTorrentVideoFilename } from '@server/lib/paths'
+import { addMoveToObjectStorageJob } from '@server/lib/video'
+import { VideoPathManager } from '@server/lib/video-path-manager'
+import { MVideoFullLight } from '@server/types/models'
+import { VideoFileImportPayload, VideoStorage } from '@shared/models'
+import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
 import { logger } from '../../../helpers/logger'
 import { VideoModel } from '../../../models/video/video'
-import { publishNewResolutionIfNeeded } from './video-transcoding'
-import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
-import { copy, stat } from 'fs-extra'
 import { VideoFileModel } from '../../../models/video/video-file'
-import { extname } from 'path'
-import { MVideoFile, MVideoWithFile } from '@server/types/models'
-import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
-import { getVideoFilePath } from '@server/lib/video-paths'
-import { VideoFileImportPayload } from '@shared/models'
 
-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)
 
@@ -24,7 +27,12 @@ async function processVideoFileImport (job: Bull.Job) {
 
   await updateVideoFile(video, payload.filePath)
 
-  await publishNewResolutionIfNeeded(video)
+  if (CONFIG.OBJECT_STORAGE.ENABLED) {
+    await addMoveToObjectStorageJob(video)
+  } else {
+    await federateVideoIfNeeded(video, false)
+  }
+
   return video
 }
 
@@ -36,42 +44,39 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) {
-  const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
+async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
+  const { resolution } = await getVideoFileResolution(inputFilePath)
   const { size } = await stat(inputFilePath)
   const fps = await getVideoFileFPS(inputFilePath)
 
-  let updatedVideoFile = new VideoFileModel({
-    resolution: videoFileResolution,
-    extname: extname(inputFilePath),
-    size,
-    fps,
-    videoId: video.id
-  }) as MVideoFile
+  const fileExt = getLowercaseExtension(inputFilePath)
 
-  const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
+  const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)
 
   if (currentVideoFile) {
     // Remove old file and old torrent
-    await video.removeFile(currentVideoFile)
-    await video.removeTorrent(currentVideoFile)
+    await video.removeWebTorrentFileAndTorrent(currentVideoFile)
     // Remove the old video file from the array
     video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
 
-    // Update the database
-    currentVideoFile.extname = updatedVideoFile.extname
-    currentVideoFile.size = updatedVideoFile.size
-    currentVideoFile.fps = updatedVideoFile.fps
-
-    updatedVideoFile = currentVideoFile
+    await currentVideoFile.destroy()
   }
 
-  const outputPath = getVideoFilePath(video, updatedVideoFile)
-  await copy(inputFilePath, outputPath)
+  const newVideoFile = new VideoFileModel({
+    resolution,
+    extname: fileExt,
+    filename: generateWebTorrentVideoFilename(resolution, fileExt),
+    storage: VideoStorage.FILE_SYSTEM,
+    size,
+    fps,
+    videoId: video.id
+  })
 
-  await createTorrentAndSetInfoHash(video, updatedVideoFile)
+  const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
+  await copy(inputFilePath, outputPath)
 
-  await updatedVideoFile.save()
+  video.VideoFiles.push(newVideoFile)
+  await createTorrentAndSetInfoHash(video, newVideoFile)
 
-  video.VideoFiles.push(updatedVideoFile)
+  await newVideoFile.save()
 }