]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/live.ts
4ee39aa4ff7b920354016a18864eafe9ab8af823
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / live.ts
1 import express from 'express'
2 import { createReqFiles } from '@server/helpers/express-utils'
3 import { CONFIG } from '@server/initializers/config'
4 import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
5 import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
6 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
7 import { Hooks } from '@server/lib/plugins/hooks'
8 import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
9 import { videoLiveAddValidator, videoLiveGetValidator, videoLiveUpdateValidator } from '@server/middlewares/validators/videos/video-live'
10 import { VideoLiveModel } from '@server/models/video/video-live'
11 import { MVideoDetails, MVideoFullLight } from '@server/types/models'
12 import { buildUUID, uuidToShort } from '@shared/core-utils'
13 import { LiveVideoCreate, LiveVideoUpdate, VideoState } from '../../../../shared'
14 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
15 import { logger } from '../../../helpers/logger'
16 import { sequelizeTypescript } from '../../../initializers/database'
17 import { updateVideoMiniatureFromExisting } from '../../../lib/thumbnail'
18 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
19 import { VideoModel } from '../../../models/video/video'
20
21 const liveRouter = express.Router()
22
23 const reqVideoFileLive = createReqFiles(
24 [ 'thumbnailfile', 'previewfile' ],
25 MIMETYPES.IMAGE.MIMETYPE_EXT,
26 {
27 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
28 previewfile: CONFIG.STORAGE.TMP_DIR
29 }
30 )
31
32 liveRouter.post('/live',
33 authenticate,
34 reqVideoFileLive,
35 asyncMiddleware(videoLiveAddValidator),
36 asyncRetryTransactionMiddleware(addLiveVideo)
37 )
38
39 liveRouter.get('/live/:videoId',
40 authenticate,
41 asyncMiddleware(videoLiveGetValidator),
42 getLiveVideo
43 )
44
45 liveRouter.put('/live/:videoId',
46 authenticate,
47 asyncMiddleware(videoLiveGetValidator),
48 videoLiveUpdateValidator,
49 asyncRetryTransactionMiddleware(updateLiveVideo)
50 )
51
52 // ---------------------------------------------------------------------------
53
54 export {
55 liveRouter
56 }
57
58 // ---------------------------------------------------------------------------
59
60 function getLiveVideo (req: express.Request, res: express.Response) {
61 const videoLive = res.locals.videoLive
62
63 return res.json(videoLive.toFormattedJSON())
64 }
65
66 async function updateLiveVideo (req: express.Request, res: express.Response) {
67 const body: LiveVideoUpdate = req.body
68
69 const video = res.locals.videoAll
70 const videoLive = res.locals.videoLive
71
72 videoLive.saveReplay = body.saveReplay || false
73 videoLive.permanentLive = body.permanentLive || false
74
75 video.VideoLive = await videoLive.save()
76
77 await federateVideoIfNeeded(video, false)
78
79 return res.status(HttpStatusCode.NO_CONTENT_204).end()
80 }
81
82 async function addLiveVideo (req: express.Request, res: express.Response) {
83 const videoInfo: LiveVideoCreate = req.body
84
85 // Prepare data so we don't block the transaction
86 let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
87 videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result')
88
89 videoData.isLive = true
90 videoData.state = VideoState.WAITING_FOR_LIVE
91 videoData.duration = 0
92
93 const video = new VideoModel(videoData) as MVideoDetails
94 video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
95
96 const videoLive = new VideoLiveModel()
97 videoLive.saveReplay = videoInfo.saveReplay || false
98 videoLive.permanentLive = videoInfo.permanentLive || false
99 videoLive.streamKey = buildUUID()
100
101 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
102 video,
103 files: req.files,
104 fallback: type => {
105 return updateVideoMiniatureFromExisting({
106 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
107 video,
108 type,
109 automaticallyGenerated: true,
110 keepOriginal: true
111 })
112 }
113 })
114
115 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
116 const sequelizeOptions = { transaction: t }
117
118 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
119
120 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
121 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
122
123 // Do not forget to add video channel information to the created video
124 videoCreated.VideoChannel = res.locals.videoChannel
125
126 videoLive.videoId = videoCreated.id
127 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
128
129 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
130
131 await federateVideoIfNeeded(videoCreated, true, t)
132
133 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
134
135 return { videoCreated }
136 })
137
138 Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res })
139
140 return res.json({
141 video: {
142 id: videoCreated.id,
143 shortUUID: uuidToShort(videoCreated.uuid),
144 uuid: videoCreated.uuid
145 }
146 })
147 }