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/lib/video.ts | |
parent | c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e (diff) | |
download | PeerTube-1ef65f4c034cc53ab5d55417e52d60e1f7fc1ddb.tar.gz PeerTube-1ef65f4c034cc53ab5d55417e52d60e1f7fc1ddb.tar.zst PeerTube-1ef65f4c034cc53ab5d55417e52d60e1f7fc1ddb.zip |
Refactor video creation
Diffstat (limited to 'server/lib/video.ts')
-rw-r--r-- | server/lib/video.ts | 68 |
1 files changed, 62 insertions, 6 deletions
diff --git a/server/lib/video.ts b/server/lib/video.ts index a28f31529..6df41e6cd 100644 --- a/server/lib/video.ts +++ b/server/lib/video.ts | |||
@@ -1,9 +1,12 @@ | |||
1 | 1 | import { Transaction } from 'sequelize/types' | |
2 | import { TagModel } from '@server/models/video/tag' | ||
2 | import { VideoModel } from '@server/models/video/video' | 3 | import { VideoModel } from '@server/models/video/video' |
3 | import { FilteredModelAttributes } from '@server/types' | 4 | import { FilteredModelAttributes } from '@server/types' |
4 | import { VideoCreate, VideoPrivacy, VideoState } from '@shared/models' | 5 | import { MTag, MThumbnail, MVideoTag, MVideoThumbnail } from '@server/types/models' |
6 | import { ThumbnailType, VideoCreate, VideoPrivacy } from '@shared/models' | ||
7 | import { createVideoMiniatureFromExisting } from './thumbnail' | ||
5 | 8 | ||
6 | function buildLocalVideoFromCreate (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> { | 9 | function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> { |
7 | return { | 10 | return { |
8 | name: videoInfo.name, | 11 | name: videoInfo.name, |
9 | remote: false, | 12 | remote: false, |
@@ -13,19 +16,72 @@ function buildLocalVideoFromCreate (videoInfo: VideoCreate, channelId: number): | |||
13 | commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true" | 16 | commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true" |
14 | downloadEnabled: videoInfo.downloadEnabled !== false, | 17 | downloadEnabled: videoInfo.downloadEnabled !== false, |
15 | waitTranscoding: videoInfo.waitTranscoding || false, | 18 | waitTranscoding: videoInfo.waitTranscoding || false, |
16 | state: VideoState.WAITING_FOR_LIVE, | ||
17 | nsfw: videoInfo.nsfw || false, | 19 | nsfw: videoInfo.nsfw || false, |
18 | description: videoInfo.description, | 20 | description: videoInfo.description, |
19 | support: videoInfo.support, | 21 | support: videoInfo.support, |
20 | privacy: videoInfo.privacy || VideoPrivacy.PRIVATE, | 22 | privacy: videoInfo.privacy || VideoPrivacy.PRIVATE, |
21 | duration: 0, | ||
22 | channelId: channelId, | 23 | channelId: channelId, |
23 | originallyPublishedAt: videoInfo.originallyPublishedAt | 24 | originallyPublishedAt: videoInfo.originallyPublishedAt |
24 | } | 25 | } |
25 | } | 26 | } |
26 | 27 | ||
28 | async function buildVideoThumbnailsFromReq (options: { | ||
29 | video: MVideoThumbnail | ||
30 | files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[] | ||
31 | fallback: (type: ThumbnailType) => Promise<MThumbnail> | ||
32 | automaticallyGenerated?: boolean | ||
33 | }) { | ||
34 | const { video, files, fallback, automaticallyGenerated } = options | ||
35 | |||
36 | const promises = [ | ||
37 | { | ||
38 | type: ThumbnailType.MINIATURE, | ||
39 | fieldName: 'thumbnailfile' | ||
40 | }, | ||
41 | { | ||
42 | type: ThumbnailType.PREVIEW, | ||
43 | fieldName: 'previewfile' | ||
44 | } | ||
45 | ].map(p => { | ||
46 | const fields = files?.[p.fieldName] | ||
47 | |||
48 | if (fields) { | ||
49 | return createVideoMiniatureFromExisting({ | ||
50 | inputPath: fields[0].path, | ||
51 | video, | ||
52 | type: p.type, | ||
53 | automaticallyGenerated: automaticallyGenerated || false | ||
54 | }) | ||
55 | } | ||
56 | |||
57 | return fallback(p.type) | ||
58 | }) | ||
59 | |||
60 | return Promise.all(promises) | ||
61 | } | ||
62 | |||
63 | async function setVideoTags (options: { | ||
64 | video: MVideoTag | ||
65 | tags: string[] | ||
66 | transaction?: Transaction | ||
67 | defaultValue?: MTag[] | ||
68 | }) { | ||
69 | const { video, tags, transaction, defaultValue } = options | ||
70 | // Set tags to the video | ||
71 | if (tags) { | ||
72 | const tagInstances = await TagModel.findOrCreateTags(tags, transaction) | ||
73 | |||
74 | await video.$set('Tags', tagInstances, { transaction }) | ||
75 | video.Tags = tagInstances | ||
76 | } else { | ||
77 | video.Tags = defaultValue || [] | ||
78 | } | ||
79 | } | ||
80 | |||
27 | // --------------------------------------------------------------------------- | 81 | // --------------------------------------------------------------------------- |
28 | 82 | ||
29 | export { | 83 | export { |
30 | buildLocalVideoFromCreate | 84 | buildLocalVideoFromReq, |
85 | buildVideoThumbnailsFromReq, | ||
86 | setVideoTags | ||
31 | } | 87 | } |