]>
Commit | Line | Data |
---|---|---|
f6d6e7f8 | 1 | import { UploadFiles } from 'express' |
1ef65f4c | 2 | import { Transaction } from 'sequelize/types' |
aa2ce188 | 3 | import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY, MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants' |
1ef65f4c | 4 | import { TagModel } from '@server/models/video/tag' |
c6c0fa6c | 5 | import { VideoModel } from '@server/models/video/video' |
0305db28 | 6 | import { VideoJobInfoModel } from '@server/models/video/video-job-info' |
c6c0fa6c | 7 | import { FilteredModelAttributes } from '@server/types' |
764b1a14 | 8 | import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models' |
1808a1f8 | 9 | import { ThumbnailType, VideoCreate, VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models' |
0305db28 | 10 | import { CreateJobOptions, JobQueue } from './job-queue/job-queue' |
91f8f8db | 11 | import { updateVideoMiniatureFromExisting } from './thumbnail' |
3cf68b86 | 12 | import { CONFIG } from '@server/initializers/config' |
aa2ce188 | 13 | import memoizee from 'memoizee' |
c6c0fa6c | 14 | |
1ef65f4c | 15 | function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> { |
c6c0fa6c C |
16 | return { |
17 | name: videoInfo.name, | |
18 | remote: false, | |
19 | category: videoInfo.category, | |
3cf68b86 | 20 | licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE, |
c6c0fa6c | 21 | language: videoInfo.language, |
3cf68b86 C |
22 | commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED, |
23 | downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED, | |
c6c0fa6c | 24 | waitTranscoding: videoInfo.waitTranscoding || false, |
c6c0fa6c C |
25 | nsfw: videoInfo.nsfw || false, |
26 | description: videoInfo.description, | |
27 | support: videoInfo.support, | |
28 | privacy: videoInfo.privacy || VideoPrivacy.PRIVATE, | |
c6c0fa6c C |
29 | channelId: channelId, |
30 | originallyPublishedAt: videoInfo.originallyPublishedAt | |
16c016e8 C |
31 | ? new Date(videoInfo.originallyPublishedAt) |
32 | : null | |
c6c0fa6c C |
33 | } |
34 | } | |
35 | ||
1ef65f4c C |
36 | async function buildVideoThumbnailsFromReq (options: { |
37 | video: MVideoThumbnail | |
f6d6e7f8 | 38 | files: UploadFiles |
1ef65f4c C |
39 | fallback: (type: ThumbnailType) => Promise<MThumbnail> |
40 | automaticallyGenerated?: boolean | |
41 | }) { | |
42 | const { video, files, fallback, automaticallyGenerated } = options | |
43 | ||
44 | const promises = [ | |
45 | { | |
46 | type: ThumbnailType.MINIATURE, | |
47 | fieldName: 'thumbnailfile' | |
48 | }, | |
49 | { | |
50 | type: ThumbnailType.PREVIEW, | |
51 | fieldName: 'previewfile' | |
52 | } | |
53 | ].map(p => { | |
54 | const fields = files?.[p.fieldName] | |
55 | ||
56 | if (fields) { | |
91f8f8db | 57 | return updateVideoMiniatureFromExisting({ |
1ef65f4c C |
58 | inputPath: fields[0].path, |
59 | video, | |
60 | type: p.type, | |
61 | automaticallyGenerated: automaticallyGenerated || false | |
62 | }) | |
63 | } | |
64 | ||
65 | return fallback(p.type) | |
66 | }) | |
67 | ||
68 | return Promise.all(promises) | |
69 | } | |
70 | ||
1808a1f8 C |
71 | // --------------------------------------------------------------------------- |
72 | ||
1ef65f4c C |
73 | async function setVideoTags (options: { |
74 | video: MVideoTag | |
75 | tags: string[] | |
76 | transaction?: Transaction | |
1ef65f4c | 77 | }) { |
6c9c3b7b | 78 | const { video, tags, transaction } = options |
1ef65f4c | 79 | |
6c9c3b7b C |
80 | const internalTags = tags || [] |
81 | const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction) | |
82 | ||
83 | await video.$set('Tags', tagInstances, { transaction }) | |
84 | video.Tags = tagInstances | |
1ef65f4c C |
85 | } |
86 | ||
1808a1f8 C |
87 | // --------------------------------------------------------------------------- |
88 | ||
89 | async function addOptimizeOrMergeAudioJob (options: { | |
90 | video: MVideoUUID | |
91 | videoFile: MVideoFile | |
92 | user: MUserId | |
93 | isNewVideo?: boolean // Default true | |
94 | }) { | |
95 | const { video, videoFile, user, isNewVideo } = options | |
96 | ||
77d7e851 C |
97 | let dataInput: VideoTranscodingPayload |
98 | ||
99 | if (videoFile.isAudio()) { | |
100 | dataInput = { | |
101 | type: 'merge-audio-to-webtorrent', | |
102 | resolution: DEFAULT_AUDIO_RESOLUTION, | |
103 | videoUUID: video.uuid, | |
0f11ec8d | 104 | createHLSIfNeeded: true, |
c729caf6 | 105 | isNewVideo |
77d7e851 C |
106 | } |
107 | } else { | |
108 | dataInput = { | |
109 | type: 'optimize-to-webtorrent', | |
110 | videoUUID: video.uuid, | |
c729caf6 | 111 | isNewVideo |
77d7e851 C |
112 | } |
113 | } | |
114 | ||
115 | const jobOptions = { | |
a6e37eeb | 116 | priority: await getTranscodingJobPriority(user) |
77d7e851 C |
117 | } |
118 | ||
0305db28 JB |
119 | return addTranscodingJob(dataInput, jobOptions) |
120 | } | |
121 | ||
ad5db104 | 122 | async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) { |
0305db28 JB |
123 | await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode') |
124 | ||
125 | return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: payload }, options) | |
126 | } | |
127 | ||
a6e37eeb | 128 | async function getTranscodingJobPriority (user: MUserId) { |
77d7e851 C |
129 | const now = new Date() |
130 | const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7) | |
131 | ||
132 | const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek) | |
133 | ||
a6e37eeb | 134 | return JOB_PRIORITY.TRANSCODING + videoUploadedByUser |
77d7e851 C |
135 | } |
136 | ||
c6c0fa6c C |
137 | // --------------------------------------------------------------------------- |
138 | ||
1808a1f8 C |
139 | async function addMoveToObjectStorageJob (options: { |
140 | video: MVideoUUID | |
141 | previousVideoState: VideoState | |
142 | isNewVideo?: boolean // Default true | |
143 | }) { | |
144 | const { video, previousVideoState, isNewVideo = true } = options | |
145 | ||
146 | await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove') | |
147 | ||
148 | const dataInput = { videoUUID: video.uuid, isNewVideo, previousVideoState } | |
149 | return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput }) | |
150 | } | |
151 | ||
152 | // --------------------------------------------------------------------------- | |
153 | ||
aa2ce188 C |
154 | async function getVideoDuration (videoId: number | string) { |
155 | const video = await VideoModel.load(videoId) | |
156 | ||
157 | const duration = video.isLive | |
158 | ? undefined | |
159 | : video.duration | |
160 | ||
161 | return { duration, isLive: video.isLive } | |
162 | } | |
163 | ||
164 | const getCachedVideoDuration = memoizee(getVideoDuration, { | |
165 | promise: true, | |
166 | max: MEMOIZE_LENGTH.VIDEO_DURATION, | |
167 | maxAge: MEMOIZE_TTL.VIDEO_DURATION | |
168 | }) | |
169 | ||
170 | // --------------------------------------------------------------------------- | |
171 | ||
c6c0fa6c | 172 | export { |
1ef65f4c C |
173 | buildLocalVideoFromReq, |
174 | buildVideoThumbnailsFromReq, | |
77d7e851 C |
175 | setVideoTags, |
176 | addOptimizeOrMergeAudioJob, | |
0305db28 JB |
177 | addTranscodingJob, |
178 | addMoveToObjectStorageJob, | |
aa2ce188 C |
179 | getTranscodingJobPriority, |
180 | getCachedVideoDuration | |
c6c0fa6c | 181 | } |