]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-file-import.ts
048963033a9bf0de137b618a16ac410c3860a31b
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file-import.ts
1 import * as Bull from 'bull'
2 import { copy, stat } from 'fs-extra'
3 import { extname } from 'path'
4 import { getLowercaseExtension } from '@server/helpers/core-utils'
5 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
6 import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
7 import { UserModel } from '@server/models/user/user'
8 import { MVideoFullLight } from '@server/types/models'
9 import { VideoFileImportPayload } from '@shared/models'
10 import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
11 import { logger } from '../../../helpers/logger'
12 import { VideoModel } from '../../../models/video/video'
13 import { VideoFileModel } from '../../../models/video/video-file'
14 import { onNewWebTorrentFileResolution } from './video-transcoding'
15
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)
19
20 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
21 // No video, maybe deleted?
22 if (!video) {
23 logger.info('Do not process job %d, video does not exist.', job.id)
24 return undefined
25 }
26
27 const data = await getVideoFileResolution(payload.filePath)
28
29 await updateVideoFile(video, payload.filePath)
30
31 const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
32
33 const newResolutionPayload = {
34 type: 'new-resolution-to-webtorrent' as 'new-resolution-to-webtorrent',
35 videoUUID: video.uuid,
36 resolution: data.videoFileResolution,
37 isPortraitMode: data.isPortraitMode,
38 copyCodecs: false,
39 isNewVideo: false
40 }
41 await onNewWebTorrentFileResolution(video, user, newResolutionPayload)
42
43 return video
44 }
45
46 // ---------------------------------------------------------------------------
47
48 export {
49 processVideoFileImport
50 }
51
52 // ---------------------------------------------------------------------------
53
54 async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
55 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
56 const { size } = await stat(inputFilePath)
57 const fps = await getVideoFileFPS(inputFilePath)
58
59 const fileExt = getLowercaseExtension(inputFilePath)
60
61 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution)
62
63 if (currentVideoFile) {
64 // Remove old file and old torrent
65 await video.removeFile(currentVideoFile)
66 await currentVideoFile.removeTorrent()
67 // Remove the old video file from the array
68 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
69
70 await currentVideoFile.destroy()
71 }
72
73 const newVideoFile = new VideoFileModel({
74 resolution: videoFileResolution,
75 extname: fileExt,
76 filename: generateVideoFilename(video, false, videoFileResolution, fileExt),
77 size,
78 fps,
79 videoId: video.id
80 })
81
82 const outputPath = getVideoFilePath(video, newVideoFile)
83 await copy(inputFilePath, outputPath)
84
85 video.VideoFiles.push(newVideoFile)
86 await createTorrentAndSetInfoHash(video, newVideoFile)
87
88 await newVideoFile.save()
89 }