]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-paths.ts
Refactor auth flow
[github/Chocobozzz/PeerTube.git] / server / lib / video-paths.ts
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'
6
7 // ################## Video file name ##################
8
9 function generateVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, isHls: boolean, resolution: number, extname: string) {
10 const video = extractVideo(videoOrPlaylist)
11
12 // FIXME: use a generated uuid instead, that will break compatibility with PeerTube < 3.1
13 // const uuid = uuidv4()
14 const uuid = video.uuid
15
16 if (isHls) {
17 return generateVideoStreamingPlaylistName(uuid, resolution)
18 }
19
20 return generateWebTorrentVideoName(uuid, resolution, extname)
21 }
22
23 function generateVideoStreamingPlaylistName (uuid: string, resolution: number) {
24 return `${uuid}-${resolution}-fragmented.mp4`
25 }
26
27 function generateWebTorrentVideoName (uuid: string, resolution: number, extname: string) {
28 return uuid + '-' + resolution + extname
29 }
30
31 function getVideoFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile, isRedundancy = false) {
32 if (videoFile.isHLS()) {
33 const video = extractVideo(videoOrPlaylist)
34
35 return join(getHLSDirectory(video), videoFile.filename)
36 }
37
38 const baseDir = isRedundancy
39 ? CONFIG.STORAGE.REDUNDANCY_DIR
40 : CONFIG.STORAGE.VIDEOS_DIR
41
42 return join(baseDir, videoFile.filename)
43 }
44
45 // ################## Redundancy ##################
46
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
50 }
51
52 function generateWebTorrentRedundancyUrl (file: MVideoFile) {
53 return WEBSERVER.URL + STATIC_PATHS.REDUNDANCY + file.filename
54 }
55
56 // ################## Streaming playlist ##################
57
58 function getHLSDirectory (video: MVideoUUID, isRedundancy = false) {
59 const baseDir = isRedundancy
60 ? HLS_REDUNDANCY_DIRECTORY
61 : HLS_STREAMING_PLAYLIST_DIRECTORY
62
63 return join(baseDir, video.uuid)
64 }
65
66 // ################## Torrents ##################
67
68 function generateTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, resolution: number) {
69 const video = extractVideo(videoOrPlaylist)
70 const extension = '.torrent'
71
72 // FIXME: use a generated uuid instead, that will break compatibility with PeerTube < 3.1
73 // const uuid = uuidv4()
74 const uuid = video.uuid
75
76 if (isStreamingPlaylist(videoOrPlaylist)) {
77 return `${uuid}-${resolution}-${videoOrPlaylist.getStringType()}${extension}`
78 }
79
80 return uuid + '-' + resolution + extension
81 }
82
83 function getTorrentFilePath (videoFile: MVideoFile) {
84 return join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
85 }
86
87 // ################## Meta data ##################
88
89 function getLocalVideoFileMetadataUrl (video: MVideoUUID, videoFile: MVideoFile) {
90 const path = '/api/v1/videos/'
91
92 return WEBSERVER.URL + path + video.uuid + '/metadata/' + videoFile.id
93 }
94
95 // ---------------------------------------------------------------------------
96
97 export {
98 generateVideoStreamingPlaylistName,
99 generateWebTorrentVideoName,
100 generateVideoFilename,
101 getVideoFilePath,
102
103 generateTorrentFileName,
104 getTorrentFilePath,
105
106 getHLSDirectory,
107
108 getLocalVideoFileMetadataUrl,
109
110 generateWebTorrentRedundancyUrl,
111 generateHLSRedundancyUrl
112 }