From 3545e72c686ff1725bbdfd8d16d693e2f4aa75a3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 12 Oct 2022 16:09:02 +0200 Subject: Put private videos under a specific subdirectory --- .../models/video/formatter/video-format-utils.ts | 22 ++++++++++------ server/models/video/video-file.ts | 29 ++++++++++++++++++++-- server/models/video/video-streaming-playlist.ts | 21 +++++++++++----- server/models/video/video.ts | 24 ++++++------------ 4 files changed, 65 insertions(+), 31 deletions(-) (limited to 'server/models') diff --git a/server/models/video/formatter/video-format-utils.ts b/server/models/video/formatter/video-format-utils.ts index e1b0eb610..76745f4b5 100644 --- a/server/models/video/formatter/video-format-utils.ts +++ b/server/models/video/formatter/video-format-utils.ts @@ -34,6 +34,7 @@ import { import { MServer, MStreamingPlaylistRedundanciesOpt, + MUserId, MVideo, MVideoAP, MVideoFile, @@ -245,8 +246,12 @@ function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) { function videoFilesModelToFormattedJSON ( video: MVideoFormattable, videoFiles: MVideoFileRedundanciesOpt[], - includeMagnet = true + options: { + includeMagnet?: boolean // default true + } = {} ): VideoFile[] { + const { includeMagnet = true } = options + const trackerUrls = includeMagnet ? video.getTrackerUrls() : [] @@ -281,11 +286,14 @@ function videoFilesModelToFormattedJSON ( }) } -function addVideoFilesInAPAcc ( - acc: ActivityUrlObject[] | ActivityTagObject[], - video: MVideo, +function addVideoFilesInAPAcc (options: { + acc: ActivityUrlObject[] | ActivityTagObject[] + video: MVideo files: MVideoFile[] -) { + user?: MUserId +}) { + const { acc, video, files } = options + const trackerUrls = video.getTrackerUrls() const sortedFiles = (files || []) @@ -370,7 +378,7 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject { } ] - addVideoFilesInAPAcc(url, video, video.VideoFiles || []) + addVideoFilesInAPAcc({ acc: url, video, files: video.VideoFiles || [] }) for (const playlist of (video.VideoStreamingPlaylists || [])) { const tag = playlist.p2pMediaLoaderInfohashes @@ -382,7 +390,7 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject { href: playlist.getSha256SegmentsUrl(video) }) - addVideoFilesInAPAcc(tag, video, playlist.VideoFiles || []) + addVideoFilesInAPAcc({ acc: tag, video, files: playlist.VideoFiles || [] }) url.push({ type: 'Link', diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index d4f07f85f..1a608932f 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -24,6 +24,7 @@ import { extractVideo } from '@server/helpers/video' import { buildRemoteVideoBaseUrl } from '@server/lib/activitypub/url' import { getHLSPublicFileUrl, getWebTorrentPublicFileUrl } from '@server/lib/object-storage' import { getFSTorrentFilePath } from '@server/lib/paths' +import { isVideoInPrivateDirectory } from '@server/lib/video-privacy' import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models' import { VideoResolution, VideoStorage } from '@shared/models' import { AttributesOnly } from '@shared/typescript-utils' @@ -295,6 +296,16 @@ export class VideoFileModel extends Model return VideoFileModel.findOne(query) } + static loadWithVideoByFilename (filename: string): Promise { + const query = { + where: { + filename + } + } + + return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query) + } + static loadWithVideoOrPlaylistByTorrentFilename (filename: string) { const query = { where: { @@ -305,6 +316,10 @@ export class VideoFileModel extends Model return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query) } + static load (id: number): Promise { + return VideoFileModel.findByPk(id) + } + static loadWithMetadata (id: number) { return VideoFileModel.scope(ScopeNames.WITH_METADATA).findByPk(id) } @@ -467,7 +482,7 @@ export class VideoFileModel extends Model } getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo { - if (this.videoId) return (this as MVideoFileVideo).Video + if (this.videoId || (this as MVideoFileVideo).Video) return (this as MVideoFileVideo).Video return (this as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist } @@ -508,7 +523,17 @@ export class VideoFileModel extends Model } getFileStaticPath (video: MVideo) { - if (this.isHLS()) return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename) + if (this.isHLS()) { + if (isVideoInPrivateDirectory(video.privacy)) { + return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.filename) + } + + return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename) + } + + if (isVideoInPrivateDirectory(video.privacy)) { + return join(STATIC_PATHS.PRIVATE_WEBSEED, this.filename) + } return join(STATIC_PATHS.WEBSEED, this.filename) } diff --git a/server/models/video/video-streaming-playlist.ts b/server/models/video/video-streaming-playlist.ts index 2b6771f27..b919046ed 100644 --- a/server/models/video/video-streaming-playlist.ts +++ b/server/models/video/video-streaming-playlist.ts @@ -17,6 +17,7 @@ import { } from 'sequelize-typescript' import { getHLSPublicFileUrl } from '@server/lib/object-storage' import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename } from '@server/lib/paths' +import { isVideoInPrivateDirectory } from '@server/lib/video-privacy' import { VideoFileModel } from '@server/models/video/video-file' import { MStreamingPlaylist, MStreamingPlaylistFilesVideo, MVideo } from '@server/types/models' import { sha1 } from '@shared/extra-utils' @@ -250,7 +251,7 @@ export class VideoStreamingPlaylistModel extends Model>> { let files: VideoFile[] = [] if (Array.isArray(this.VideoFiles)) { - const result = videoFilesModelToFormattedJSON(this, this.VideoFiles, includeMagnet) + const result = videoFilesModelToFormattedJSON(this, this.VideoFiles, { includeMagnet }) files = files.concat(result) } for (const p of (this.VideoStreamingPlaylists || [])) { - const result = videoFilesModelToFormattedJSON(this, p.VideoFiles, includeMagnet) + const result = videoFilesModelToFormattedJSON(this, p.VideoFiles, { includeMagnet }) files = files.concat(result) } @@ -1868,22 +1868,14 @@ export class VideoModel extends Model>> { return setAsUpdated('video', this.id, transaction) } - requiresAuth () { - return this.privacy === VideoPrivacy.PRIVATE || this.privacy === VideoPrivacy.INTERNAL || !!this.VideoBlacklist - } + requiresAuth (paramId: string) { + if (this.privacy === VideoPrivacy.UNLISTED) { + if (!isUUIDValid(paramId)) return true - setPrivacy (newPrivacy: VideoPrivacy) { - if (this.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) { - this.publishedAt = new Date() + return false } - this.privacy = newPrivacy - } - - isConfidential () { - return this.privacy === VideoPrivacy.PRIVATE || - this.privacy === VideoPrivacy.UNLISTED || - this.privacy === VideoPrivacy.INTERNAL + return this.privacy === VideoPrivacy.PRIVATE || this.privacy === VideoPrivacy.INTERNAL || !!this.VideoBlacklist } async setNewState (newState: VideoState, isNewVideo: boolean, transaction: Transaction) { -- cgit v1.2.3