video.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { VideoModel } from '../models/video/video'
  2. import * as Bluebird from 'bluebird'
  3. import {
  4. MVideoAccountLightBlacklistAllFiles,
  5. MVideoFullLight,
  6. MVideoIdThumbnail,
  7. MVideoThumbnail,
  8. MVideoWithRights
  9. } from '@server/typings/models'
  10. import { Response } from 'express'
  11. type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none'
  12. function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight>
  13. function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail>
  14. function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights>
  15. function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail>
  16. function fetchVideo (
  17. id: number | string,
  18. fetchType: VideoFetchType,
  19. userId?: number
  20. ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail>
  21. function fetchVideo (
  22. id: number | string,
  23. fetchType: VideoFetchType,
  24. userId?: number
  25. ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail> {
  26. if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
  27. if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id)
  28. if (fetchType === 'only-video') return VideoModel.load(id)
  29. if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
  30. }
  31. type VideoFetchByUrlType = 'all' | 'only-video'
  32. function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird<MVideoAccountLightBlacklistAllFiles>
  33. function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird<MVideoThumbnail>
  34. function fetchVideoByUrl (url: string, fetchType: VideoFetchByUrlType): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
  35. function fetchVideoByUrl (url: string, fetchType: VideoFetchByUrlType): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail> {
  36. if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url)
  37. if (fetchType === 'only-video') return VideoModel.loadByUrl(url)
  38. }
  39. function getVideoWithAttributes (res: Response) {
  40. return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
  41. }
  42. export {
  43. VideoFetchType,
  44. VideoFetchByUrlType,
  45. fetchVideo,
  46. getVideoWithAttributes,
  47. fetchVideoByUrl
  48. }