]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
f443a746 | 2 | import { exists } from '@server/helpers/custom-validators/misc' |
c6c0fa6c | 3 | import { createReqFiles } from '@server/helpers/express-utils' |
26e3e98f | 4 | import { getFormattedObjects } from '@server/helpers/utils' |
c6c0fa6c | 5 | import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants' |
de94ac86 | 6 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' |
af4ae64f | 7 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' |
3cabf353 | 8 | import { Hooks } from '@server/lib/plugins/hooks' |
1ef65f4c | 9 | import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' |
26e3e98f C |
10 | import { |
11 | videoLiveAddValidator, | |
12 | videoLiveFindReplaySessionValidator, | |
13 | videoLiveGetValidator, | |
14 | videoLiveListSessionsValidator, | |
15 | videoLiveUpdateValidator | |
16 | } from '@server/middlewares/validators/videos/video-live' | |
c6c0fa6c | 17 | import { VideoLiveModel } from '@server/models/video/video-live' |
26e3e98f | 18 | import { VideoLiveSessionModel } from '@server/models/video/video-live-session' |
c6c0fa6c | 19 | import { MVideoDetails, MVideoFullLight } from '@server/types/models' |
0628157f | 20 | import { buildUUID, uuidToShort } from '@shared/extra-utils' |
961cbe42 | 21 | import { HttpStatusCode, LiveVideoCreate, LiveVideoLatencyMode, LiveVideoUpdate, UserRight, VideoState } from '@shared/models' |
c6c0fa6c C |
22 | import { logger } from '../../../helpers/logger' |
23 | import { sequelizeTypescript } from '../../../initializers/database' | |
91f8f8db | 24 | import { updateVideoMiniatureFromExisting } from '../../../lib/thumbnail' |
961cbe42 | 25 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, optionalAuthenticate } from '../../../middlewares' |
c6c0fa6c | 26 | import { VideoModel } from '../../../models/video/video' |
c6c0fa6c C |
27 | |
28 | const liveRouter = express.Router() | |
29 | ||
d3d3deaa | 30 | const reqVideoFileLive = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
c6c0fa6c C |
31 | |
32 | liveRouter.post('/live', | |
33 | authenticate, | |
34 | reqVideoFileLive, | |
35 | asyncMiddleware(videoLiveAddValidator), | |
36 | asyncRetryTransactionMiddleware(addLiveVideo) | |
37 | ) | |
38 | ||
26e3e98f C |
39 | liveRouter.get('/live/:videoId/sessions', |
40 | authenticate, | |
41 | asyncMiddleware(videoLiveGetValidator), | |
42 | videoLiveListSessionsValidator, | |
43 | asyncMiddleware(getLiveVideoSessions) | |
44 | ) | |
45 | ||
c6c0fa6c | 46 | liveRouter.get('/live/:videoId', |
961cbe42 | 47 | optionalAuthenticate, |
c6c0fa6c | 48 | asyncMiddleware(videoLiveGetValidator), |
98ab5dc8 | 49 | getLiveVideo |
b5b68755 C |
50 | ) |
51 | ||
52 | liveRouter.put('/live/:videoId', | |
53 | authenticate, | |
54 | asyncMiddleware(videoLiveGetValidator), | |
55 | videoLiveUpdateValidator, | |
56 | asyncRetryTransactionMiddleware(updateLiveVideo) | |
c6c0fa6c C |
57 | ) |
58 | ||
26e3e98f C |
59 | liveRouter.get('/:videoId/live-session', |
60 | asyncMiddleware(videoLiveFindReplaySessionValidator), | |
61 | getLiveReplaySession | |
62 | ) | |
63 | ||
c6c0fa6c C |
64 | // --------------------------------------------------------------------------- |
65 | ||
66 | export { | |
67 | liveRouter | |
68 | } | |
69 | ||
70 | // --------------------------------------------------------------------------- | |
71 | ||
98ab5dc8 | 72 | function getLiveVideo (req: express.Request, res: express.Response) { |
c6c0fa6c C |
73 | const videoLive = res.locals.videoLive |
74 | ||
961cbe42 C |
75 | return res.json(videoLive.toFormattedJSON(canSeePrivateLiveInformation(res))) |
76 | } | |
77 | ||
26e3e98f C |
78 | function getLiveReplaySession (req: express.Request, res: express.Response) { |
79 | const session = res.locals.videoLiveSession | |
80 | ||
81 | return res.json(session.toFormattedJSON()) | |
82 | } | |
83 | ||
84 | async function getLiveVideoSessions (req: express.Request, res: express.Response) { | |
85 | const videoLive = res.locals.videoLive | |
86 | ||
87 | const data = await VideoLiveSessionModel.listSessionsOfLiveForAPI({ videoId: videoLive.videoId }) | |
88 | ||
89 | return res.json(getFormattedObjects(data, data.length)) | |
90 | } | |
91 | ||
961cbe42 C |
92 | function canSeePrivateLiveInformation (res: express.Response) { |
93 | const user = res.locals.oauth?.token.User | |
94 | if (!user) return false | |
95 | ||
96 | if (user.hasRight(UserRight.GET_ANY_LIVE)) return true | |
97 | ||
98 | const video = res.locals.videoAll | |
99 | return video.VideoChannel.Account.userId === user.id | |
c6c0fa6c C |
100 | } |
101 | ||
b5b68755 C |
102 | async function updateLiveVideo (req: express.Request, res: express.Response) { |
103 | const body: LiveVideoUpdate = req.body | |
104 | ||
af4ae64f | 105 | const video = res.locals.videoAll |
b5b68755 | 106 | const videoLive = res.locals.videoLive |
bb4ba6d9 | 107 | |
f443a746 C |
108 | if (exists(body.saveReplay)) videoLive.saveReplay = body.saveReplay |
109 | if (exists(body.permanentLive)) videoLive.permanentLive = body.permanentLive | |
110 | if (exists(body.latencyMode)) videoLive.latencyMode = body.latencyMode | |
b5b68755 | 111 | |
af4ae64f C |
112 | video.VideoLive = await videoLive.save() |
113 | ||
114 | await federateVideoIfNeeded(video, false) | |
b5b68755 | 115 | |
76148b27 | 116 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
b5b68755 C |
117 | } |
118 | ||
c6c0fa6c | 119 | async function addLiveVideo (req: express.Request, res: express.Response) { |
b5b68755 | 120 | const videoInfo: LiveVideoCreate = req.body |
c6c0fa6c C |
121 | |
122 | // Prepare data so we don't block the transaction | |
d17d7430 C |
123 | let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id) |
124 | videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result') | |
125 | ||
c6c0fa6c | 126 | videoData.isLive = true |
1ef65f4c C |
127 | videoData.state = VideoState.WAITING_FOR_LIVE |
128 | videoData.duration = 0 | |
c6c0fa6c C |
129 | |
130 | const video = new VideoModel(videoData) as MVideoDetails | |
de94ac86 | 131 | video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object |
c6c0fa6c | 132 | |
1ef65f4c | 133 | const videoLive = new VideoLiveModel() |
b5b68755 | 134 | videoLive.saveReplay = videoInfo.saveReplay || false |
bb4ba6d9 | 135 | videoLive.permanentLive = videoInfo.permanentLive || false |
f443a746 | 136 | videoLive.latencyMode = videoInfo.latencyMode || LiveVideoLatencyMode.DEFAULT |
d4a8e7a6 | 137 | videoLive.streamKey = buildUUID() |
c6c0fa6c | 138 | |
1ef65f4c C |
139 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ |
140 | video, | |
141 | files: req.files, | |
142 | fallback: type => { | |
91f8f8db | 143 | return updateVideoMiniatureFromExisting({ |
b5b68755 C |
144 | inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, |
145 | video, | |
146 | type, | |
147 | automaticallyGenerated: true, | |
148 | keepOriginal: true | |
149 | }) | |
1ef65f4c C |
150 | } |
151 | }) | |
c6c0fa6c C |
152 | |
153 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { | |
154 | const sequelizeOptions = { transaction: t } | |
155 | ||
156 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight | |
157 | ||
158 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) | |
159 | if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t) | |
160 | ||
161 | // Do not forget to add video channel information to the created video | |
162 | videoCreated.VideoChannel = res.locals.videoChannel | |
163 | ||
164 | videoLive.videoId = videoCreated.id | |
af4ae64f | 165 | videoCreated.VideoLive = await videoLive.save(sequelizeOptions) |
c6c0fa6c | 166 | |
1ef65f4c | 167 | await setVideoTags({ video, tags: videoInfo.tags, transaction: t }) |
c6c0fa6c | 168 | |
af4ae64f C |
169 | await federateVideoIfNeeded(videoCreated, true, t) |
170 | ||
c6c0fa6c C |
171 | logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid) |
172 | ||
173 | return { videoCreated } | |
174 | }) | |
175 | ||
7226e90f | 176 | Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res }) |
3cabf353 | 177 | |
c6c0fa6c C |
178 | return res.json({ |
179 | video: { | |
180 | id: videoCreated.id, | |
d4a8e7a6 | 181 | shortUUID: uuidToShort(videoCreated.uuid), |
c6c0fa6c C |
182 | uuid: videoCreated.uuid |
183 | } | |
184 | }) | |
185 | } |