]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/video-file-import.ts
Remove deprecated transcoding job names
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file-import.ts
index 582efea3a87df2a30fc9ac660c544d0227fbe022..4d199f24739bad97cf328020517fc8713edb2f9e 100644 (file)
@@ -1,10 +1,10 @@
 import * as Bull from 'bull'
 import { copy, stat } from 'fs-extra'
-import { extname } from 'path'
+import { getLowercaseExtension } from '@server/helpers/core-utils'
 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
-import { getVideoFilePath } from '@server/lib/video-paths'
-import { UserModel } from '@server/models/account/user'
-import { MVideoFile, MVideoWithFile } from '@server/types/models'
+import { generateWebTorrentVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
+import { UserModel } from '@server/models/user/user'
+import { MVideoFullLight } from '@server/types/models'
 import { VideoFileImportPayload } from '@shared/models'
 import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
 import { logger } from '../../../helpers/logger'
@@ -23,10 +23,21 @@ async function processVideoFileImport (job: Bull.Job) {
     return undefined
   }
 
+  const data = await getVideoFileResolution(payload.filePath)
+
   await updateVideoFile(video, payload.filePath)
 
   const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
-  await onNewWebTorrentFileResolution(video, user)
+
+  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
+  }
+  await onNewWebTorrentFileResolution(video, user, newResolutionPayload)
 
   return video
 }
@@ -39,42 +50,38 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) {
+async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
   const { videoFileResolution } = 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 === videoFileResolution)
 
   if (currentVideoFile) {
     // Remove old file and old torrent
-    await video.removeFile(currentVideoFile)
-    await video.removeTorrent(currentVideoFile)
+    await video.removeFileAndTorrent(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: videoFileResolution,
+    extname: fileExt,
+    filename: generateWebTorrentVideoFilename(videoFileResolution, fileExt),
+    size,
+    fps,
+    videoId: video.id
+  })
 
-  await createTorrentAndSetInfoHash(video, updatedVideoFile)
+  const outputPath = getVideoFilePath(video, newVideoFile)
+  await copy(inputFilePath, outputPath)
 
-  await updatedVideoFile.save()
+  video.VideoFiles.push(newVideoFile)
+  await createTorrentAndSetInfoHash(video, newVideoFile)
 
-  video.VideoFiles.push(updatedVideoFile)
+  await newVideoFile.save()
 }