1 import * as Bull from 'bull'
2 import { logger } from '../../../helpers/logger'
3 import { VideoModel } from '../../../models/video/video'
4 import { publishVideoIfNeeded } 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'
10 export type VideoFileImportPayload = {
15 async function processVideoFileImport (job: Bull.Job) {
16 const payload = job.data as VideoFileImportPayload
17 logger.info('Processing video file import in job %d.', job.id)
19 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
20 // No video, maybe deleted?
22 logger.info('Do not process job %d, video does not exist.', job.id)
26 await updateVideoFile(video, payload.filePath)
28 await publishVideoIfNeeded(video)
32 // ---------------------------------------------------------------------------
35 processVideoFileImport
38 // ---------------------------------------------------------------------------
40 async function updateVideoFile (video: VideoModel, inputFilePath: string) {
41 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
42 const { size } = await stat(inputFilePath)
43 const fps = await getVideoFileFPS(inputFilePath)
45 let updatedVideoFile = new VideoFileModel({
46 resolution: videoFileResolution,
47 extname: extname(inputFilePath),
53 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
55 if (currentVideoFile) {
56 // Remove old file and old torrent
57 await video.removeFile(currentVideoFile)
58 await video.removeTorrent(currentVideoFile)
59 // Remove the old video file from the array
60 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
62 // Update the database
63 currentVideoFile.set('extname', updatedVideoFile.extname)
64 currentVideoFile.set('size', updatedVideoFile.size)
65 currentVideoFile.set('fps', updatedVideoFile.fps)
67 updatedVideoFile = currentVideoFile
70 const outputPath = video.getVideoFilePath(updatedVideoFile)
71 await copy(inputFilePath, outputPath)
73 await video.createTorrentAndSetInfoHash(updatedVideoFile)
75 await updatedVideoFile.save()
77 video.VideoFiles.push(updatedVideoFile)