]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video.ts
Refactor video links building
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
CommitLineData
f6d6e7f8 1import { UploadFiles } from 'express'
1ef65f4c 2import { Transaction } from 'sequelize/types'
77d7e851 3import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
d846d99c 4import { sequelizeTypescript } from '@server/initializers/database'
1ef65f4c 5import { TagModel } from '@server/models/video/tag'
c6c0fa6c
C
6import { VideoModel } from '@server/models/video/video'
7import { FilteredModelAttributes } from '@server/types'
764b1a14 8import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
77d7e851 9import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
d846d99c 10import { federateVideoIfNeeded } from './activitypub/videos'
77d7e851 11import { JobQueue } from './job-queue/job-queue'
d846d99c 12import { Notifier } from './notifier'
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,
20 licence: videoInfo.licence,
21 language: videoInfo.language,
22 commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true"
23 downloadEnabled: videoInfo.downloadEnabled !== false,
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,
c6c0fa6c
C
29 channelId: channelId,
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
71async function setVideoTags (options: {
72 video: MVideoTag
73 tags: string[]
74 transaction?: Transaction
1ef65f4c 75}) {
6c9c3b7b 76 const { video, tags, transaction } = options
1ef65f4c 77
6c9c3b7b
C
78 const internalTags = tags || []
79 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
80
81 await video.$set('Tags', tagInstances, { transaction })
82 video.Tags = tagInstances
1ef65f4c
C
83}
84
97969c4e
C
85async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) {
86 const result = await sequelizeTypescript.transaction(async t => {
d846d99c
C
87 // Maybe the video changed in database, refresh it
88 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
89 // Video does not exist anymore
90 if (!videoDatabase) return undefined
91
92 // We transcoded the video file in another format, now we can publish it
93 const videoPublished = await videoDatabase.publishIfNeededAndSave(t)
94
95 // If the video was not published, we consider it is a new one for other instances
97969c4e
C
96 // Live videos are always federated, so it's not a new video
97 await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t)
d846d99c
C
98
99 return { videoDatabase, videoPublished }
100 })
101
97969c4e
C
102 if (result?.videoPublished) {
103 Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase)
104 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase)
d846d99c
C
105 }
106}
107
764b1a14 108async function addOptimizeOrMergeAudioJob (video: MVideoUUID, videoFile: MVideoFile, user: MUserId) {
77d7e851
C
109 let dataInput: VideoTranscodingPayload
110
111 if (videoFile.isAudio()) {
112 dataInput = {
113 type: 'merge-audio-to-webtorrent',
114 resolution: DEFAULT_AUDIO_RESOLUTION,
115 videoUUID: video.uuid,
116 isNewVideo: true
117 }
118 } else {
119 dataInput = {
120 type: 'optimize-to-webtorrent',
121 videoUUID: video.uuid,
122 isNewVideo: true
123 }
124 }
125
126 const jobOptions = {
a6e37eeb 127 priority: await getTranscodingJobPriority(user)
77d7e851
C
128 }
129
130 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }, jobOptions)
131}
132
a6e37eeb 133async function getTranscodingJobPriority (user: MUserId) {
77d7e851
C
134 const now = new Date()
135 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
136
137 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
138
a6e37eeb 139 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
77d7e851
C
140}
141
c6c0fa6c
C
142// ---------------------------------------------------------------------------
143
144export {
1ef65f4c 145 buildLocalVideoFromReq,
d846d99c 146 publishAndFederateIfNeeded,
1ef65f4c 147 buildVideoThumbnailsFromReq,
77d7e851
C
148 setVideoTags,
149 addOptimizeOrMergeAudioJob,
a6e37eeb 150 getTranscodingJobPriority
c6c0fa6c 151}