]>
Commit | Line | Data |
---|---|---|
1 | import { UploadFiles } from 'express' | |
2 | import { Transaction } from 'sequelize/types' | |
3 | import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants' | |
4 | import { TagModel } from '@server/models/video/tag' | |
5 | import { VideoModel } from '@server/models/video/video' | |
6 | import { VideoJobInfoModel } from '@server/models/video/video-job-info' | |
7 | import { FilteredModelAttributes } from '@server/types' | |
8 | import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models' | |
9 | import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models' | |
10 | import { CreateJobOptions, JobQueue } from './job-queue/job-queue' | |
11 | import { updateVideoMiniatureFromExisting } from './thumbnail' | |
12 | ||
13 | function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> { | |
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, | |
23 | nsfw: videoInfo.nsfw || false, | |
24 | description: videoInfo.description, | |
25 | support: videoInfo.support, | |
26 | privacy: videoInfo.privacy || VideoPrivacy.PRIVATE, | |
27 | channelId: channelId, | |
28 | originallyPublishedAt: videoInfo.originallyPublishedAt | |
29 | ? new Date(videoInfo.originallyPublishedAt) | |
30 | : null | |
31 | } | |
32 | } | |
33 | ||
34 | async function buildVideoThumbnailsFromReq (options: { | |
35 | video: MVideoThumbnail | |
36 | files: UploadFiles | |
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) { | |
55 | return updateVideoMiniatureFromExisting({ | |
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 | |
73 | }) { | |
74 | const { video, tags, transaction } = options | |
75 | ||
76 | const internalTags = tags || [] | |
77 | const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction) | |
78 | ||
79 | await video.$set('Tags', tagInstances, { transaction }) | |
80 | video.Tags = tagInstances | |
81 | } | |
82 | ||
83 | async function addOptimizeOrMergeAudioJob (video: MVideoUUID, videoFile: MVideoFile, user: MUserId) { | |
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 = { | |
102 | priority: await getTranscodingJobPriority(user) | |
103 | } | |
104 | ||
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 }) | |
119 | } | |
120 | ||
121 | async function getTranscodingJobPriority (user: MUserId) { | |
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 | ||
127 | return JOB_PRIORITY.TRANSCODING + videoUploadedByUser | |
128 | } | |
129 | ||
130 | // --------------------------------------------------------------------------- | |
131 | ||
132 | export { | |
133 | buildLocalVideoFromReq, | |
134 | buildVideoThumbnailsFromReq, | |
135 | setVideoTags, | |
136 | addOptimizeOrMergeAudioJob, | |
137 | addTranscodingJob, | |
138 | addMoveToObjectStorageJob, | |
139 | getTranscodingJobPriority | |
140 | } |