]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/live.ts
Add `req` and `res` as controllers hooks parameters
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / live.ts
CommitLineData
41fb13c3 1import express from 'express'
c6c0fa6c 2import { createReqFiles } from '@server/helpers/express-utils'
d4a8e7a6 3import { buildUUID, uuidToShort } from '@server/helpers/uuid'
c6c0fa6c
C
4import { CONFIG } from '@server/initializers/config'
5import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
de94ac86 6import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
af4ae64f 7import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
3cabf353 8import { Hooks } from '@server/lib/plugins/hooks'
1ef65f4c 9import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
b5b68755 10import { videoLiveAddValidator, videoLiveGetValidator, videoLiveUpdateValidator } from '@server/middlewares/validators/videos/video-live'
c6c0fa6c
C
11import { VideoLiveModel } from '@server/models/video/video-live'
12import { MVideoDetails, MVideoFullLight } from '@server/types/models'
b5b68755 13import { LiveVideoCreate, LiveVideoUpdate, VideoState } from '../../../../shared'
c0e8b12e 14import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
c6c0fa6c
C
15import { logger } from '../../../helpers/logger'
16import { sequelizeTypescript } from '../../../initializers/database'
91f8f8db 17import { updateVideoMiniatureFromExisting } from '../../../lib/thumbnail'
c6c0fa6c 18import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
c6c0fa6c 19import { VideoModel } from '../../../models/video/video'
c6c0fa6c
C
20
21const liveRouter = express.Router()
22
23const 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
32liveRouter.post('/live',
33 authenticate,
34 reqVideoFileLive,
35 asyncMiddleware(videoLiveAddValidator),
36 asyncRetryTransactionMiddleware(addLiveVideo)
37)
38
39liveRouter.get('/live/:videoId',
40 authenticate,
41 asyncMiddleware(videoLiveGetValidator),
98ab5dc8 42 getLiveVideo
b5b68755
C
43)
44
45liveRouter.put('/live/:videoId',
46 authenticate,
47 asyncMiddleware(videoLiveGetValidator),
48 videoLiveUpdateValidator,
49 asyncRetryTransactionMiddleware(updateLiveVideo)
c6c0fa6c
C
50)
51
52// ---------------------------------------------------------------------------
53
54export {
55 liveRouter
56}
57
58// ---------------------------------------------------------------------------
59
98ab5dc8 60function getLiveVideo (req: express.Request, res: express.Response) {
c6c0fa6c
C
61 const videoLive = res.locals.videoLive
62
63 return res.json(videoLive.toFormattedJSON())
64}
65
b5b68755
C
66async function updateLiveVideo (req: express.Request, res: express.Response) {
67 const body: LiveVideoUpdate = req.body
68
af4ae64f 69 const video = res.locals.videoAll
b5b68755 70 const videoLive = res.locals.videoLive
bb4ba6d9 71
b5b68755 72 videoLive.saveReplay = body.saveReplay || false
bb4ba6d9 73 videoLive.permanentLive = body.permanentLive || false
b5b68755 74
af4ae64f
C
75 video.VideoLive = await videoLive.save()
76
77 await federateVideoIfNeeded(video, false)
b5b68755 78
76148b27 79 return res.status(HttpStatusCode.NO_CONTENT_204).end()
b5b68755
C
80}
81
c6c0fa6c 82async function addLiveVideo (req: express.Request, res: express.Response) {
b5b68755 83 const videoInfo: LiveVideoCreate = req.body
c6c0fa6c
C
84
85 // Prepare data so we don't block the transaction
1ef65f4c 86 const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
c6c0fa6c 87 videoData.isLive = true
1ef65f4c
C
88 videoData.state = VideoState.WAITING_FOR_LIVE
89 videoData.duration = 0
c6c0fa6c
C
90
91 const video = new VideoModel(videoData) as MVideoDetails
de94ac86 92 video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
c6c0fa6c 93
1ef65f4c 94 const videoLive = new VideoLiveModel()
b5b68755 95 videoLive.saveReplay = videoInfo.saveReplay || false
bb4ba6d9 96 videoLive.permanentLive = videoInfo.permanentLive || false
d4a8e7a6 97 videoLive.streamKey = buildUUID()
c6c0fa6c 98
1ef65f4c
C
99 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
100 video,
101 files: req.files,
102 fallback: type => {
91f8f8db 103 return updateVideoMiniatureFromExisting({
b5b68755
C
104 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
105 video,
106 type,
107 automaticallyGenerated: true,
108 keepOriginal: true
109 })
1ef65f4c
C
110 }
111 })
c6c0fa6c
C
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
af4ae64f 125 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
c6c0fa6c 126
1ef65f4c 127 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
c6c0fa6c 128
af4ae64f
C
129 await federateVideoIfNeeded(videoCreated, true, t)
130
c6c0fa6c
C
131 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
132
133 return { videoCreated }
134 })
135
7226e90f 136 Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res })
3cabf353 137
c6c0fa6c
C
138 return res.json({
139 video: {
140 id: videoCreated.id,
d4a8e7a6 141 shortUUID: uuidToShort(videoCreated.uuid),
c6c0fa6c
C
142 uuid: videoCreated.uuid
143 }
144 })
145}