05aaca8afd64ddc104df02be39fcca27dd08cf2a
[oweals/peertube.git] / server / lib / video-paths.ts
1 import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoUUID } from '@server/typings/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'
6
7 // ################## Video file name ##################
8
9 function getVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
10   const video = extractVideo(videoOrPlaylist)
11
12   if (isStreamingPlaylist(videoOrPlaylist)) {
13     return generateVideoStreamingPlaylistName(video.uuid, videoFile.resolution)
14   }
15
16   return generateWebTorrentVideoName(video.uuid, videoFile.resolution, videoFile.extname)
17 }
18
19 function generateVideoStreamingPlaylistName (uuid: string, resolution: number) {
20   return `${uuid}-${resolution}-fragmented.mp4`
21 }
22
23 function generateWebTorrentVideoName (uuid: string, resolution: number, extname: string) {
24   return uuid + '-' + resolution + extname
25 }
26
27 function getVideoFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile, isRedundancy = false) {
28   if (isStreamingPlaylist(videoOrPlaylist)) {
29     const video = extractVideo(videoOrPlaylist)
30     return join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid, getVideoFilename(videoOrPlaylist, videoFile))
31   }
32
33   const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR
34   return join(baseDir, getVideoFilename(videoOrPlaylist, videoFile))
35 }
36
37 // ################## Streaming playlist ##################
38
39 function getHLSDirectory (video: MVideoUUID, isRedundancy = false) {
40   const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_STREAMING_PLAYLIST_DIRECTORY
41
42   return join(baseDir, video.uuid)
43 }
44
45 // ################## Torrents ##################
46
47 function getTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
48   const video = extractVideo(videoOrPlaylist)
49   const extension = '.torrent'
50
51   if (isStreamingPlaylist(videoOrPlaylist)) {
52     return `${video.uuid}-${videoFile.resolution}-${videoOrPlaylist.getStringType()}${extension}`
53   }
54
55   return video.uuid + '-' + videoFile.resolution + extension
56 }
57
58 function getTorrentFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
59   return join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile))
60 }
61
62 // ---------------------------------------------------------------------------
63
64 export {
65   generateVideoStreamingPlaylistName,
66   generateWebTorrentVideoName,
67   getVideoFilename,
68   getVideoFilePath,
69
70   getTorrentFileName,
71   getTorrentFilePath,
72
73   getHLSDirectory
74 }