1 import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoUUID } from '@server/types/models'
2 import { join } from 'path'
3 import { CONFIG } from '@server/initializers/config'
4 import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants'
5 import { extractVideo } from '@server/helpers/video'
7 // ################## Video file name ##################
9 function getVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
10 const video = extractVideo(videoOrPlaylist)
12 if (videoFile.isHLS()) {
13 return generateVideoStreamingPlaylistName(video.uuid, videoFile.resolution)
16 return generateWebTorrentVideoName(video.uuid, videoFile.resolution, videoFile.extname)
19 function generateVideoStreamingPlaylistName (uuid: string, resolution: number) {
20 return `${uuid}-${resolution}-fragmented.mp4`
23 function generateWebTorrentVideoName (uuid: string, resolution: number, extname: string) {
24 return uuid + '-' + resolution + extname
27 function getVideoFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile, isRedundancy = false) {
28 if (videoFile.isHLS()) {
29 const video = extractVideo(videoOrPlaylist)
31 return join(getHLSDirectory(video), getVideoFilename(videoOrPlaylist, videoFile))
34 const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR
35 return join(baseDir, getVideoFilename(videoOrPlaylist, videoFile))
38 // ################## Streaming playlist ##################
40 function getHLSDirectory (video: MVideoUUID, isRedundancy = false) {
41 const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_STREAMING_PLAYLIST_DIRECTORY
43 return join(baseDir, video.uuid)
46 // ################## Torrents ##################
48 function getTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
49 const video = extractVideo(videoOrPlaylist)
50 const extension = '.torrent'
52 if (isStreamingPlaylist(videoOrPlaylist)) {
53 return `${video.uuid}-${videoFile.resolution}-${videoOrPlaylist.getStringType()}${extension}`
56 return video.uuid + '-' + videoFile.resolution + extension
59 function getTorrentFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
60 return join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile))
63 // ---------------------------------------------------------------------------
66 generateVideoStreamingPlaylistName,
67 generateWebTorrentVideoName,