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