]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-file-import.ts
Optimize torrent URL update
[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 { 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'
18
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)
22
23 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
24 // No video, maybe deleted?
25 if (!video) {
26 logger.info('Do not process job %d, video does not exist.', job.id)
27 return undefined
28 }
29
30 const data = await getVideoFileResolution(payload.filePath)
31
32 await updateVideoFile(video, payload.filePath)
33
34 const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
35
36 await createHlsJobIfEnabled(user, {
37 videoUUID: video.uuid,
38 resolution: data.resolution,
39 isPortraitMode: data.isPortraitMode,
40 copyCodecs: true,
41 isMaxQuality: false
42 })
43
44 if (CONFIG.OBJECT_STORAGE.ENABLED) {
45 await addMoveToObjectStorageJob(video)
46 } else {
47 await federateVideoIfNeeded(video, false)
48 }
49
50 return video
51 }
52
53 // ---------------------------------------------------------------------------
54
55 export {
56 processVideoFileImport
57 }
58
59 // ---------------------------------------------------------------------------
60
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)
65
66 const fileExt = getLowercaseExtension(inputFilePath)
67
68 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)
69
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)
75
76 await currentVideoFile.destroy()
77 }
78
79 const newVideoFile = new VideoFileModel({
80 resolution,
81 extname: fileExt,
82 filename: generateWebTorrentVideoFilename(resolution, fileExt),
83 storage: VideoStorage.FILE_SYSTEM,
84 size,
85 fps,
86 videoId: video.id
87 })
88
89 const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
90 await copy(inputFilePath, outputPath)
91
92 video.VideoFiles.push(newVideoFile)
93 await createTorrentAndSetInfoHash(video, newVideoFile)
94
95 await newVideoFile.save()
96 }