diff options
Diffstat (limited to 'server/models/video/video-file.ts')
-rw-r--r-- | server/models/video/video-file.ts | 80 |
1 files changed, 71 insertions, 9 deletions
diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index d4f07f85f..9c4e6d078 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts | |||
@@ -22,8 +22,14 @@ import validator from 'validator' | |||
22 | import { logger } from '@server/helpers/logger' | 22 | import { logger } from '@server/helpers/logger' |
23 | import { extractVideo } from '@server/helpers/video' | 23 | import { extractVideo } from '@server/helpers/video' |
24 | import { buildRemoteVideoBaseUrl } from '@server/lib/activitypub/url' | 24 | import { buildRemoteVideoBaseUrl } from '@server/lib/activitypub/url' |
25 | import { getHLSPublicFileUrl, getWebTorrentPublicFileUrl } from '@server/lib/object-storage' | 25 | import { |
26 | getHLSPrivateFileUrl, | ||
27 | getHLSPublicFileUrl, | ||
28 | getWebTorrentPrivateFileUrl, | ||
29 | getWebTorrentPublicFileUrl | ||
30 | } from '@server/lib/object-storage' | ||
26 | import { getFSTorrentFilePath } from '@server/lib/paths' | 31 | import { getFSTorrentFilePath } from '@server/lib/paths' |
32 | import { isVideoInPrivateDirectory } from '@server/lib/video-privacy' | ||
27 | import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models' | 33 | import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models' |
28 | import { VideoResolution, VideoStorage } from '@shared/models' | 34 | import { VideoResolution, VideoStorage } from '@shared/models' |
29 | import { AttributesOnly } from '@shared/typescript-utils' | 35 | import { AttributesOnly } from '@shared/typescript-utils' |
@@ -48,6 +54,7 @@ import { doesExist } from '../shared' | |||
48 | import { parseAggregateResult, throwIfNotValid } from '../utils' | 54 | import { parseAggregateResult, throwIfNotValid } from '../utils' |
49 | import { VideoModel } from './video' | 55 | import { VideoModel } from './video' |
50 | import { VideoStreamingPlaylistModel } from './video-streaming-playlist' | 56 | import { VideoStreamingPlaylistModel } from './video-streaming-playlist' |
57 | import { CONFIG } from '@server/initializers/config' | ||
51 | 58 | ||
52 | export enum ScopeNames { | 59 | export enum ScopeNames { |
53 | WITH_VIDEO = 'WITH_VIDEO', | 60 | WITH_VIDEO = 'WITH_VIDEO', |
@@ -295,6 +302,16 @@ export class VideoFileModel extends Model<Partial<AttributesOnly<VideoFileModel> | |||
295 | return VideoFileModel.findOne(query) | 302 | return VideoFileModel.findOne(query) |
296 | } | 303 | } |
297 | 304 | ||
305 | static loadWithVideoByFilename (filename: string): Promise<MVideoFileVideo | MVideoFileStreamingPlaylistVideo> { | ||
306 | const query = { | ||
307 | where: { | ||
308 | filename | ||
309 | } | ||
310 | } | ||
311 | |||
312 | return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query) | ||
313 | } | ||
314 | |||
298 | static loadWithVideoOrPlaylistByTorrentFilename (filename: string) { | 315 | static loadWithVideoOrPlaylistByTorrentFilename (filename: string) { |
299 | const query = { | 316 | const query = { |
300 | where: { | 317 | where: { |
@@ -305,6 +322,10 @@ export class VideoFileModel extends Model<Partial<AttributesOnly<VideoFileModel> | |||
305 | return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query) | 322 | return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query) |
306 | } | 323 | } |
307 | 324 | ||
325 | static load (id: number): Promise<MVideoFile> { | ||
326 | return VideoFileModel.findByPk(id) | ||
327 | } | ||
328 | |||
308 | static loadWithMetadata (id: number) { | 329 | static loadWithMetadata (id: number) { |
309 | return VideoFileModel.scope(ScopeNames.WITH_METADATA).findByPk(id) | 330 | return VideoFileModel.scope(ScopeNames.WITH_METADATA).findByPk(id) |
310 | } | 331 | } |
@@ -467,7 +488,7 @@ export class VideoFileModel extends Model<Partial<AttributesOnly<VideoFileModel> | |||
467 | } | 488 | } |
468 | 489 | ||
469 | getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo { | 490 | getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo { |
470 | if (this.videoId) return (this as MVideoFileVideo).Video | 491 | if (this.videoId || (this as MVideoFileVideo).Video) return (this as MVideoFileVideo).Video |
471 | 492 | ||
472 | return (this as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist | 493 | return (this as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist |
473 | } | 494 | } |
@@ -488,7 +509,25 @@ export class VideoFileModel extends Model<Partial<AttributesOnly<VideoFileModel> | |||
488 | return !!this.videoStreamingPlaylistId | 509 | return !!this.videoStreamingPlaylistId |
489 | } | 510 | } |
490 | 511 | ||
491 | getObjectStorageUrl () { | 512 | // --------------------------------------------------------------------------- |
513 | |||
514 | getObjectStorageUrl (video: MVideo) { | ||
515 | if (video.hasPrivateStaticPath() && CONFIG.OBJECT_STORAGE.PROXY.PROXIFY_PRIVATE_FILES === true) { | ||
516 | return this.getPrivateObjectStorageUrl(video) | ||
517 | } | ||
518 | |||
519 | return this.getPublicObjectStorageUrl() | ||
520 | } | ||
521 | |||
522 | private getPrivateObjectStorageUrl (video: MVideo) { | ||
523 | if (this.isHLS()) { | ||
524 | return getHLSPrivateFileUrl(video, this.filename) | ||
525 | } | ||
526 | |||
527 | return getWebTorrentPrivateFileUrl(this.filename) | ||
528 | } | ||
529 | |||
530 | private getPublicObjectStorageUrl () { | ||
492 | if (this.isHLS()) { | 531 | if (this.isHLS()) { |
493 | return getHLSPublicFileUrl(this.fileUrl) | 532 | return getHLSPublicFileUrl(this.fileUrl) |
494 | } | 533 | } |
@@ -496,23 +535,46 @@ export class VideoFileModel extends Model<Partial<AttributesOnly<VideoFileModel> | |||
496 | return getWebTorrentPublicFileUrl(this.fileUrl) | 535 | return getWebTorrentPublicFileUrl(this.fileUrl) |
497 | } | 536 | } |
498 | 537 | ||
538 | // --------------------------------------------------------------------------- | ||
539 | |||
499 | getFileUrl (video: MVideo) { | 540 | getFileUrl (video: MVideo) { |
500 | if (this.storage === VideoStorage.OBJECT_STORAGE) { | 541 | if (video.isOwned()) { |
501 | return this.getObjectStorageUrl() | 542 | if (this.storage === VideoStorage.OBJECT_STORAGE) { |
502 | } | 543 | return this.getObjectStorageUrl(video) |
544 | } | ||
503 | 545 | ||
504 | if (!this.Video) this.Video = video as VideoModel | 546 | return WEBSERVER.URL + this.getFileStaticPath(video) |
505 | if (video.isOwned()) return WEBSERVER.URL + this.getFileStaticPath(video) | 547 | } |
506 | 548 | ||
507 | return this.fileUrl | 549 | return this.fileUrl |
508 | } | 550 | } |
509 | 551 | ||
552 | // --------------------------------------------------------------------------- | ||
553 | |||
510 | getFileStaticPath (video: MVideo) { | 554 | getFileStaticPath (video: MVideo) { |
511 | if (this.isHLS()) return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename) | 555 | if (this.isHLS()) return this.getHLSFileStaticPath(video) |
556 | |||
557 | return this.getWebTorrentFileStaticPath(video) | ||
558 | } | ||
559 | |||
560 | private getWebTorrentFileStaticPath (video: MVideo) { | ||
561 | if (isVideoInPrivateDirectory(video.privacy)) { | ||
562 | return join(STATIC_PATHS.PRIVATE_WEBSEED, this.filename) | ||
563 | } | ||
512 | 564 | ||
513 | return join(STATIC_PATHS.WEBSEED, this.filename) | 565 | return join(STATIC_PATHS.WEBSEED, this.filename) |
514 | } | 566 | } |
515 | 567 | ||
568 | private getHLSFileStaticPath (video: MVideo) { | ||
569 | if (isVideoInPrivateDirectory(video.privacy)) { | ||
570 | return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.filename) | ||
571 | } | ||
572 | |||
573 | return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename) | ||
574 | } | ||
575 | |||
576 | // --------------------------------------------------------------------------- | ||
577 | |||
516 | getFileDownloadUrl (video: MVideoWithHost) { | 578 | getFileDownloadUrl (video: MVideoWithHost) { |
517 | const path = this.isHLS() | 579 | const path = this.isHLS() |
518 | ? join(STATIC_DOWNLOAD_PATHS.HLS_VIDEOS, `${video.uuid}-${this.resolution}-fragmented${this.extname}`) | 580 | ? join(STATIC_DOWNLOAD_PATHS.HLS_VIDEOS, `${video.uuid}-${this.resolution}-fragmented${this.extname}`) |