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