]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video.ts
Support two factor authentication in backend
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
CommitLineData
f6d6e7f8 1import { UploadFiles } from 'express'
bd911b54 2import memoizee from 'memoizee'
1ef65f4c 3import { Transaction } from 'sequelize/types'
bd911b54 4import { CONFIG } from '@server/initializers/config'
aa2ce188 5import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY, MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants'
1ef65f4c 6import { TagModel } from '@server/models/video/tag'
c6c0fa6c 7import { VideoModel } from '@server/models/video/video'
0305db28 8import { VideoJobInfoModel } from '@server/models/video/video-job-info'
c6c0fa6c 9import { FilteredModelAttributes } from '@server/types'
764b1a14 10import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
1808a1f8 11import { ThumbnailType, VideoCreate, VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
b42c2c7e 12import { CreateJobOptions } from './job-queue/job-queue'
91f8f8db 13import { updateVideoMiniatureFromExisting } from './thumbnail'
c6c0fa6c 14
1ef65f4c 15function 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,
ba2684ce 29 channelId,
c6c0fa6c 30 originallyPublishedAt: videoInfo.originallyPublishedAt
16c016e8
C
31 ? new Date(videoInfo.originallyPublishedAt)
32 : null
c6c0fa6c
C
33 }
34}
35
1ef65f4c
C
36async 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
73async 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
bd911b54 89async function buildOptimizeOrMergeAudioJob (options: {
1808a1f8
C
90 video: MVideoUUID
91 videoFile: MVideoFile
92 user: MUserId
93 isNewVideo?: boolean // Default true
94}) {
95 const { video, videoFile, user, isNewVideo } = options
96
bd911b54 97 let payload: VideoTranscodingPayload
77d7e851
C
98
99 if (videoFile.isAudio()) {
bd911b54 100 payload = {
77d7e851
C
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 {
bd911b54 108 payload = {
77d7e851
C
109 type: 'optimize-to-webtorrent',
110 videoUUID: video.uuid,
c729caf6 111 isNewVideo
77d7e851
C
112 }
113 }
114
bd911b54 115 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
77d7e851 116
bd911b54
C
117 return {
118 type: 'video-transcoding' as 'video-transcoding',
119 priority: await getTranscodingJobPriority(user),
120 payload
121 }
0305db28
JB
122}
123
b42c2c7e 124async function buildTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
0305db28
JB
125 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
126
b42c2c7e 127 return { type: 'video-transcoding' as 'video-transcoding', payload, ...options }
0305db28
JB
128}
129
a6e37eeb 130async function getTranscodingJobPriority (user: MUserId) {
77d7e851
C
131 const now = new Date()
132 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
133
134 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
135
a6e37eeb 136 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
77d7e851
C
137}
138
c6c0fa6c
C
139// ---------------------------------------------------------------------------
140
bd911b54 141async function buildMoveToObjectStorageJob (options: {
1808a1f8
C
142 video: MVideoUUID
143 previousVideoState: VideoState
144 isNewVideo?: boolean // Default true
145}) {
146 const { video, previousVideoState, isNewVideo = true } = options
147
148 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
149
bd911b54
C
150 return {
151 type: 'move-to-object-storage' as 'move-to-object-storage',
152 payload: {
153 videoUUID: video.uuid,
154 isNewVideo,
155 previousVideoState
156 }
157 }
1808a1f8
C
158}
159
160// ---------------------------------------------------------------------------
161
aa2ce188
C
162async function getVideoDuration (videoId: number | string) {
163 const video = await VideoModel.load(videoId)
164
165 const duration = video.isLive
166 ? undefined
167 : video.duration
168
169 return { duration, isLive: video.isLive }
170}
171
172const getCachedVideoDuration = memoizee(getVideoDuration, {
173 promise: true,
174 max: MEMOIZE_LENGTH.VIDEO_DURATION,
175 maxAge: MEMOIZE_TTL.VIDEO_DURATION
176})
177
178// ---------------------------------------------------------------------------
179
c6c0fa6c 180export {
1ef65f4c
C
181 buildLocalVideoFromReq,
182 buildVideoThumbnailsFromReq,
77d7e851 183 setVideoTags,
bd911b54 184 buildOptimizeOrMergeAudioJob,
b42c2c7e 185 buildTranscodingJob,
bd911b54 186 buildMoveToObjectStorageJob,
aa2ce188
C
187 getTranscodingJobPriority,
188 getCachedVideoDuration
c6c0fa6c 189}