diff options
author | Chocobozzz <me@florianbigard.com> | 2020-09-17 10:00:46 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-11-09 15:33:04 +0100 |
commit | 1ef65f4c034cc53ab5d55417e52d60e1f7fc1ddb (patch) | |
tree | 8bb02f8dc2590e5071306fb311bdc53289e20336 /server/controllers/api/videos/live.ts | |
parent | c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e (diff) | |
download | PeerTube-1ef65f4c034cc53ab5d55417e52d60e1f7fc1ddb.tar.gz PeerTube-1ef65f4c034cc53ab5d55417e52d60e1f7fc1ddb.tar.zst PeerTube-1ef65f4c034cc53ab5d55417e52d60e1f7fc1ddb.zip |
Refactor video creation
Diffstat (limited to 'server/controllers/api/videos/live.ts')
-rw-r--r-- | server/controllers/api/videos/live.ts | 40 |
1 files changed, 15 insertions, 25 deletions
diff --git a/server/controllers/api/videos/live.ts b/server/controllers/api/videos/live.ts index d08ef9869..97b135f96 100644 --- a/server/controllers/api/videos/live.ts +++ b/server/controllers/api/videos/live.ts | |||
@@ -4,18 +4,16 @@ import { createReqFiles } from '@server/helpers/express-utils' | |||
4 | import { CONFIG } from '@server/initializers/config' | 4 | import { CONFIG } from '@server/initializers/config' |
5 | import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants' | 5 | import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants' |
6 | import { getVideoActivityPubUrl } from '@server/lib/activitypub/url' | 6 | import { getVideoActivityPubUrl } from '@server/lib/activitypub/url' |
7 | import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' | ||
7 | import { videoLiveAddValidator, videoLiveGetValidator } from '@server/middlewares/validators/videos/video-live' | 8 | import { videoLiveAddValidator, videoLiveGetValidator } from '@server/middlewares/validators/videos/video-live' |
8 | import { VideoLiveModel } from '@server/models/video/video-live' | 9 | import { VideoLiveModel } from '@server/models/video/video-live' |
9 | import { MVideoDetails, MVideoFullLight } from '@server/types/models' | 10 | import { MVideoDetails, MVideoFullLight } from '@server/types/models' |
10 | import { VideoCreate, VideoPrivacy, VideoState } from '../../../../shared' | 11 | import { VideoCreate, VideoState } from '../../../../shared' |
11 | import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' | ||
12 | import { logger } from '../../../helpers/logger' | 12 | import { logger } from '../../../helpers/logger' |
13 | import { sequelizeTypescript } from '../../../initializers/database' | 13 | import { sequelizeTypescript } from '../../../initializers/database' |
14 | import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail' | 14 | import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail' |
15 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' | 15 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' |
16 | import { TagModel } from '../../../models/video/tag' | ||
17 | import { VideoModel } from '../../../models/video/video' | 16 | import { VideoModel } from '../../../models/video/video' |
18 | import { buildLocalVideoFromCreate } from '@server/lib/video' | ||
19 | 17 | ||
20 | const liveRouter = express.Router() | 18 | const liveRouter = express.Router() |
21 | 19 | ||
@@ -59,26 +57,24 @@ async function addLiveVideo (req: express.Request, res: express.Response) { | |||
59 | const videoInfo: VideoCreate = req.body | 57 | const videoInfo: VideoCreate = req.body |
60 | 58 | ||
61 | // Prepare data so we don't block the transaction | 59 | // Prepare data so we don't block the transaction |
62 | const videoData = buildLocalVideoFromCreate(videoInfo, res.locals.videoChannel.id) | 60 | const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id) |
63 | videoData.isLive = true | 61 | videoData.isLive = true |
64 | 62 | videoData.state = VideoState.WAITING_FOR_LIVE | |
65 | const videoLive = new VideoLiveModel() | 63 | videoData.duration = 0 |
66 | videoLive.streamKey = uuidv4() | ||
67 | 64 | ||
68 | const video = new VideoModel(videoData) as MVideoDetails | 65 | const video = new VideoModel(videoData) as MVideoDetails |
69 | video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object | 66 | video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object |
70 | 67 | ||
71 | // Process thumbnail or create it from the video | 68 | const videoLive = new VideoLiveModel() |
72 | const thumbnailField = req.files ? req.files['thumbnailfile'] : null | 69 | videoLive.streamKey = uuidv4() |
73 | const thumbnailModel = thumbnailField | ||
74 | ? await createVideoMiniatureFromExisting(thumbnailField[0].path, video, ThumbnailType.MINIATURE, false) | ||
75 | : await createVideoMiniatureFromExisting(ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, video, ThumbnailType.MINIATURE, true) | ||
76 | 70 | ||
77 | // Process preview or create it from the video | 71 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ |
78 | const previewField = req.files ? req.files['previewfile'] : null | 72 | video, |
79 | const previewModel = previewField | 73 | files: req.files, |
80 | ? await createVideoMiniatureFromExisting(previewField[0].path, video, ThumbnailType.PREVIEW, false) | 74 | fallback: type => { |
81 | : await createVideoMiniatureFromExisting(ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, video, ThumbnailType.PREVIEW, true) | 75 | return createVideoMiniatureFromExisting({ inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, video, type, automaticallyGenerated: true }) |
76 | } | ||
77 | }) | ||
82 | 78 | ||
83 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { | 79 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { |
84 | const sequelizeOptions = { transaction: t } | 80 | const sequelizeOptions = { transaction: t } |
@@ -94,13 +90,7 @@ async function addLiveVideo (req: express.Request, res: express.Response) { | |||
94 | videoLive.videoId = videoCreated.id | 90 | videoLive.videoId = videoCreated.id |
95 | await videoLive.save(sequelizeOptions) | 91 | await videoLive.save(sequelizeOptions) |
96 | 92 | ||
97 | // Create tags | 93 | await setVideoTags({ video, tags: videoInfo.tags, transaction: t }) |
98 | if (videoInfo.tags !== undefined) { | ||
99 | const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t) | ||
100 | |||
101 | await video.$set('Tags', tagInstances, sequelizeOptions) | ||
102 | video.Tags = tagInstances | ||
103 | } | ||
104 | 94 | ||
105 | logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid) | 95 | logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid) |
106 | 96 | ||