video.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import memoizee from 'memoizee'
  2. import { Transaction } from 'sequelize'
  3. import { MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants.js'
  4. import { TagModel } from '@server/models/video/tag.js'
  5. import { VideoModel } from '@server/models/video/video.js'
  6. import { MVideoTag } from '@server/types/models/index.js'
  7. // ---------------------------------------------------------------------------
  8. export async function setVideoTags (options: {
  9. video: MVideoTag
  10. tags: string[]
  11. transaction?: Transaction
  12. }) {
  13. const { video, tags, transaction } = options
  14. const internalTags = tags || []
  15. const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
  16. await video.$set('Tags', tagInstances, { transaction })
  17. video.Tags = tagInstances
  18. }
  19. // ---------------------------------------------------------------------------
  20. async function getVideoDuration (videoId: number | string) {
  21. const video = await VideoModel.load(videoId)
  22. const duration = video.isLive
  23. ? undefined
  24. : video.duration
  25. return { duration, isLive: video.isLive }
  26. }
  27. export const getCachedVideoDuration = memoizee(getVideoDuration, {
  28. promise: true,
  29. max: MEMOIZE_LENGTH.VIDEO_DURATION,
  30. maxAge: MEMOIZE_TTL.VIDEO_DURATION
  31. })