]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/live.ts
Move zxx to its own group in select-languages component (#4664)
[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'
c55e3d72 12import { buildUUID, uuidToShort } from '@shared/core-utils'
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
d17d7430
C
86 let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
87 videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result')
88
c6c0fa6c 89 videoData.isLive = true
1ef65f4c
C
90 videoData.state = VideoState.WAITING_FOR_LIVE
91 videoData.duration = 0
c6c0fa6c
C
92
93 const video = new VideoModel(videoData) as MVideoDetails
de94ac86 94 video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
c6c0fa6c 95
1ef65f4c 96 const videoLive = new VideoLiveModel()
b5b68755 97 videoLive.saveReplay = videoInfo.saveReplay || false
bb4ba6d9 98 videoLive.permanentLive = videoInfo.permanentLive || false
d4a8e7a6 99 videoLive.streamKey = buildUUID()
c6c0fa6c 100
1ef65f4c
C
101 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
102 video,
103 files: req.files,
104 fallback: type => {
91f8f8db 105 return updateVideoMiniatureFromExisting({
b5b68755
C
106 inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND,
107 video,
108 type,
109 automaticallyGenerated: true,
110 keepOriginal: true
111 })
1ef65f4c
C
112 }
113 })
c6c0fa6c
C
114
115 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
116 const sequelizeOptions = { transaction: t }
117
118 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
119
120 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
121 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
122
123 // Do not forget to add video channel information to the created video
124 videoCreated.VideoChannel = res.locals.videoChannel
125
126 videoLive.videoId = videoCreated.id
af4ae64f 127 videoCreated.VideoLive = await videoLive.save(sequelizeOptions)
c6c0fa6c 128
1ef65f4c 129 await setVideoTags({ video, tags: videoInfo.tags, transaction: t })
c6c0fa6c 130
af4ae64f
C
131 await federateVideoIfNeeded(videoCreated, true, t)
132
c6c0fa6c
C
133 logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid)
134
135 return { videoCreated }
136 })
137
7226e90f 138 Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res })
3cabf353 139
c6c0fa6c
C
140 return res.json({
141 video: {
142 id: videoCreated.id,
d4a8e7a6 143 shortUUID: uuidToShort(videoCreated.uuid),
c6c0fa6c
C
144 uuid: videoCreated.uuid
145 }
146 })
147}