]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video.ts
createThumbnail -> updateThumbnail
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
1 import { UploadFiles } from 'express'
2 import { Transaction } from 'sequelize/types'
3 import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
4 import { sequelizeTypescript } from '@server/initializers/database'
5 import { TagModel } from '@server/models/video/tag'
6 import { VideoModel } from '@server/models/video/video'
7 import { FilteredModelAttributes } from '@server/types'
8 import { MThumbnail, MUserId, MVideo, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
9 import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
10 import { federateVideoIfNeeded } from './activitypub/videos'
11 import { JobQueue } from './job-queue/job-queue'
12 import { Notifier } from './notifier'
13 import { updateVideoMiniatureFromExisting } from './thumbnail'
14
15 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
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,
25 nsfw: videoInfo.nsfw || false,
26 description: videoInfo.description,
27 support: videoInfo.support,
28 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
29 channelId: channelId,
30 originallyPublishedAt: videoInfo.originallyPublishedAt
31 ? new Date(videoInfo.originallyPublishedAt)
32 : null
33 }
34 }
35
36 async function buildVideoThumbnailsFromReq (options: {
37 video: MVideoThumbnail
38 files: UploadFiles
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) {
57 return updateVideoMiniatureFromExisting({
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
71 async function setVideoTags (options: {
72 video: MVideoTag
73 tags: string[]
74 transaction?: Transaction
75 }) {
76 const { video, tags, transaction } = options
77
78 const internalTags = tags || []
79 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
80
81 await video.$set('Tags', tagInstances, { transaction })
82 video.Tags = tagInstances
83 }
84
85 async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) {
86 const result = await sequelizeTypescript.transaction(async t => {
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
96 // Live videos are always federated, so it's not a new video
97 await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t)
98
99 return { videoDatabase, videoPublished }
100 })
101
102 if (result?.videoPublished) {
103 Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase)
104 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase)
105 }
106 }
107
108 async function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile, user: MUserId) {
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 = {
127 priority: await getTranscodingJobPriority(user)
128 }
129
130 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }, jobOptions)
131 }
132
133 async function getTranscodingJobPriority (user: MUserId) {
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
139 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
140 }
141
142 // ---------------------------------------------------------------------------
143
144 export {
145 buildLocalVideoFromReq,
146 publishAndFederateIfNeeded,
147 buildVideoThumbnailsFromReq,
148 setVideoTags,
149 addOptimizeOrMergeAudioJob,
150 getTranscodingJobPriority
151 }