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'
11 export type VideoFileImportPayload = {
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 await updateVideoFile(video, payload.filePath)
29 await publishNewResolutionIfNeeded(video)
33 // ---------------------------------------------------------------------------
36 processVideoFileImport
39 // ---------------------------------------------------------------------------
41 async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) {
42 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
43 const { size } = await stat(inputFilePath)
44 const fps = await getVideoFileFPS(inputFilePath)
46 let updatedVideoFile = new VideoFileModel({
47 resolution: videoFileResolution,
48 extname: extname(inputFilePath),
54 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
56 if (currentVideoFile) {
57 // Remove old file and old torrent
58 await video.removeFile(currentVideoFile)
59 await video.removeTorrent(currentVideoFile)
60 // Remove the old video file from the array
61 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
63 // Update the database
64 currentVideoFile.extname = updatedVideoFile.extname
65 currentVideoFile.size = updatedVideoFile.size
66 currentVideoFile.fps = updatedVideoFile.fps
68 updatedVideoFile = currentVideoFile
71 const outputPath = video.getVideoFilePath(updatedVideoFile)
72 await copy(inputFilePath, outputPath)
74 await video.createTorrentAndSetInfoHash(updatedVideoFile)
76 await updatedVideoFile.save()
78 video.VideoFiles.push(updatedVideoFile)