]>
Commit | Line | Data |
---|---|---|
d7a25329 | 1 | import { join } from 'path' |
8dc8a34e | 2 | import { extractVideo } from '@server/helpers/video' |
90a8bd30 C |
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' | |
d7a25329 C |
6 | |
7 | // ################## Video file name ################## | |
8 | ||
90a8bd30 | 9 | function 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 | ||
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) { | |
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 | ||
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 | |
d7a25329 C |
54 | } |
55 | ||
66fb2aa3 C |
56 | // ################## Streaming playlist ################## |
57 | ||
58 | function 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 | 68 | function 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 | ||
83 | function getTorrentFilePath (videoFile: MVideoFile) { | |
84 | return join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename) | |
d7a25329 C |
85 | } |
86 | ||
90a8bd30 C |
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 | |
d7a25329 C |
93 | } |
94 | ||
95 | // --------------------------------------------------------------------------- | |
96 | ||
97 | export { | |
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 | } |