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