aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/video')
-rw-r--r--server/models/video/video-file.ts79
-rw-r--r--server/models/video/video-format-utils.ts4
-rw-r--r--server/models/video/video.ts3
3 files changed, 44 insertions, 42 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) {
diff --git a/server/models/video/video-format-utils.ts b/server/models/video/video-format-utils.ts
index 21f0e0a68..365c9581e 100644
--- a/server/models/video/video-format-utils.ts
+++ b/server/models/video/video-format-utils.ts
@@ -181,6 +181,8 @@ function videoFilesModelToFormattedJSON (
181 baseUrlWs: string, 181 baseUrlWs: string,
182 videoFiles: MVideoFileRedundanciesOpt[] 182 videoFiles: MVideoFileRedundanciesOpt[]
183): VideoFile[] { 183): VideoFile[] {
184 const video = extractVideo(model)
185
184 return videoFiles 186 return videoFiles
185 .map(videoFile => { 187 .map(videoFile => {
186 return { 188 return {
@@ -195,7 +197,7 @@ function videoFilesModelToFormattedJSON (
195 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp), 197 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
196 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp), 198 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
197 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp), 199 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp),
198 metadataUrl: videoFile.metadataUrl // only send the metadataUrl and not the metadata over the wire 200 metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp)
199 } as VideoFile 201 } as VideoFile
200 }) 202 })
201 .sort((a, b) => { 203 .sort((a, b) => {
diff --git a/server/models/video/video.ts b/server/models/video/video.ts
index 5e4b7d44c..958a49e65 100644
--- a/server/models/video/video.ts
+++ b/server/models/video/video.ts
@@ -1849,7 +1849,8 @@ export class VideoModel extends Model<VideoModel> {
1849 1849
1850 getVideoFileMetadataUrl (videoFile: MVideoFile, baseUrlHttp: string) { 1850 getVideoFileMetadataUrl (videoFile: MVideoFile, baseUrlHttp: string) {
1851 const path = '/api/v1/videos/' 1851 const path = '/api/v1/videos/'
1852 return videoFile.metadata 1852
1853 return this.isOwned()
1853 ? baseUrlHttp + path + this.uuid + '/metadata/' + videoFile.id 1854 ? baseUrlHttp + path + this.uuid + '/metadata/' + videoFile.id
1854 : videoFile.metadataUrl 1855 : videoFile.metadataUrl
1855 } 1856 }