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