]>
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' | |
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' |
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' |
2d53be02 | 19 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
c6c0fa6c C |
20 | |
21 | const liveRouter = express.Router() | |
22 | ||
23 | const 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 | ||
32 | liveRouter.post('/live', | |
33 | authenticate, | |
34 | reqVideoFileLive, | |
35 | asyncMiddleware(videoLiveAddValidator), | |
36 | asyncRetryTransactionMiddleware(addLiveVideo) | |
37 | ) | |
38 | ||
39 | liveRouter.get('/live/:videoId', | |
40 | authenticate, | |
41 | asyncMiddleware(videoLiveGetValidator), | |
b5b68755 C |
42 | asyncRetryTransactionMiddleware(getLiveVideo) |
43 | ) | |
44 | ||
45 | liveRouter.put('/live/:videoId', | |
46 | authenticate, | |
47 | asyncMiddleware(videoLiveGetValidator), | |
48 | videoLiveUpdateValidator, | |
49 | asyncRetryTransactionMiddleware(updateLiveVideo) | |
c6c0fa6c C |
50 | ) |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | ||
54 | export { | |
55 | liveRouter | |
56 | } | |
57 | ||
58 | // --------------------------------------------------------------------------- | |
59 | ||
b5b68755 | 60 | async function 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 |
66 | async 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 | |
2d53be02 | 79 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |
b5b68755 C |
80 | } |
81 | ||
c6c0fa6c | 82 | async 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 | |
1ef65f4c | 86 | const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id) |
c6c0fa6c | 87 | videoData.isLive = true |
1ef65f4c C |
88 | videoData.state = VideoState.WAITING_FOR_LIVE |
89 | videoData.duration = 0 | |
c6c0fa6c C |
90 | |
91 | const video = new VideoModel(videoData) as MVideoDetails | |
de94ac86 | 92 | video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object |
c6c0fa6c | 93 | |
1ef65f4c | 94 | const videoLive = new VideoLiveModel() |
b5b68755 | 95 | videoLive.saveReplay = videoInfo.saveReplay || false |
bb4ba6d9 | 96 | videoLive.permanentLive = videoInfo.permanentLive || false |
1ef65f4c | 97 | videoLive.streamKey = uuidv4() |
c6c0fa6c | 98 | |
1ef65f4c C |
99 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ |
100 | video, | |
101 | files: req.files, | |
102 | fallback: type => { | |
b5b68755 C |
103 | return createVideoMiniatureFromExisting({ |
104 | inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, | |
105 | video, | |
106 | type, | |
107 | automaticallyGenerated: true, | |
108 | keepOriginal: true | |
109 | }) | |
1ef65f4c C |
110 | } |
111 | }) | |
c6c0fa6c C |
112 | |
113 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { | |
114 | const sequelizeOptions = { transaction: t } | |
115 | ||
116 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight | |
117 | ||
118 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) | |
119 | if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t) | |
120 | ||
121 | // Do not forget to add video channel information to the created video | |
122 | videoCreated.VideoChannel = res.locals.videoChannel | |
123 | ||
124 | videoLive.videoId = videoCreated.id | |
af4ae64f | 125 | videoCreated.VideoLive = await videoLive.save(sequelizeOptions) |
c6c0fa6c | 126 | |
1ef65f4c | 127 | await setVideoTags({ video, tags: videoInfo.tags, transaction: t }) |
c6c0fa6c | 128 | |
af4ae64f C |
129 | await federateVideoIfNeeded(videoCreated, true, t) |
130 | ||
c6c0fa6c C |
131 | logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid) |
132 | ||
133 | return { videoCreated } | |
134 | }) | |
135 | ||
3cabf353 C |
136 | Hooks.runAction('action:api.live-video.created', { video: videoCreated }) |
137 | ||
c6c0fa6c C |
138 | return res.json({ |
139 | video: { | |
140 | id: videoCreated.id, | |
141 | uuid: videoCreated.uuid | |
142 | } | |
143 | }) | |
144 | } |