]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize/types' | |
2 | import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants' | |
3 | import { sequelizeTypescript } from '@server/initializers/database' | |
4 | import { TagModel } from '@server/models/video/tag' | |
5 | import { VideoModel } from '@server/models/video/video' | |
6 | import { FilteredModelAttributes } from '@server/types' | |
7 | import { MTag, MThumbnail, MUserId, MVideo, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models' | |
8 | import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models' | |
9 | import { federateVideoIfNeeded } from './activitypub/videos' | |
10 | import { JobQueue } from './job-queue/job-queue' | |
11 | import { Notifier } from './notifier' | |
12 | import { createVideoMiniatureFromExisting } from './thumbnail' | |
13 | ||
14 | function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> { | |
15 | return { | |
16 | name: videoInfo.name, | |
17 | remote: false, | |
18 | category: videoInfo.category, | |
19 | licence: videoInfo.licence, | |
20 | language: videoInfo.language, | |
21 | commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true" | |
22 | downloadEnabled: videoInfo.downloadEnabled !== false, | |
23 | waitTranscoding: videoInfo.waitTranscoding || false, | |
24 | nsfw: videoInfo.nsfw || false, | |
25 | description: videoInfo.description, | |
26 | support: videoInfo.support, | |
27 | privacy: videoInfo.privacy || VideoPrivacy.PRIVATE, | |
28 | channelId: channelId, | |
29 | originallyPublishedAt: videoInfo.originallyPublishedAt | |
30 | } | |
31 | } | |
32 | ||
33 | async function buildVideoThumbnailsFromReq (options: { | |
34 | video: MVideoThumbnail | |
35 | files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[] | |
36 | fallback: (type: ThumbnailType) => Promise<MThumbnail> | |
37 | automaticallyGenerated?: boolean | |
38 | }) { | |
39 | const { video, files, fallback, automaticallyGenerated } = options | |
40 | ||
41 | const promises = [ | |
42 | { | |
43 | type: ThumbnailType.MINIATURE, | |
44 | fieldName: 'thumbnailfile' | |
45 | }, | |
46 | { | |
47 | type: ThumbnailType.PREVIEW, | |
48 | fieldName: 'previewfile' | |
49 | } | |
50 | ].map(p => { | |
51 | const fields = files?.[p.fieldName] | |
52 | ||
53 | if (fields) { | |
54 | return createVideoMiniatureFromExisting({ | |
55 | inputPath: fields[0].path, | |
56 | video, | |
57 | type: p.type, | |
58 | automaticallyGenerated: automaticallyGenerated || false | |
59 | }) | |
60 | } | |
61 | ||
62 | return fallback(p.type) | |
63 | }) | |
64 | ||
65 | return Promise.all(promises) | |
66 | } | |
67 | ||
68 | async function setVideoTags (options: { | |
69 | video: MVideoTag | |
70 | tags: string[] | |
71 | transaction?: Transaction | |
72 | defaultValue?: MTag[] | |
73 | }) { | |
74 | const { video, tags, transaction, defaultValue } = options | |
75 | // Set tags to the video | |
76 | if (tags) { | |
77 | const tagInstances = await TagModel.findOrCreateTags(tags, transaction) | |
78 | ||
79 | await video.$set('Tags', tagInstances, { transaction }) | |
80 | video.Tags = tagInstances | |
81 | } else { | |
82 | video.Tags = defaultValue || [] | |
83 | } | |
84 | } | |
85 | ||
86 | async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) { | |
87 | const result = await sequelizeTypescript.transaction(async t => { | |
88 | // Maybe the video changed in database, refresh it | |
89 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | |
90 | // Video does not exist anymore | |
91 | if (!videoDatabase) return undefined | |
92 | ||
93 | // We transcoded the video file in another format, now we can publish it | |
94 | const videoPublished = await videoDatabase.publishIfNeededAndSave(t) | |
95 | ||
96 | // If the video was not published, we consider it is a new one for other instances | |
97 | // Live videos are always federated, so it's not a new video | |
98 | await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t) | |
99 | ||
100 | return { videoDatabase, videoPublished } | |
101 | }) | |
102 | ||
103 | if (result?.videoPublished) { | |
104 | Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase) | |
105 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase) | |
106 | } | |
107 | } | |
108 | ||
109 | async function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile, user: MUserId) { | |
110 | let dataInput: VideoTranscodingPayload | |
111 | ||
112 | if (videoFile.isAudio()) { | |
113 | dataInput = { | |
114 | type: 'merge-audio-to-webtorrent', | |
115 | resolution: DEFAULT_AUDIO_RESOLUTION, | |
116 | videoUUID: video.uuid, | |
117 | isNewVideo: true | |
118 | } | |
119 | } else { | |
120 | dataInput = { | |
121 | type: 'optimize-to-webtorrent', | |
122 | videoUUID: video.uuid, | |
123 | isNewVideo: true | |
124 | } | |
125 | } | |
126 | ||
127 | const jobOptions = { | |
128 | priority: JOB_PRIORITY.TRANSCODING.OPTIMIZER + await getJobTranscodingPriorityMalus(user) | |
129 | } | |
130 | ||
131 | return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }, jobOptions) | |
132 | } | |
133 | ||
134 | async function getJobTranscodingPriorityMalus (user: MUserId) { | |
135 | const now = new Date() | |
136 | const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7) | |
137 | ||
138 | const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek) | |
139 | ||
140 | return videoUploadedByUser | |
141 | } | |
142 | ||
143 | // --------------------------------------------------------------------------- | |
144 | ||
145 | export { | |
146 | buildLocalVideoFromReq, | |
147 | publishAndFederateIfNeeded, | |
148 | buildVideoThumbnailsFromReq, | |
149 | setVideoTags, | |
150 | addOptimizeOrMergeAudioJob, | |
151 | getJobTranscodingPriorityMalus | |
152 | } |