1 import { join } from 'path'
2 import { extractVideo } from '@server/helpers/video'
3 import { CONFIG } from '@server/initializers/config'
4 import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_PATHS, WEBSERVER } from '@server/initializers/constants'
5 import { isStreamingPlaylist, MStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoUUID } from '@server/types/models'
7 // ################## Video file name ##################
9 function generateVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, isHls: boolean, resolution: number, extname: string) {
10 const video = extractVideo(videoOrPlaylist)
12 // FIXME: use a generated uuid instead, that will break compatibility with PeerTube < 3.2
13 // const uuid = uuidv4()
14 const uuid = video.uuid
17 return generateVideoStreamingPlaylistName(uuid, resolution)
20 return generateWebTorrentVideoName(uuid, resolution, extname)
23 function generateVideoStreamingPlaylistName (uuid: string, resolution: number) {
24 return `${uuid}-${resolution}-fragmented.mp4`
27 function generateWebTorrentVideoName (uuid: string, resolution: number, extname: string) {
28 return uuid + '-' + resolution + extname
31 function getVideoFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile, isRedundancy = false) {
32 if (videoFile.isHLS()) {
33 const video = extractVideo(videoOrPlaylist)
35 return join(getHLSDirectory(video), videoFile.filename)
38 const baseDir = isRedundancy
39 ? CONFIG.STORAGE.REDUNDANCY_DIR
40 : CONFIG.STORAGE.VIDEOS_DIR
42 return join(baseDir, videoFile.filename)
45 // ################## Redundancy ##################
47 function generateHLSRedundancyUrl (video: MVideo, playlist: MStreamingPlaylist) {
48 // Base URL used by our HLS player
49 return WEBSERVER.URL + STATIC_PATHS.REDUNDANCY + playlist.getStringType() + '/' + video.uuid
52 function generateWebTorrentRedundancyUrl (file: MVideoFile) {
53 return WEBSERVER.URL + STATIC_PATHS.REDUNDANCY + file.filename
56 // ################## Streaming playlist ##################
58 function getHLSDirectory (video: MVideoUUID, isRedundancy = false) {
59 const baseDir = isRedundancy
60 ? HLS_REDUNDANCY_DIRECTORY
61 : HLS_STREAMING_PLAYLIST_DIRECTORY
63 return join(baseDir, video.uuid)
66 // ################## Torrents ##################
68 function generateTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, resolution: number) {
69 const video = extractVideo(videoOrPlaylist)
70 const extension = '.torrent'
72 // FIXME: use a generated uuid instead, that will break compatibility with PeerTube < 3.2
73 // const uuid = uuidv4()
74 const uuid = video.uuid
76 if (isStreamingPlaylist(videoOrPlaylist)) {
77 return `${uuid}-${resolution}-${videoOrPlaylist.getStringType()}${extension}`
80 return uuid + '-' + resolution + extension
83 function getTorrentFilePath (videoFile: MVideoFile) {
84 return join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
87 // ################## Meta data ##################
89 function getLocalVideoFileMetadataUrl (video: MVideoUUID, videoFile: MVideoFile) {
90 const path = '/api/v1/videos/'
92 return WEBSERVER.URL + path + video.uuid + '/metadata/' + videoFile.id
95 // ---------------------------------------------------------------------------
98 generateVideoStreamingPlaylistName,
99 generateWebTorrentVideoName,
100 generateVideoFilename,
103 generateTorrentFileName,
108 getLocalVideoFileMetadataUrl,
110 generateWebTorrentRedundancyUrl,
111 generateHLSRedundancyUrl