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