X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fvideo.ts;h=f5f645d3eac1997614c177f00c5a64c215f38ef0;hb=ab18fadfd1104ff2fab1b7e90915426a0c707b7f;hp=4fe2a60f0d57ca925bbee339c373cc063919f0a4;hpb=0fee09d878c576e6cbb2dd6cc733021717acc644;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/video.ts b/server/helpers/video.ts index 4fe2a60f0..f5f645d3e 100644 --- a/server/helpers/video.ts +++ b/server/helpers/video.ts @@ -1,71 +1,50 @@ -import { VideoModel } from '../models/video/video' -import * as Bluebird from 'bluebird' -import { - MVideoAccountLightBlacklistAllFiles, - MVideoFullLight, - MVideoIdThumbnail, - MVideoThumbnail, - MVideoWithRights, - MVideoImmutable -} from '@server/typings/models' import { Response } from 'express' +import { CONFIG } from '@server/initializers/config' +import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/types/models' +import { VideoPrivacy, VideoState } from '@shared/models' -type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes' +function getVideoWithAttributes (res: Response) { + return res.locals.videoAPI || res.locals.videoAll || res.locals.onlyVideo +} -function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird -function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Bluebird -function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird -function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird -function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird -function fetchVideo ( - id: number | string, - fetchType: VideoFetchType, - userId?: number -): Bluebird -function fetchVideo ( - id: number | string, - fetchType: VideoFetchType, - userId?: number -): Bluebird { - if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId) +function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) { + return isStreamingPlaylist(videoOrPlaylist) + ? videoOrPlaylist.Video + : videoOrPlaylist +} - if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id) +function isPrivacyForFederation (privacy: VideoPrivacy) { + const castedPrivacy = parseInt(privacy + '', 10) - if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id) + return castedPrivacy === VideoPrivacy.PUBLIC || + (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true && castedPrivacy === VideoPrivacy.UNLISTED) +} - if (fetchType === 'only-video') return VideoModel.load(id) +function isStateForFederation (state: VideoState) { + const castedState = parseInt(state + '', 10) - if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id) + return castedState === VideoState.PUBLISHED || castedState === VideoState.WAITING_FOR_LIVE || castedState === VideoState.LIVE_ENDED } -type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes' - -function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird -function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Bluebird -function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird -function fetchVideoByUrl ( - url: string, - fetchType: VideoFetchByUrlType -): Bluebird -function fetchVideoByUrl ( - url: string, - fetchType: VideoFetchByUrlType -): Bluebird { - if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url) +function getPrivaciesForFederation () { + return (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true) + ? [ { privacy: VideoPrivacy.PUBLIC }, { privacy: VideoPrivacy.UNLISTED } ] + : [ { privacy: VideoPrivacy.PUBLIC } ] +} - if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url) +function getExtFromMimetype (mimeTypes: { [id: string]: string | string[] }, mimeType: string) { + const value = mimeTypes[mimeType] - if (fetchType === 'only-video') return VideoModel.loadByUrl(url) -} + if (Array.isArray(value)) return value[0] -function getVideoWithAttributes (res: Response) { - return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights + return value } export { - VideoFetchType, - VideoFetchByUrlType, - fetchVideo, getVideoWithAttributes, - fetchVideoByUrl + extractVideo, + getExtFromMimetype, + isStateForFederation, + isPrivacyForFederation, + getPrivaciesForFederation }