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