]>
Commit | Line | Data |
---|---|---|
f6d6e7f8 | 1 | import { UploadFiles } from 'express' |
1ef65f4c | 2 | import { Transaction } from 'sequelize/types' |
77d7e851 | 3 | import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } 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' |
77d7e851 | 9 | import { ThumbnailType, VideoCreate, VideoPrivacy, 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' |
c6c0fa6c | 13 | |
1ef65f4c | 14 | function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> { |
c6c0fa6c C |
15 | return { |
16 | name: videoInfo.name, | |
17 | remote: false, | |
18 | category: videoInfo.category, | |
3cf68b86 | 19 | licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE, |
c6c0fa6c | 20 | language: videoInfo.language, |
3cf68b86 C |
21 | commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED, |
22 | downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED, | |
c6c0fa6c | 23 | waitTranscoding: videoInfo.waitTranscoding || false, |
c6c0fa6c C |
24 | nsfw: videoInfo.nsfw || false, |
25 | description: videoInfo.description, | |
26 | support: videoInfo.support, | |
27 | privacy: videoInfo.privacy || VideoPrivacy.PRIVATE, | |
c6c0fa6c C |
28 | channelId: channelId, |
29 | originallyPublishedAt: videoInfo.originallyPublishedAt | |
16c016e8 C |
30 | ? new Date(videoInfo.originallyPublishedAt) |
31 | : null | |
c6c0fa6c C |
32 | } |
33 | } | |
34 | ||
1ef65f4c C |
35 | async function buildVideoThumbnailsFromReq (options: { |
36 | video: MVideoThumbnail | |
f6d6e7f8 | 37 | files: UploadFiles |
1ef65f4c C |
38 | fallback: (type: ThumbnailType) => Promise<MThumbnail> |
39 | automaticallyGenerated?: boolean | |
40 | }) { | |
41 | const { video, files, fallback, automaticallyGenerated } = options | |
42 | ||
43 | const promises = [ | |
44 | { | |
45 | type: ThumbnailType.MINIATURE, | |
46 | fieldName: 'thumbnailfile' | |
47 | }, | |
48 | { | |
49 | type: ThumbnailType.PREVIEW, | |
50 | fieldName: 'previewfile' | |
51 | } | |
52 | ].map(p => { | |
53 | const fields = files?.[p.fieldName] | |
54 | ||
55 | if (fields) { | |
91f8f8db | 56 | return updateVideoMiniatureFromExisting({ |
1ef65f4c C |
57 | inputPath: fields[0].path, |
58 | video, | |
59 | type: p.type, | |
60 | automaticallyGenerated: automaticallyGenerated || false | |
61 | }) | |
62 | } | |
63 | ||
64 | return fallback(p.type) | |
65 | }) | |
66 | ||
67 | return Promise.all(promises) | |
68 | } | |
69 | ||
70 | async function setVideoTags (options: { | |
71 | video: MVideoTag | |
72 | tags: string[] | |
73 | transaction?: Transaction | |
1ef65f4c | 74 | }) { |
6c9c3b7b | 75 | const { video, tags, transaction } = options |
1ef65f4c | 76 | |
6c9c3b7b C |
77 | const internalTags = tags || [] |
78 | const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction) | |
79 | ||
80 | await video.$set('Tags', tagInstances, { transaction }) | |
81 | video.Tags = tagInstances | |
1ef65f4c C |
82 | } |
83 | ||
764b1a14 | 84 | async function addOptimizeOrMergeAudioJob (video: MVideoUUID, videoFile: MVideoFile, user: MUserId) { |
77d7e851 C |
85 | let dataInput: VideoTranscodingPayload |
86 | ||
87 | if (videoFile.isAudio()) { | |
88 | dataInput = { | |
89 | type: 'merge-audio-to-webtorrent', | |
90 | resolution: DEFAULT_AUDIO_RESOLUTION, | |
91 | videoUUID: video.uuid, | |
92 | isNewVideo: true | |
93 | } | |
94 | } else { | |
95 | dataInput = { | |
96 | type: 'optimize-to-webtorrent', | |
97 | videoUUID: video.uuid, | |
98 | isNewVideo: true | |
99 | } | |
100 | } | |
101 | ||
102 | const jobOptions = { | |
a6e37eeb | 103 | priority: await getTranscodingJobPriority(user) |
77d7e851 C |
104 | } |
105 | ||
0305db28 JB |
106 | return addTranscodingJob(dataInput, jobOptions) |
107 | } | |
108 | ||
ad5db104 | 109 | async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) { |
0305db28 JB |
110 | await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode') |
111 | ||
112 | return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: payload }, options) | |
113 | } | |
114 | ||
115 | async function addMoveToObjectStorageJob (video: MVideoUUID, isNewVideo = true) { | |
116 | await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove') | |
117 | ||
118 | const dataInput = { videoUUID: video.uuid, isNewVideo } | |
119 | return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput }) | |
77d7e851 C |
120 | } |
121 | ||
a6e37eeb | 122 | async function getTranscodingJobPriority (user: MUserId) { |
77d7e851 C |
123 | const now = new Date() |
124 | const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7) | |
125 | ||
126 | const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek) | |
127 | ||
a6e37eeb | 128 | return JOB_PRIORITY.TRANSCODING + videoUploadedByUser |
77d7e851 C |
129 | } |
130 | ||
c6c0fa6c C |
131 | // --------------------------------------------------------------------------- |
132 | ||
133 | export { | |
1ef65f4c C |
134 | buildLocalVideoFromReq, |
135 | buildVideoThumbnailsFromReq, | |
77d7e851 C |
136 | setVideoTags, |
137 | addOptimizeOrMergeAudioJob, | |
0305db28 JB |
138 | addTranscodingJob, |
139 | addMoveToObjectStorageJob, | |
a6e37eeb | 140 | getTranscodingJobPriority |
c6c0fa6c | 141 | } |