From 90a8bd305de4153ec21137a73ff482dcc2e3e19b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Feb 2021 16:25:53 +0100 Subject: Dissociate video file names and video uuid --- server/lib/video-paths.ts | 75 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 19 deletions(-) (limited to 'server/lib/video-paths.ts') diff --git a/server/lib/video-paths.ts b/server/lib/video-paths.ts index 53fc8e81d..0385e89cc 100644 --- a/server/lib/video-paths.ts +++ b/server/lib/video-paths.ts @@ -1,19 +1,23 @@ -import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoUUID } from '@server/types/models' import { join } from 'path' -import { CONFIG } from '@server/initializers/config' -import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants' import { extractVideo } from '@server/helpers/video' +import { CONFIG } from '@server/initializers/config' +import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_PATHS, WEBSERVER } from '@server/initializers/constants' +import { isStreamingPlaylist, MStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoUUID } from '@server/types/models' // ################## Video file name ################## -function getVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { +function generateVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, isHls: boolean, resolution: number, extname: string) { const video = extractVideo(videoOrPlaylist) - if (videoFile.isHLS()) { - return generateVideoStreamingPlaylistName(video.uuid, videoFile.resolution) + // FIXME: use a generated uuid instead, that will break compatibility with PeerTube < 3.2 + // const uuid = uuidv4() + const uuid = video.uuid + + if (isHls) { + return generateVideoStreamingPlaylistName(uuid, resolution) } - return generateWebTorrentVideoName(video.uuid, videoFile.resolution, videoFile.extname) + return generateWebTorrentVideoName(uuid, resolution, extname) } function generateVideoStreamingPlaylistName (uuid: string, resolution: number) { @@ -28,36 +32,64 @@ function getVideoFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, vi if (videoFile.isHLS()) { const video = extractVideo(videoOrPlaylist) - return join(getHLSDirectory(video), getVideoFilename(videoOrPlaylist, videoFile)) + return join(getHLSDirectory(video), videoFile.filename) } - const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR - return join(baseDir, getVideoFilename(videoOrPlaylist, videoFile)) + const baseDir = isRedundancy + ? CONFIG.STORAGE.REDUNDANCY_DIR + : CONFIG.STORAGE.VIDEOS_DIR + + return join(baseDir, videoFile.filename) +} + +// ################## Redundancy ################## + +function generateHLSRedundancyUrl (video: MVideo, playlist: MStreamingPlaylist) { + // Base URL used by our HLS player + return WEBSERVER.URL + STATIC_PATHS.REDUNDANCY + playlist.getStringType() + '/' + video.uuid +} + +function generateWebTorrentRedundancyUrl (file: MVideoFile) { + return WEBSERVER.URL + STATIC_PATHS.REDUNDANCY + file.filename } // ################## Streaming playlist ################## function getHLSDirectory (video: MVideoUUID, isRedundancy = false) { - const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_STREAMING_PLAYLIST_DIRECTORY + const baseDir = isRedundancy + ? HLS_REDUNDANCY_DIRECTORY + : HLS_STREAMING_PLAYLIST_DIRECTORY return join(baseDir, video.uuid) } // ################## Torrents ################## -function getTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { +function generateTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, resolution: number) { const video = extractVideo(videoOrPlaylist) const extension = '.torrent' + // FIXME: use a generated uuid instead, that will break compatibility with PeerTube < 3.2 + // const uuid = uuidv4() + const uuid = video.uuid + if (isStreamingPlaylist(videoOrPlaylist)) { - return `${video.uuid}-${videoFile.resolution}-${videoOrPlaylist.getStringType()}${extension}` + return `${uuid}-${resolution}-${videoOrPlaylist.getStringType()}${extension}` } - return video.uuid + '-' + videoFile.resolution + extension + return uuid + '-' + resolution + extension +} + +function getTorrentFilePath (videoFile: MVideoFile) { + return join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename) } -function getTorrentFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { - return join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile)) +// ################## Meta data ################## + +function getLocalVideoFileMetadataUrl (video: MVideoUUID, videoFile: MVideoFile) { + const path = '/api/v1/videos/' + + return WEBSERVER.URL + path + video.uuid + '/metadata/' + videoFile.id } // --------------------------------------------------------------------------- @@ -65,11 +97,16 @@ function getTorrentFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, export { generateVideoStreamingPlaylistName, generateWebTorrentVideoName, - getVideoFilename, + generateVideoFilename, getVideoFilePath, - getTorrentFileName, + generateTorrentFileName, getTorrentFilePath, - getHLSDirectory + getHLSDirectory, + + getLocalVideoFileMetadataUrl, + + generateWebTorrentRedundancyUrl, + generateHLSRedundancyUrl } -- cgit v1.2.3