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