]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video.ts
Fix E2E tests
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
CommitLineData
1ef65f4c 1import { Transaction } from 'sequelize/types'
77d7e851 2import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
d846d99c 3import { sequelizeTypescript } from '@server/initializers/database'
1ef65f4c 4import { TagModel } from '@server/models/video/tag'
c6c0fa6c
C
5import { VideoModel } from '@server/models/video/video'
6import { FilteredModelAttributes } from '@server/types'
6c9c3b7b 7import { MThumbnail, MUserId, MVideo, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
77d7e851 8import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
d846d99c 9import { federateVideoIfNeeded } from './activitypub/videos'
77d7e851 10import { JobQueue } from './job-queue/job-queue'
d846d99c 11import { Notifier } from './notifier'
1ef65f4c 12import { createVideoMiniatureFromExisting } from './thumbnail'
c6c0fa6c 13
1ef65f4c 14function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
c6c0fa6c
C
15 return {
16 name: videoInfo.name,
17 remote: false,
18 category: videoInfo.category,
19 licence: videoInfo.licence,
20 language: videoInfo.language,
21 commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true"
22 downloadEnabled: videoInfo.downloadEnabled !== false,
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
30 }
31}
32
1ef65f4c
C
33async function buildVideoThumbnailsFromReq (options: {
34 video: MVideoThumbnail
35 files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[]
36 fallback: (type: ThumbnailType) => Promise<MThumbnail>
37 automaticallyGenerated?: boolean
38}) {
39 const { video, files, fallback, automaticallyGenerated } = options
40
41 const promises = [
42 {
43 type: ThumbnailType.MINIATURE,
44 fieldName: 'thumbnailfile'
45 },
46 {
47 type: ThumbnailType.PREVIEW,
48 fieldName: 'previewfile'
49 }
50 ].map(p => {
51 const fields = files?.[p.fieldName]
52
53 if (fields) {
54 return createVideoMiniatureFromExisting({
55 inputPath: fields[0].path,
56 video,
57 type: p.type,
58 automaticallyGenerated: automaticallyGenerated || false
59 })
60 }
61
62 return fallback(p.type)
63 })
64
65 return Promise.all(promises)
66}
67
68async function setVideoTags (options: {
69 video: MVideoTag
70 tags: string[]
71 transaction?: Transaction
1ef65f4c 72}) {
6c9c3b7b 73 const { video, tags, transaction } = options
1ef65f4c 74
6c9c3b7b
C
75 const internalTags = tags || []
76 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
77
78 await video.$set('Tags', tagInstances, { transaction })
79 video.Tags = tagInstances
1ef65f4c
C
80}
81
97969c4e
C
82async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) {
83 const result = await sequelizeTypescript.transaction(async t => {
d846d99c
C
84 // Maybe the video changed in database, refresh it
85 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
86 // Video does not exist anymore
87 if (!videoDatabase) return undefined
88
89 // We transcoded the video file in another format, now we can publish it
90 const videoPublished = await videoDatabase.publishIfNeededAndSave(t)
91
92 // If the video was not published, we consider it is a new one for other instances
97969c4e
C
93 // Live videos are always federated, so it's not a new video
94 await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t)
d846d99c
C
95
96 return { videoDatabase, videoPublished }
97 })
98
97969c4e
C
99 if (result?.videoPublished) {
100 Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase)
101 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase)
d846d99c
C
102 }
103}
104
77d7e851
C
105async function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile, user: MUserId) {
106 let dataInput: VideoTranscodingPayload
107
108 if (videoFile.isAudio()) {
109 dataInput = {
110 type: 'merge-audio-to-webtorrent',
111 resolution: DEFAULT_AUDIO_RESOLUTION,
112 videoUUID: video.uuid,
113 isNewVideo: true
114 }
115 } else {
116 dataInput = {
117 type: 'optimize-to-webtorrent',
118 videoUUID: video.uuid,
119 isNewVideo: true
120 }
121 }
122
123 const jobOptions = {
124 priority: JOB_PRIORITY.TRANSCODING.OPTIMIZER + await getJobTranscodingPriorityMalus(user)
125 }
126
127 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }, jobOptions)
128}
129
130async function getJobTranscodingPriorityMalus (user: MUserId) {
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
136 return videoUploadedByUser
137}
138
c6c0fa6c
C
139// ---------------------------------------------------------------------------
140
141export {
1ef65f4c 142 buildLocalVideoFromReq,
d846d99c 143 publishAndFederateIfNeeded,
1ef65f4c 144 buildVideoThumbnailsFromReq,
77d7e851
C
145 setVideoTags,
146 addOptimizeOrMergeAudioJob,
147 getJobTranscodingPriorityMalus
c6c0fa6c 148}