2021-05-10 11:13:41 +02:00
|
|
|
import { UploadFiles } from 'express'
|
2020-09-17 10:00:46 +02:00
|
|
|
import { Transaction } from 'sequelize/types'
|
2021-01-21 16:57:21 +01:00
|
|
|
import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
|
2020-09-17 10:00:46 +02:00
|
|
|
import { TagModel } from '@server/models/video/tag'
|
2020-09-17 09:20:52 +02:00
|
|
|
import { VideoModel } from '@server/models/video/video'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info'
|
2020-09-17 09:20:52 +02:00
|
|
|
import { FilteredModelAttributes } from '@server/types'
|
2021-07-23 11:20:00 +02:00
|
|
|
import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
|
2021-01-21 16:57:21 +01:00
|
|
|
import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { CreateJobOptions, JobQueue } from './job-queue/job-queue'
|
2021-06-04 09:19:01 +02:00
|
|
|
import { updateVideoMiniatureFromExisting } from './thumbnail'
|
2021-12-14 17:17:01 +01:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2020-09-17 09:20:52 +02:00
|
|
|
|
2020-09-17 10:00:46 +02:00
|
|
|
function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
|
2020-09-17 09:20:52 +02:00
|
|
|
return {
|
|
|
|
name: videoInfo.name,
|
|
|
|
remote: false,
|
|
|
|
category: videoInfo.category,
|
2021-12-14 17:17:01 +01:00
|
|
|
licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
|
2020-09-17 09:20:52 +02:00
|
|
|
language: videoInfo.language,
|
2021-12-14 17:17:01 +01:00
|
|
|
commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
|
|
|
|
downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
|
2020-09-17 09:20:52 +02:00
|
|
|
waitTranscoding: videoInfo.waitTranscoding || false,
|
|
|
|
nsfw: videoInfo.nsfw || false,
|
|
|
|
description: videoInfo.description,
|
|
|
|
support: videoInfo.support,
|
|
|
|
privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
|
|
|
|
channelId: channelId,
|
|
|
|
originallyPublishedAt: videoInfo.originallyPublishedAt
|
2021-05-12 14:09:04 +02:00
|
|
|
? new Date(videoInfo.originallyPublishedAt)
|
|
|
|
: null
|
2020-09-17 09:20:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:00:46 +02:00
|
|
|
async function buildVideoThumbnailsFromReq (options: {
|
|
|
|
video: MVideoThumbnail
|
2021-05-10 11:13:41 +02:00
|
|
|
files: UploadFiles
|
2020-09-17 10:00:46 +02:00
|
|
|
fallback: (type: ThumbnailType) => Promise<MThumbnail>
|
|
|
|
automaticallyGenerated?: boolean
|
|
|
|
}) {
|
|
|
|
const { video, files, fallback, automaticallyGenerated } = options
|
|
|
|
|
|
|
|
const promises = [
|
|
|
|
{
|
|
|
|
type: ThumbnailType.MINIATURE,
|
|
|
|
fieldName: 'thumbnailfile'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: ThumbnailType.PREVIEW,
|
|
|
|
fieldName: 'previewfile'
|
|
|
|
}
|
|
|
|
].map(p => {
|
|
|
|
const fields = files?.[p.fieldName]
|
|
|
|
|
|
|
|
if (fields) {
|
2021-06-04 09:19:01 +02:00
|
|
|
return updateVideoMiniatureFromExisting({
|
2020-09-17 10:00:46 +02:00
|
|
|
inputPath: fields[0].path,
|
|
|
|
video,
|
|
|
|
type: p.type,
|
|
|
|
automaticallyGenerated: automaticallyGenerated || false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback(p.type)
|
|
|
|
})
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setVideoTags (options: {
|
|
|
|
video: MVideoTag
|
|
|
|
tags: string[]
|
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
2021-03-03 11:03:30 +01:00
|
|
|
const { video, tags, transaction } = options
|
2020-09-17 10:00:46 +02:00
|
|
|
|
2021-03-03 11:03:30 +01:00
|
|
|
const internalTags = tags || []
|
|
|
|
const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
|
|
|
|
|
|
|
|
await video.$set('Tags', tagInstances, { transaction })
|
|
|
|
video.Tags = tagInstances
|
2020-09-17 10:00:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-11 10:51:33 +01:00
|
|
|
async function addOptimizeOrMergeAudioJob (video: MVideoUUID, videoFile: MVideoFile, user: MUserId, isNewVideo = true) {
|
2021-01-21 16:57:21 +01:00
|
|
|
let dataInput: VideoTranscodingPayload
|
|
|
|
|
|
|
|
if (videoFile.isAudio()) {
|
|
|
|
dataInput = {
|
|
|
|
type: 'merge-audio-to-webtorrent',
|
|
|
|
resolution: DEFAULT_AUDIO_RESOLUTION,
|
|
|
|
videoUUID: video.uuid,
|
2022-02-01 11:16:45 +01:00
|
|
|
createHLSIfNeeded: true,
|
2022-02-11 10:51:33 +01:00
|
|
|
isNewVideo
|
2021-01-21 16:57:21 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dataInput = {
|
|
|
|
type: 'optimize-to-webtorrent',
|
|
|
|
videoUUID: video.uuid,
|
2022-02-11 10:51:33 +01:00
|
|
|
isNewVideo
|
2021-01-21 16:57:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const jobOptions = {
|
2021-05-05 09:25:11 +02:00
|
|
|
priority: await getTranscodingJobPriority(user)
|
2021-01-21 16:57:21 +01:00
|
|
|
}
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
return addTranscodingJob(dataInput, jobOptions)
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:35:08 +01:00
|
|
|
async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
|
2021-08-17 08:26:20 +02:00
|
|
|
await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
|
|
|
|
|
|
|
|
return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: payload }, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addMoveToObjectStorageJob (video: MVideoUUID, isNewVideo = true) {
|
|
|
|
await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
|
|
|
|
|
|
|
|
const dataInput = { videoUUID: video.uuid, isNewVideo }
|
|
|
|
return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput })
|
2021-01-21 16:57:21 +01:00
|
|
|
}
|
|
|
|
|
2021-05-05 09:25:11 +02:00
|
|
|
async function getTranscodingJobPriority (user: MUserId) {
|
2021-01-21 16:57:21 +01:00
|
|
|
const now = new Date()
|
|
|
|
const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
|
|
|
|
|
|
|
|
const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
|
|
|
|
|
2021-05-05 09:25:11 +02:00
|
|
|
return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
|
2021-01-21 16:57:21 +01:00
|
|
|
}
|
|
|
|
|
2020-09-17 09:20:52 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2020-09-17 10:00:46 +02:00
|
|
|
buildLocalVideoFromReq,
|
|
|
|
buildVideoThumbnailsFromReq,
|
2021-01-21 16:57:21 +01:00
|
|
|
setVideoTags,
|
|
|
|
addOptimizeOrMergeAudioJob,
|
2021-08-17 08:26:20 +02:00
|
|
|
addTranscodingJob,
|
|
|
|
addMoveToObjectStorageJob,
|
2021-05-05 09:25:11 +02:00
|
|
|
getTranscodingJobPriority
|
2020-09-17 09:20:52 +02:00
|
|
|
}
|