]>
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' | |
1ef65f4c | 7 | import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' |
c6c0fa6c C |
8 | import { videoLiveAddValidator, videoLiveGetValidator } from '@server/middlewares/validators/videos/video-live' |
9 | import { VideoLiveModel } from '@server/models/video/video-live' | |
10 | import { MVideoDetails, MVideoFullLight } from '@server/types/models' | |
1ef65f4c | 11 | import { VideoCreate, VideoState } from '../../../../shared' |
c6c0fa6c C |
12 | import { logger } from '../../../helpers/logger' |
13 | import { sequelizeTypescript } from '../../../initializers/database' | |
14 | import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail' | |
15 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' | |
c6c0fa6c | 16 | import { VideoModel } from '../../../models/video/video' |
c6c0fa6c C |
17 | |
18 | const liveRouter = express.Router() | |
19 | ||
20 | const reqVideoFileLive = createReqFiles( | |
21 | [ 'thumbnailfile', 'previewfile' ], | |
22 | MIMETYPES.IMAGE.MIMETYPE_EXT, | |
23 | { | |
24 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, | |
25 | previewfile: CONFIG.STORAGE.TMP_DIR | |
26 | } | |
27 | ) | |
28 | ||
29 | liveRouter.post('/live', | |
30 | authenticate, | |
31 | reqVideoFileLive, | |
32 | asyncMiddleware(videoLiveAddValidator), | |
33 | asyncRetryTransactionMiddleware(addLiveVideo) | |
34 | ) | |
35 | ||
36 | liveRouter.get('/live/:videoId', | |
37 | authenticate, | |
38 | asyncMiddleware(videoLiveGetValidator), | |
39 | asyncRetryTransactionMiddleware(getVideoLive) | |
40 | ) | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | export { | |
45 | liveRouter | |
46 | } | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | async function getVideoLive (req: express.Request, res: express.Response) { | |
51 | const videoLive = res.locals.videoLive | |
52 | ||
53 | return res.json(videoLive.toFormattedJSON()) | |
54 | } | |
55 | ||
56 | async function addLiveVideo (req: express.Request, res: express.Response) { | |
57 | const videoInfo: VideoCreate = req.body | |
58 | ||
59 | // Prepare data so we don't block the transaction | |
1ef65f4c | 60 | const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id) |
c6c0fa6c | 61 | videoData.isLive = true |
1ef65f4c C |
62 | videoData.state = VideoState.WAITING_FOR_LIVE |
63 | videoData.duration = 0 | |
c6c0fa6c C |
64 | |
65 | const video = new VideoModel(videoData) as MVideoDetails | |
66 | video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object | |
67 | ||
1ef65f4c C |
68 | const videoLive = new VideoLiveModel() |
69 | videoLive.streamKey = uuidv4() | |
c6c0fa6c | 70 | |
1ef65f4c C |
71 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ |
72 | video, | |
73 | files: req.files, | |
74 | fallback: type => { | |
75 | return createVideoMiniatureFromExisting({ inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, video, type, automaticallyGenerated: true }) | |
76 | } | |
77 | }) | |
c6c0fa6c C |
78 | |
79 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { | |
80 | const sequelizeOptions = { transaction: t } | |
81 | ||
82 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight | |
83 | ||
84 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) | |
85 | if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t) | |
86 | ||
87 | // Do not forget to add video channel information to the created video | |
88 | videoCreated.VideoChannel = res.locals.videoChannel | |
89 | ||
90 | videoLive.videoId = videoCreated.id | |
91 | await videoLive.save(sequelizeOptions) | |
92 | ||
1ef65f4c | 93 | await setVideoTags({ video, tags: videoInfo.tags, transaction: t }) |
c6c0fa6c C |
94 | |
95 | logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid) | |
96 | ||
97 | return { videoCreated } | |
98 | }) | |
99 | ||
100 | return res.json({ | |
101 | video: { | |
102 | id: videoCreated.id, | |
103 | uuid: videoCreated.uuid | |
104 | } | |
105 | }) | |
106 | } |