]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-file.ts
Reset published date on publish
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file.ts
1 import * as kue from 'kue'
2 import { VideoResolution, VideoState } from '../../../../shared'
3 import { logger } from '../../../helpers/logger'
4 import { computeResolutionsToTranscode } from '../../../helpers/utils'
5 import { VideoModel } from '../../../models/video/video'
6 import { JobQueue } from '../job-queue'
7 import { federateVideoIfNeeded } from '../../activitypub'
8 import { retryTransactionWrapper } from '../../../helpers/database-utils'
9 import { sequelizeTypescript } from '../../../initializers'
10
11 export type VideoFilePayload = {
12 videoUUID: string
13 isNewVideo?: boolean
14 resolution?: VideoResolution
15 isPortraitMode?: boolean
16 }
17
18 export type VideoFileImportPayload = {
19 videoUUID: string,
20 filePath: string
21 }
22
23 async function processVideoFileImport (job: kue.Job) {
24 const payload = job.data as VideoFileImportPayload
25 logger.info('Processing video file import in job %d.', job.id)
26
27 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
28 // No video, maybe deleted?
29 if (!video) {
30 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
31 return undefined
32 }
33
34 await video.importVideoFile(payload.filePath)
35
36 await onVideoFileTranscoderOrImportSuccess(video)
37 return video
38 }
39
40 async function processVideoFile (job: kue.Job) {
41 const payload = job.data as VideoFilePayload
42 logger.info('Processing video file in job %d.', job.id)
43
44 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
45 // No video, maybe deleted?
46 if (!video) {
47 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
48 return undefined
49 }
50
51 // Transcoding in other resolution
52 if (payload.resolution) {
53 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
54
55 await retryTransactionWrapper(onVideoFileTranscoderOrImportSuccess, video)
56 } else {
57 await video.optimizeOriginalVideofile()
58
59 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload.isNewVideo)
60 }
61
62 return video
63 }
64
65 async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
66 if (video === undefined) return undefined
67
68 return sequelizeTypescript.transaction(async t => {
69 // Maybe the video changed in database, refresh it
70 let videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
71 // Video does not exist anymore
72 if (!videoDatabase) return undefined
73
74 let isNewVideo = false
75
76 // We transcoded the video file in another format, now we can publish it
77 if (videoDatabase.state !== VideoState.PUBLISHED) {
78 isNewVideo = true
79
80 videoDatabase.state = VideoState.PUBLISHED
81 videoDatabase.publishedAt = new Date()
82 videoDatabase = await videoDatabase.save({ transaction: t })
83 }
84
85 // If the video was not published, we consider it is a new one for other instances
86 await federateVideoIfNeeded(videoDatabase, isNewVideo, t)
87
88 return undefined
89 })
90 }
91
92 async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
93 if (video === undefined) return undefined
94
95 // Outside the transaction (IO on disk)
96 const { videoFileResolution } = await video.getOriginalFileResolution()
97
98 return sequelizeTypescript.transaction(async t => {
99 // Maybe the video changed in database, refresh it
100 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
101 // Video does not exist anymore
102 if (!videoDatabase) return undefined
103
104 // Create transcoding jobs if there are enabled resolutions
105 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
106 logger.info(
107 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
108 { resolutions: resolutionsEnabled }
109 )
110
111 if (resolutionsEnabled.length !== 0) {
112 const tasks: Promise<any>[] = []
113
114 for (const resolution of resolutionsEnabled) {
115 const dataInput = {
116 videoUUID: videoDatabase.uuid,
117 resolution
118 }
119
120 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
121 tasks.push(p)
122 }
123
124 await Promise.all(tasks)
125
126 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
127 } else {
128 // No transcoding to do, it's now published
129 video.state = VideoState.PUBLISHED
130 video = await video.save({ transaction: t })
131
132 logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid)
133 }
134
135 return federateVideoIfNeeded(video, isNewVideo, t)
136 })
137 }
138
139 // ---------------------------------------------------------------------------
140
141 export {
142 processVideoFile,
143 processVideoFileImport
144 }