1 import * as Bull from 'bull'
2 import { logger } from '../../../helpers/logger'
3 import { VideoModel } from '../../../models/video/video'
4 import { publishNewResolutionIfNeeded } from './video-transcoding'
5 import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
6 import { copy, stat } from 'fs-extra'
7 import { VideoFileModel } from '../../../models/video/video-file'
8 import { extname } from 'path'
9 import { MVideoFile, MVideoWithFile } from '@server/typings/models'
10 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
11 import { getVideoFilePath } from '@server/lib/video-paths'
13 export type VideoFileImportPayload = {
18 async function processVideoFileImport (job: Bull.Job) {
19 const payload = job.data as VideoFileImportPayload
20 logger.info('Processing video file import in job %d.', job.id)
22 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
23 // No video, maybe deleted?
25 logger.info('Do not process job %d, video does not exist.', job.id)
29 await updateVideoFile(video, payload.filePath)
31 await publishNewResolutionIfNeeded(video)
35 // ---------------------------------------------------------------------------
38 processVideoFileImport
41 // ---------------------------------------------------------------------------
43 async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) {
44 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
45 const { size } = await stat(inputFilePath)
46 const fps = await getVideoFileFPS(inputFilePath)
48 let updatedVideoFile = new VideoFileModel({
49 resolution: videoFileResolution,
50 extname: extname(inputFilePath),
56 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
58 if (currentVideoFile) {
59 // Remove old file and old torrent
60 await video.removeFile(currentVideoFile)
61 await video.removeTorrent(currentVideoFile)
62 // Remove the old video file from the array
63 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
65 // Update the database
66 currentVideoFile.extname = updatedVideoFile.extname
67 currentVideoFile.size = updatedVideoFile.size
68 currentVideoFile.fps = updatedVideoFile.fps
70 updatedVideoFile = currentVideoFile
73 const outputPath = getVideoFilePath(video, updatedVideoFile)
74 await copy(inputFilePath, outputPath)
76 await createTorrentAndSetInfoHash(video, updatedVideoFile)
78 await updatedVideoFile.save()
80 video.VideoFiles.push(updatedVideoFile)