aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-file.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-03-10 14:49:02 +0100
committerChocobozzz <me@florianbigard.com>2020-03-10 16:18:29 +0100
commit7b81edc854902a536083298472bf92bb6726edcf (patch)
tree822dae6b6d4755ac9afffe2569b74dcb45da5535 /server/models/video/video-file.ts
parent8319d6ae72d4da6de51bd3d4b5c68040fc8dc3b4 (diff)
downloadPeerTube-7b81edc854902a536083298472bf92bb6726edcf.tar.gz
PeerTube-7b81edc854902a536083298472bf92bb6726edcf.tar.zst
PeerTube-7b81edc854902a536083298472bf92bb6726edcf.zip
Video file metadata PR cleanup
Diffstat (limited to 'server/models/video/video-file.ts')
-rw-r--r--server/models/video/video-file.ts79
1 files changed, 39 insertions, 40 deletions
diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts
index 029468004..201f0c0f1 100644
--- a/server/models/video/video-file.ts
+++ b/server/models/video/video-file.ts
@@ -30,18 +30,16 @@ import { MIMETYPES, MEMOIZE_LENGTH, MEMOIZE_TTL } from '../../initializers/const
30import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../typings/models/video/video-file' 30import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../typings/models/video/video-file'
31import { MStreamingPlaylistVideo, MVideo } from '@server/typings/models' 31import { MStreamingPlaylistVideo, MVideo } from '@server/typings/models'
32import * as memoizee from 'memoizee' 32import * as memoizee from 'memoizee'
33import validator from 'validator'
33 34
34export enum ScopeNames { 35export enum ScopeNames {
35 WITH_VIDEO = 'WITH_VIDEO', 36 WITH_VIDEO = 'WITH_VIDEO',
36 WITH_VIDEO_OR_PLAYLIST = 'WITH_VIDEO_OR_PLAYLIST',
37 WITH_METADATA = 'WITH_METADATA' 37 WITH_METADATA = 'WITH_METADATA'
38} 38}
39 39
40const METADATA_FIELDS = [ 'metadata', 'metadataUrl' ]
41
42@DefaultScope(() => ({ 40@DefaultScope(() => ({
43 attributes: { 41 attributes: {
44 exclude: [ METADATA_FIELDS[0] ] 42 exclude: [ 'metadata' ]
45 } 43 }
46})) 44}))
47@Scopes(() => ({ 45@Scopes(() => ({
@@ -53,35 +51,9 @@ const METADATA_FIELDS = [ 'metadata', 'metadataUrl' ]
53 } 51 }
54 ] 52 ]
55 }, 53 },
56 [ScopeNames.WITH_VIDEO_OR_PLAYLIST]: (videoIdOrUUID: string | number) => {
57 const where = (typeof videoIdOrUUID === 'number')
58 ? { id: videoIdOrUUID }
59 : { uuid: videoIdOrUUID }
60
61 return {
62 include: [
63 {
64 model: VideoModel.unscoped(),
65 required: false,
66 where
67 },
68 {
69 model: VideoStreamingPlaylistModel.unscoped(),
70 required: false,
71 include: [
72 {
73 model: VideoModel.unscoped(),
74 required: true,
75 where
76 }
77 ]
78 }
79 ]
80 }
81 },
82 [ScopeNames.WITH_METADATA]: { 54 [ScopeNames.WITH_METADATA]: {
83 attributes: { 55 attributes: {
84 include: METADATA_FIELDS 56 include: [ 'metadata' ]
85 } 57 }
86 } 58 }
87})) 59}))
@@ -223,10 +195,8 @@ export class VideoFileModel extends Model<VideoFileModel> {
223 195
224 static async doesVideoExistForVideoFile (id: number, videoIdOrUUID: number | string) { 196 static async doesVideoExistForVideoFile (id: number, videoIdOrUUID: number | string) {
225 const videoFile = await VideoFileModel.loadWithVideoOrPlaylist(id, videoIdOrUUID) 197 const videoFile = await VideoFileModel.loadWithVideoOrPlaylist(id, videoIdOrUUID)
226 return (videoFile?.Video.id === videoIdOrUUID) || 198
227 (videoFile?.Video.uuid === videoIdOrUUID) || 199 return !!videoFile
228 (videoFile?.VideoStreamingPlaylist?.Video?.id === videoIdOrUUID) ||
229 (videoFile?.VideoStreamingPlaylist?.Video?.uuid === videoIdOrUUID)
230 } 200 }
231 201
232 static loadWithMetadata (id: number) { 202 static loadWithMetadata (id: number) {
@@ -238,12 +208,41 @@ export class VideoFileModel extends Model<VideoFileModel> {
238 } 208 }
239 209
240 static loadWithVideoOrPlaylist (id: number, videoIdOrUUID: number | string) { 210 static loadWithVideoOrPlaylist (id: number, videoIdOrUUID: number | string) {
241 return VideoFileModel.scope({ 211 const whereVideo = validator.isUUID(videoIdOrUUID + '')
242 method: [ 212 ? { uuid: videoIdOrUUID }
243 ScopeNames.WITH_VIDEO_OR_PLAYLIST, 213 : { id: videoIdOrUUID }
244 videoIdOrUUID 214
215 const options = {
216 where: {
217 id
218 },
219 include: [
220 {
221 model: VideoModel.unscoped(),
222 required: false,
223 where: whereVideo
224 },
225 {
226 model: VideoStreamingPlaylistModel.unscoped(),
227 required: false,
228 include: [
229 {
230 model: VideoModel.unscoped(),
231 required: true,
232 where: whereVideo
233 }
234 ]
235 }
245 ] 236 ]
246 }).findByPk(id) 237 }
238
239 return VideoFileModel.findOne(options)
240 .then(file => {
241 // We used `required: false` so check we have at least a video or a streaming playlist
242 if (!file.Video && !file.VideoStreamingPlaylist) return null
243
244 return file
245 })
247 } 246 }
248 247
249 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) { 248 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) {