]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/live.ts
refactor API errors to standard error format
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / live.ts
1 import * as express from 'express'
2 import { v4 as uuidv4 } from 'uuid'
3 import { createReqFiles } from '@server/helpers/express-utils'
4 import { CONFIG } from '@server/initializers/config'
5 import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
6 import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
7 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
8 import { Hooks } from '@server/lib/plugins/hooks'
9 import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
10 import { videoLiveAddValidator, videoLiveGetValidator, videoLiveUpdateValidator } from '@server/middlewares/validators/videos/video-live'
11 import { VideoLiveModel } from '@server/models/video/video-live'
12 import { MVideoDetails, MVideoFullLight } from '@server/types/models'
13 import { LiveVideoCreate, LiveVideoUpdate, VideoState } from '../../../../shared'
14 import { logger } from '../../../helpers/logger'
15 import { sequelizeTypescript } from '../../../initializers/database'
16 import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail'
17 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
18 import { VideoModel } from '../../../models/video/video'
19 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
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 asyncRetryTransactionMiddleware(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 async 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 const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
87 videoData.isLive = true
88 videoData.state = VideoState.WAITING_FOR_LIVE
89 videoData.duration = 0
90
91 const video = new VideoModel(videoData) as MVideoDetails
92 video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
93
94 const videoLive = new VideoLiveModel()
95 videoLive.saveReplay = videoInfo.saveReplay || false
96 videoLive.permanentLive = videoInfo.permanentLive || false
97 videoLive.streamKey = uuidv4()
98
99 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
100 video,
101 files: req.files,
102 fallback: type => {
103 return createVideoMiniatureFromExisting({
104 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
105 video,
106 type,
107 automaticallyGenerated: true,
108 keepOriginal: true
109 })
110 }
111 })
112
113 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
114 const sequelizeOptions = { transaction: t }
115
116 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
117
118 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
119 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
120
121 // Do not forget to add video channel information to the created video
122 videoCreated.VideoChannel = res.locals.videoChannel
123
124 videoLive.videoId = videoCreated.id
125 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
126
127 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
128
129 await federateVideoIfNeeded(videoCreated, true, t)
130
131 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
132
133 return { videoCreated }
134 })
135
136 Hooks.runAction('action:api.live-video.created', { video: videoCreated })
137
138 return res.json({
139 video: {
140 id: videoCreated.id,
141 uuid: videoCreated.uuid
142 }
143 })
144 }