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