ae11f1de3c88dd20350a86c881d58cf1e7c9f7e0
[oweals/peertube.git] / server / lib / job-queue / handlers / video-file-import.ts
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'
12 import { VideoFileImportPayload } from '@shared/models'
13
14 async function processVideoFileImport (job: Bull.Job) {
15   const payload = job.data as VideoFileImportPayload
16   logger.info('Processing video file import in job %d.', job.id)
17
18   const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
19   // No video, maybe deleted?
20   if (!video) {
21     logger.info('Do not process job %d, video does not exist.', job.id)
22     return undefined
23   }
24
25   await updateVideoFile(video, payload.filePath)
26
27   await publishNewResolutionIfNeeded(video)
28   return video
29 }
30
31 // ---------------------------------------------------------------------------
32
33 export {
34   processVideoFileImport
35 }
36
37 // ---------------------------------------------------------------------------
38
39 async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) {
40   const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
41   const { size } = await stat(inputFilePath)
42   const fps = await getVideoFileFPS(inputFilePath)
43
44   let updatedVideoFile = new VideoFileModel({
45     resolution: videoFileResolution,
46     extname: extname(inputFilePath),
47     size,
48     fps,
49     videoId: video.id
50   }) as MVideoFile
51
52   const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
53
54   if (currentVideoFile) {
55     // Remove old file and old torrent
56     await video.removeFile(currentVideoFile)
57     await video.removeTorrent(currentVideoFile)
58     // Remove the old video file from the array
59     video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
60
61     // Update the database
62     currentVideoFile.extname = updatedVideoFile.extname
63     currentVideoFile.size = updatedVideoFile.size
64     currentVideoFile.fps = updatedVideoFile.fps
65
66     updatedVideoFile = currentVideoFile
67   }
68
69   const outputPath = getVideoFilePath(video, updatedVideoFile)
70   await copy(inputFilePath, outputPath)
71
72   await createTorrentAndSetInfoHash(video, updatedVideoFile)
73
74   await updatedVideoFile.save()
75
76   video.VideoFiles.push(updatedVideoFile)
77 }