2021-05-10 11:13:41 +02:00
|
|
|
import { UploadFiles } from 'express'
|
2022-08-08 15:48:17 +02:00
|
|
|
import memoizee from 'memoizee'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { Transaction } from 'sequelize'
|
|
|
|
import {
|
|
|
|
ManageVideoTorrentPayload,
|
|
|
|
ThumbnailType,
|
|
|
|
ThumbnailType_Type,
|
|
|
|
VideoCreate,
|
|
|
|
VideoPrivacy,
|
|
|
|
VideoPrivacyType,
|
|
|
|
VideoStateType
|
|
|
|
} from '@peertube/peertube-models'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants.js'
|
|
|
|
import { TagModel } from '@server/models/video/tag.js'
|
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info.js'
|
|
|
|
import { VideoModel } from '@server/models/video/video.js'
|
|
|
|
import { FilteredModelAttributes } from '@server/types/index.js'
|
2023-12-27 10:39:09 +01:00
|
|
|
import { MThumbnail, MVideo, MVideoFullLight, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models/index.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { CreateJobArgument, JobQueue } from './job-queue/job-queue.js'
|
|
|
|
import { updateLocalVideoMiniatureFromExisting } from './thumbnail.js'
|
|
|
|
import { moveFilesIfPrivacyChanged } from './video-privacy.js'
|
2020-09-17 09:20:52 +02:00
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
export 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,
|
2022-07-13 11:58:01 +02:00
|
|
|
channelId,
|
2020-09-17 09:20:52 +02:00
|
|
|
originallyPublishedAt: videoInfo.originallyPublishedAt
|
2021-05-12 14:09:04 +02:00
|
|
|
? new Date(videoInfo.originallyPublishedAt)
|
|
|
|
: null
|
2020-09-17 09:20:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
export async function buildVideoThumbnailsFromReq (options: {
|
2020-09-17 10:00:46 +02:00
|
|
|
video: MVideoThumbnail
|
2021-05-10 11:13:41 +02:00
|
|
|
files: UploadFiles
|
2023-07-31 14:34:36 +02:00
|
|
|
fallback: (type: ThumbnailType_Type) => Promise<MThumbnail>
|
2020-09-17 10:00:46 +02:00
|
|
|
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) {
|
2023-06-06 15:59:51 +02:00
|
|
|
return updateLocalVideoMiniatureFromExisting({
|
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)
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
export async function setVideoTags (options: {
|
2020-09-17 10:00:46 +02:00
|
|
|
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-03-22 14:35:04 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
export async function buildMoveJob (options: {
|
2022-03-22 14:35:04 +01:00
|
|
|
video: MVideoUUID
|
2023-07-31 14:34:36 +02:00
|
|
|
previousVideoState: VideoStateType
|
2023-10-31 12:15:40 +01:00
|
|
|
type: 'move-to-object-storage' | 'move-to-file-system'
|
2022-03-22 14:35:04 +01:00
|
|
|
isNewVideo?: boolean // Default true
|
|
|
|
}) {
|
2023-10-31 12:15:40 +01:00
|
|
|
const { video, previousVideoState, isNewVideo = true, type } = options
|
2022-03-22 14:35:04 +01:00
|
|
|
|
|
|
|
await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
|
|
|
|
|
2022-08-08 15:48:17 +02:00
|
|
|
return {
|
2023-10-31 12:15:40 +01:00
|
|
|
type,
|
2022-08-08 15:48:17 +02:00
|
|
|
payload: {
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
isNewVideo,
|
|
|
|
previousVideoState
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 14:35:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-12-27 10:39:09 +01:00
|
|
|
export function buildStoryboardJobIfNeeded (options: {
|
|
|
|
video: MVideo
|
|
|
|
federate: boolean
|
|
|
|
}) {
|
|
|
|
const { video, federate } = options
|
|
|
|
|
|
|
|
if (CONFIG.STORYBOARDS.ENABLED) {
|
|
|
|
return {
|
|
|
|
type: 'generate-video-storyboard' as 'generate-video-storyboard',
|
|
|
|
payload: {
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
federate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (federate === true) {
|
|
|
|
return {
|
|
|
|
type: 'federate-video' as 'federate-video',
|
|
|
|
payload: {
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
isNewVideoForFederation: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
export async function getVideoDuration (videoId: number | string) {
|
2022-06-17 14:34:37 +02:00
|
|
|
const video = await VideoModel.load(videoId)
|
|
|
|
|
|
|
|
const duration = video.isLive
|
|
|
|
? undefined
|
|
|
|
: video.duration
|
|
|
|
|
|
|
|
return { duration, isLive: video.isLive }
|
|
|
|
}
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
export const getCachedVideoDuration = memoizee(getVideoDuration, {
|
2022-06-17 14:34:37 +02:00
|
|
|
promise: true,
|
|
|
|
max: MEMOIZE_LENGTH.VIDEO_DURATION,
|
|
|
|
maxAge: MEMOIZE_TTL.VIDEO_DURATION
|
|
|
|
})
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
export async function addVideoJobsAfterUpdate (options: {
|
2022-10-12 16:09:02 +02:00
|
|
|
video: MVideoFullLight
|
2023-12-15 14:52:12 +01:00
|
|
|
isNewVideoForFederation: boolean
|
2022-10-12 16:09:02 +02:00
|
|
|
|
|
|
|
nameChanged: boolean
|
2023-07-31 14:34:36 +02:00
|
|
|
oldPrivacy: VideoPrivacyType
|
2022-10-12 16:09:02 +02:00
|
|
|
}) {
|
2023-12-15 14:52:12 +01:00
|
|
|
const { video, nameChanged, oldPrivacy, isNewVideoForFederation } = options
|
2022-10-12 16:09:02 +02:00
|
|
|
const jobs: CreateJobArgument[] = []
|
|
|
|
|
|
|
|
const filePathChanged = await moveFilesIfPrivacyChanged(video, oldPrivacy)
|
|
|
|
|
|
|
|
if (!video.isLive && (nameChanged || filePathChanged)) {
|
|
|
|
for (const file of (video.VideoFiles || [])) {
|
|
|
|
const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id }
|
|
|
|
|
|
|
|
jobs.push({ type: 'manage-video-torrent', payload })
|
|
|
|
}
|
|
|
|
|
|
|
|
const hls = video.getHLSPlaylist()
|
|
|
|
|
|
|
|
for (const file of (hls?.VideoFiles || [])) {
|
|
|
|
const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id }
|
|
|
|
|
|
|
|
jobs.push({ type: 'manage-video-torrent', payload })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jobs.push({
|
|
|
|
type: 'federate-video',
|
|
|
|
payload: {
|
|
|
|
videoUUID: video.uuid,
|
2023-12-15 14:52:12 +01:00
|
|
|
isNewVideoForFederation
|
2022-10-12 16:09:02 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-07-31 14:34:36 +02:00
|
|
|
const wasConfidentialVideo = new Set<VideoPrivacyType>([
|
|
|
|
VideoPrivacy.PRIVATE,
|
|
|
|
VideoPrivacy.UNLISTED,
|
|
|
|
VideoPrivacy.INTERNAL
|
|
|
|
]).has(oldPrivacy)
|
2022-10-12 16:09:02 +02:00
|
|
|
|
|
|
|
if (wasConfidentialVideo) {
|
|
|
|
jobs.push({
|
|
|
|
type: 'notify',
|
|
|
|
payload: {
|
|
|
|
action: 'new-video',
|
|
|
|
videoUUID: video.uuid
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return JobQueue.Instance.createSequentialJobFlow(...jobs)
|
|
|
|
}
|