1 import * as Bull from 'bull'
2 import { copy, stat } from 'fs-extra'
3 import { getLowercaseExtension } from '@server/helpers/core-utils'
4 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
5 import { CONFIG } from '@server/initializers/config'
6 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
7 import { generateWebTorrentVideoFilename } from '@server/lib/paths'
8 import { addMoveToObjectStorageJob } from '@server/lib/video'
9 import { VideoPathManager } from '@server/lib/video-path-manager'
10 import { UserModel } from '@server/models/user/user'
11 import { MVideoFullLight } from '@server/types/models'
12 import { VideoFileImportPayload, VideoStorage } from '@shared/models'
13 import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
14 import { logger } from '../../../helpers/logger'
15 import { VideoModel } from '../../../models/video/video'
16 import { VideoFileModel } from '../../../models/video/video-file'
17 import { createHlsJobIfEnabled } from './video-transcoding'
19 async function processVideoFileImport (job: Bull.Job) {
20 const payload = job.data as VideoFileImportPayload
21 logger.info('Processing video file import in job %d.', job.id)
23 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
24 // No video, maybe deleted?
26 logger.info('Do not process job %d, video does not exist.', job.id)
30 const data = await getVideoFileResolution(payload.filePath)
32 await updateVideoFile(video, payload.filePath)
34 const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
36 await createHlsJobIfEnabled(user, {
37 videoUUID: video.uuid,
38 resolution: data.resolution,
39 isPortraitMode: data.isPortraitMode,
44 if (CONFIG.OBJECT_STORAGE.ENABLED) {
45 await addMoveToObjectStorageJob(video)
47 await federateVideoIfNeeded(video, false)
53 // ---------------------------------------------------------------------------
56 processVideoFileImport
59 // ---------------------------------------------------------------------------
61 async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
62 const { resolution } = await getVideoFileResolution(inputFilePath)
63 const { size } = await stat(inputFilePath)
64 const fps = await getVideoFileFPS(inputFilePath)
66 const fileExt = getLowercaseExtension(inputFilePath)
68 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)
70 if (currentVideoFile) {
71 // Remove old file and old torrent
72 await video.removeFileAndTorrent(currentVideoFile)
73 // Remove the old video file from the array
74 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
76 await currentVideoFile.destroy()
79 const newVideoFile = new VideoFileModel({
82 filename: generateWebTorrentVideoFilename(resolution, fileExt),
83 storage: VideoStorage.FILE_SYSTEM,
89 const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
90 await copy(inputFilePath, outputPath)
92 video.VideoFiles.push(newVideoFile)
93 await createTorrentAndSetInfoHash(video, newVideoFile)
95 await newVideoFile.save()