2019-11-15 08:06:03 -06:00
|
|
|
import { join } from 'path'
|
2020-04-23 02:32:53 -05:00
|
|
|
import { extractVideo } from '@server/helpers/video'
|
2021-02-16 09:25:53 -06:00
|
|
|
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'
|
2019-11-15 08:06:03 -06:00
|
|
|
|
|
|
|
// ################## Video file name ##################
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
function generateVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, isHls: boolean, resolution: number, extname: string) {
|
2019-11-15 08:06:03 -06:00
|
|
|
const video = extractVideo(videoOrPlaylist)
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
// 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)
|
2019-11-15 08:06:03 -06:00
|
|
|
}
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
return generateWebTorrentVideoName(uuid, resolution, extname)
|
2019-11-15 08:06:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function generateVideoStreamingPlaylistName (uuid: string, resolution: number) {
|
|
|
|
return `${uuid}-${resolution}-fragmented.mp4`
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateWebTorrentVideoName (uuid: string, resolution: number, extname: string) {
|
|
|
|
return uuid + '-' + resolution + extname
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVideoFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile, isRedundancy = false) {
|
2020-11-06 03:57:40 -06:00
|
|
|
if (videoFile.isHLS()) {
|
2019-11-15 08:06:03 -06:00
|
|
|
const video = extractVideo(videoOrPlaylist)
|
2020-09-17 02:20:52 -05:00
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
return join(getHLSDirectory(video), videoFile.filename)
|
2019-11-15 08:06:03 -06:00
|
|
|
}
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
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
|
2019-11-15 08:06:03 -06:00
|
|
|
}
|
|
|
|
|
2019-11-21 05:16:27 -06:00
|
|
|
// ################## Streaming playlist ##################
|
|
|
|
|
|
|
|
function getHLSDirectory (video: MVideoUUID, isRedundancy = false) {
|
2021-02-16 09:25:53 -06:00
|
|
|
const baseDir = isRedundancy
|
|
|
|
? HLS_REDUNDANCY_DIRECTORY
|
|
|
|
: HLS_STREAMING_PLAYLIST_DIRECTORY
|
2019-11-21 05:16:27 -06:00
|
|
|
|
|
|
|
return join(baseDir, video.uuid)
|
|
|
|
}
|
|
|
|
|
2019-11-15 08:06:03 -06:00
|
|
|
// ################## Torrents ##################
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
function generateTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, resolution: number) {
|
2019-11-15 08:06:03 -06:00
|
|
|
const video = extractVideo(videoOrPlaylist)
|
|
|
|
const extension = '.torrent'
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
// FIXME: use a generated uuid instead, that will break compatibility with PeerTube < 3.2
|
|
|
|
// const uuid = uuidv4()
|
|
|
|
const uuid = video.uuid
|
|
|
|
|
2019-11-15 08:06:03 -06:00
|
|
|
if (isStreamingPlaylist(videoOrPlaylist)) {
|
2021-02-16 09:25:53 -06:00
|
|
|
return `${uuid}-${resolution}-${videoOrPlaylist.getStringType()}${extension}`
|
2019-11-15 08:06:03 -06:00
|
|
|
}
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
return uuid + '-' + resolution + extension
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTorrentFilePath (videoFile: MVideoFile) {
|
|
|
|
return join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
|
2019-11-15 08:06:03 -06:00
|
|
|
}
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
// ################## Meta data ##################
|
|
|
|
|
|
|
|
function getLocalVideoFileMetadataUrl (video: MVideoUUID, videoFile: MVideoFile) {
|
|
|
|
const path = '/api/v1/videos/'
|
|
|
|
|
|
|
|
return WEBSERVER.URL + path + video.uuid + '/metadata/' + videoFile.id
|
2019-11-15 08:06:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
generateVideoStreamingPlaylistName,
|
|
|
|
generateWebTorrentVideoName,
|
2021-02-16 09:25:53 -06:00
|
|
|
generateVideoFilename,
|
2019-11-15 08:06:03 -06:00
|
|
|
getVideoFilePath,
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
generateTorrentFileName,
|
2019-11-21 05:16:27 -06:00
|
|
|
getTorrentFilePath,
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
getHLSDirectory,
|
|
|
|
|
|
|
|
getLocalVideoFileMetadataUrl,
|
|
|
|
|
|
|
|
generateWebTorrentRedundancyUrl,
|
|
|
|
generateHLSRedundancyUrl
|
2019-11-15 08:06:03 -06:00
|
|
|
}
|