1 import * as Bull from 'bull'
2 import { copy, stat } from 'fs-extra'
3 import { extname } from 'path'
4 import { getLowercaseExtension } from '@server/helpers/core-utils'
5 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
6 import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
7 import { UserModel } from '@server/models/user/user'
8 import { MVideoFullLight } from '@server/types/models'
9 import { VideoFileImportPayload } from '@shared/models'
10 import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
11 import { logger } from '../../../helpers/logger'
12 import { VideoModel } from '../../../models/video/video'
13 import { VideoFileModel } from '../../../models/video/video-file'
14 import { onNewWebTorrentFileResolution } from './video-transcoding'
16 async function processVideoFileImport (job: Bull.Job) {
17 const payload = job.data as VideoFileImportPayload
18 logger.info('Processing video file import in job %d.', job.id)
20 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
21 // No video, maybe deleted?
23 logger.info('Do not process job %d, video does not exist.', job.id)
27 const data = await getVideoFileResolution(payload.filePath)
29 await updateVideoFile(video, payload.filePath)
31 const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
33 const newResolutionPayload = {
34 type: 'new-resolution-to-webtorrent' as 'new-resolution-to-webtorrent',
35 videoUUID: video.uuid,
36 resolution: data.videoFileResolution,
37 isPortraitMode: data.isPortraitMode,
41 await onNewWebTorrentFileResolution(video, user, newResolutionPayload)
46 // ---------------------------------------------------------------------------
49 processVideoFileImport
52 // ---------------------------------------------------------------------------
54 async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
55 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
56 const { size } = await stat(inputFilePath)
57 const fps = await getVideoFileFPS(inputFilePath)
59 const fileExt = getLowercaseExtension(inputFilePath)
61 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution)
63 if (currentVideoFile) {
64 // Remove old file and old torrent
65 await video.removeFile(currentVideoFile)
66 await currentVideoFile.removeTorrent()
67 // Remove the old video file from the array
68 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
70 await currentVideoFile.destroy()
73 const newVideoFile = new VideoFileModel({
74 resolution: videoFileResolution,
76 filename: generateVideoFilename(video, false, videoFileResolution, fileExt),
82 const outputPath = getVideoFilePath(video, newVideoFile)
83 await copy(inputFilePath, outputPath)
85 video.VideoFiles.push(newVideoFile)
86 await createTorrentAndSetInfoHash(video, newVideoFile)
88 await newVideoFile.save()