]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-file-import.ts
Update validator dependency
[github/Chocobozzz/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
13 export type VideoFileImportPayload = {
14 videoUUID: string,
15 filePath: string
16 }
17
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)
21
22 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
23 // No video, maybe deleted?
24 if (!video) {
25 logger.info('Do not process job %d, video does not exist.', job.id)
26 return undefined
27 }
28
29 await updateVideoFile(video, payload.filePath)
30
31 await publishNewResolutionIfNeeded(video)
32 return video
33 }
34
35 // ---------------------------------------------------------------------------
36
37 export {
38 processVideoFileImport
39 }
40
41 // ---------------------------------------------------------------------------
42
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)
47
48 let updatedVideoFile = new VideoFileModel({
49 resolution: videoFileResolution,
50 extname: extname(inputFilePath),
51 size,
52 fps,
53 videoId: video.id
54 }) as MVideoFile
55
56 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
57
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)
64
65 // Update the database
66 currentVideoFile.extname = updatedVideoFile.extname
67 currentVideoFile.size = updatedVideoFile.size
68 currentVideoFile.fps = updatedVideoFile.fps
69
70 updatedVideoFile = currentVideoFile
71 }
72
73 const outputPath = getVideoFilePath(video, updatedVideoFile)
74 await copy(inputFilePath, outputPath)
75
76 await createTorrentAndSetInfoHash(video, updatedVideoFile)
77
78 await updatedVideoFile.save()
79
80 video.VideoFiles.push(updatedVideoFile)
81 }