]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-file.ts
Fix video_share_url duplicate key in transcoding job
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file.ts
CommitLineData
94a5ff8a
C
1import * as kue from 'kue'
2import { VideoResolution } from '../../../../shared'
e12a0092 3import { VideoPrivacy } from '../../../../shared/models/videos'
da854ddd
C
4import { logger } from '../../../helpers/logger'
5import { computeResolutionsToTranscode } from '../../../helpers/utils'
3fd3ab2d
C
6import { sequelizeTypescript } from '../../../initializers'
7import { VideoModel } from '../../../models/video/video'
a7d647c4 8import { shareVideoByServerAndChannel } from '../../activitypub'
94a5ff8a
C
9import { sendCreateVideo, sendUpdateVideo } from '../../activitypub/send'
10import { JobQueue } from '../job-queue'
40298b02 11
94a5ff8a
C
12export type VideoFilePayload = {
13 videoUUID: string
056aa7f2
C
14 resolution?: VideoResolution,
15 isPortraitMode?: boolean
94a5ff8a
C
16}
17
18async function processVideoFile (job: kue.Job) {
19 const payload = job.data as VideoFilePayload
20 logger.info('Processing video file in job %d.', job.id)
21
22 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
f5028693
C
23 // No video, maybe deleted?
24 if (!video) {
94a5ff8a 25 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
f5028693
C
26 return undefined
27 }
28
94a5ff8a
C
29 // Transcoding in other resolution
30 if (payload.resolution) {
056aa7f2 31 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
94a5ff8a
C
32 await onVideoFileTranscoderSuccess(video)
33 } else {
34 await video.optimizeOriginalVideofile()
35 await onVideoFileOptimizerSuccess(video)
36 }
031094f7 37
f5028693 38 return video
40298b02
C
39}
40
94a5ff8a
C
41async function onVideoFileTranscoderSuccess (video: VideoModel) {
42 if (video === undefined) return undefined
43
44 // Maybe the video changed in database, refresh it
45 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
46 // Video does not exist anymore
47 if (!videoDatabase) return undefined
48
49 if (video.privacy !== VideoPrivacy.PRIVATE) {
50 await sendUpdateVideo(video, undefined)
51 }
52
53 return undefined
40298b02
C
54}
55
94a5ff8a 56async function onVideoFileOptimizerSuccess (video: VideoModel) {
031094f7
C
57 if (video === undefined) return undefined
58
4077df72 59 // Maybe the video changed in database, refresh it
3fd3ab2d 60 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
4077df72
C
61 // Video does not exist anymore
62 if (!videoDatabase) return undefined
63
e12a0092
C
64 if (video.privacy !== VideoPrivacy.PRIVATE) {
65 // Now we'll add the video's meta data to our followers
a7977280
C
66 await sequelizeTypescript.transaction(async t => {
67 await sendCreateVideo(video, t)
68 await shareVideoByServerAndChannel(video, t)
69 })
e12a0092 70 }
f5028693 71
056aa7f2 72 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
f5028693 73
8e10cf1a 74 // Create transcoding jobs if there are enabled resolutions
056aa7f2 75 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
f5028693 76 logger.info(
056aa7f2 77 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
f5028693
C
78 { resolutions: resolutionsEnabled }
79 )
80
81 if (resolutionsEnabled.length !== 0) {
a7977280
C
82 const tasks: Promise<any>[] = []
83
84 for (const resolution of resolutionsEnabled) {
85 const dataInput = {
86 videoUUID: videoDatabase.uuid,
87 resolution
88 }
89
90 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
91 tasks.push(p)
f5028693 92 }
a7977280
C
93
94 await Promise.all(tasks)
95
96 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
f5028693
C
97 } else {
98 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
99 return undefined
100 }
40298b02
C
101}
102
103// ---------------------------------------------------------------------------
104
105export {
94a5ff8a 106 processVideoFile
40298b02 107}