1
0
Fork 0
peertube/server/core/lib/video.ts

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-08-08 09:48:17 -04:00
import memoizee from 'memoizee'
import { Transaction } from 'sequelize'
import { MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants.js'
import { TagModel } from '@server/models/video/tag.js'
import { VideoModel } from '@server/models/video/video.js'
2024-02-13 08:23:32 -05:00
import { MVideoTag } from '@server/types/models/index.js'
2020-09-17 04:00:46 -04:00
// ---------------------------------------------------------------------------
export async function setVideoTags (options: {
2020-09-17 04:00:46 -04:00
video: MVideoTag
tags: string[]
transaction?: Transaction
}) {
2021-03-03 05:03:30 -05:00
const { video, tags, transaction } = options
2020-09-17 04:00:46 -04:00
2021-03-03 05:03:30 -05:00
const internalTags = tags || []
const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
await video.$set('Tags', tagInstances, { transaction })
video.Tags = tagInstances
2020-09-17 04:00:46 -04:00
}
// ---------------------------------------------------------------------------
2024-02-13 08:23:32 -05:00
async function getVideoDuration (videoId: number | string) {
2022-06-17 08:34:37 -04:00
const video = await VideoModel.load(videoId)
const duration = video.isLive
? undefined
: video.duration
return { duration, isLive: video.isLive }
}
export const getCachedVideoDuration = memoizee(getVideoDuration, {
2022-06-17 08:34:37 -04:00
promise: true,
max: MEMOIZE_LENGTH.VIDEO_DURATION,
maxAge: MEMOIZE_TTL.VIDEO_DURATION
})