]>
Commit | Line | Data |
---|---|---|
cfd57d2c | 1 | import { basename, join } from 'path' |
0305db28 JB |
2 | import { logger } from '@server/helpers/logger' |
3 | import { CONFIG } from '@server/initializers/config' | |
3545e72c | 4 | import { MStreamingPlaylistVideo, MVideo, MVideoFile } from '@server/types/models' |
0305db28 | 5 | import { getHLSDirectory } from '../paths' |
3545e72c | 6 | import { VideoPathManager } from '../video-path-manager' |
0305db28 | 7 | import { generateHLSObjectBaseStorageKey, generateHLSObjectStorageKey, generateWebTorrentObjectStorageKey } from './keys' |
9ab330b9 C |
8 | import { |
9 | createObjectReadStream, | |
10 | listKeysOfPrefix, | |
11 | lTags, | |
12 | makeAvailable, | |
13 | removeObject, | |
aa887096 | 14 | removeObjectByFullKey, |
9ab330b9 | 15 | removePrefix, |
ec720aff | 16 | storeContent, |
9ab330b9 C |
17 | storeObject, |
18 | updateObjectACL, | |
19 | updatePrefixACL | |
20 | } from './shared' | |
0305db28 | 21 | |
cfd57d2c C |
22 | function listHLSFileKeysOf (playlist: MStreamingPlaylistVideo) { |
23 | return listKeysOfPrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS) | |
24 | } | |
25 | ||
26 | // --------------------------------------------------------------------------- | |
27 | ||
28 | function storeHLSFileFromFilename (playlist: MStreamingPlaylistVideo, filename: string) { | |
0305db28 | 29 | return storeObject({ |
cfd57d2c | 30 | inputPath: join(getHLSDirectory(playlist.Video), filename), |
ad5db104 | 31 | objectStorageKey: generateHLSObjectStorageKey(playlist, filename), |
9ab330b9 C |
32 | bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS, |
33 | isPrivate: playlist.Video.hasPrivateStaticPath() | |
0305db28 JB |
34 | }) |
35 | } | |
36 | ||
cfd57d2c C |
37 | function storeHLSFileFromPath (playlist: MStreamingPlaylistVideo, path: string) { |
38 | return storeObject({ | |
39 | inputPath: path, | |
40 | objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)), | |
9ab330b9 C |
41 | bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS, |
42 | isPrivate: playlist.Video.hasPrivateStaticPath() | |
cfd57d2c C |
43 | }) |
44 | } | |
45 | ||
34023e12 | 46 | function storeHLSFileFromContent (playlist: MStreamingPlaylistVideo, path: string, content: string) { |
ec720aff C |
47 | return storeContent({ |
48 | content, | |
34023e12 C |
49 | inputPath: path, |
50 | objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)), | |
51 | bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS, | |
52 | isPrivate: playlist.Video.hasPrivateStaticPath() | |
53 | }) | |
54 | } | |
55 | ||
cfd57d2c C |
56 | // --------------------------------------------------------------------------- |
57 | ||
3545e72c | 58 | function storeWebTorrentFile (video: MVideo, file: MVideoFile) { |
0305db28 | 59 | return storeObject({ |
3545e72c C |
60 | inputPath: VideoPathManager.Instance.getFSVideoFileOutputPath(video, file), |
61 | objectStorageKey: generateWebTorrentObjectStorageKey(file.filename), | |
9ab330b9 C |
62 | bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS, |
63 | isPrivate: video.hasPrivateStaticPath() | |
64 | }) | |
65 | } | |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | ||
8180f604 C |
69 | async function updateWebTorrentFileACL (video: MVideo, file: MVideoFile) { |
70 | await updateObjectACL({ | |
9ab330b9 C |
71 | objectStorageKey: generateWebTorrentObjectStorageKey(file.filename), |
72 | bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS, | |
73 | isPrivate: video.hasPrivateStaticPath() | |
74 | }) | |
75 | } | |
76 | ||
8180f604 C |
77 | async function updateHLSFilesACL (playlist: MStreamingPlaylistVideo) { |
78 | await updatePrefixACL({ | |
9ab330b9 C |
79 | prefix: generateHLSObjectBaseStorageKey(playlist), |
80 | bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS, | |
81 | isPrivate: playlist.Video.hasPrivateStaticPath() | |
0305db28 JB |
82 | }) |
83 | } | |
84 | ||
cfd57d2c C |
85 | // --------------------------------------------------------------------------- |
86 | ||
ad5db104 C |
87 | function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) { |
88 | return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS) | |
0305db28 JB |
89 | } |
90 | ||
aa887096 | 91 | function removeHLSFileObjectStorageByFilename (playlist: MStreamingPlaylistVideo, filename: string) { |
7b6b445d C |
92 | return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS) |
93 | } | |
94 | ||
aa887096 C |
95 | function removeHLSFileObjectStorageByPath (playlist: MStreamingPlaylistVideo, path: string) { |
96 | return removeObject(generateHLSObjectStorageKey(playlist, basename(path)), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS) | |
97 | } | |
98 | ||
99 | function removeHLSFileObjectStorageByFullKey (key: string) { | |
100 | return removeObjectByFullKey(key, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS) | |
101 | } | |
102 | ||
cfd57d2c C |
103 | // --------------------------------------------------------------------------- |
104 | ||
0305db28 JB |
105 | function removeWebTorrentObjectStorage (videoFile: MVideoFile) { |
106 | return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS) | |
107 | } | |
108 | ||
cfd57d2c C |
109 | // --------------------------------------------------------------------------- |
110 | ||
ad5db104 C |
111 | async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) { |
112 | const key = generateHLSObjectStorageKey(playlist, filename) | |
0305db28 JB |
113 | |
114 | logger.info('Fetching HLS file %s from object storage to %s.', key, destination, lTags()) | |
115 | ||
116 | await makeAvailable({ | |
117 | key, | |
118 | destination, | |
119 | bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS | |
120 | }) | |
121 | ||
122 | return destination | |
123 | } | |
124 | ||
125 | async function makeWebTorrentFileAvailable (filename: string, destination: string) { | |
126 | const key = generateWebTorrentObjectStorageKey(filename) | |
127 | ||
128 | logger.info('Fetching WebTorrent file %s from object storage to %s.', key, destination, lTags()) | |
129 | ||
130 | await makeAvailable({ | |
131 | key, | |
132 | destination, | |
133 | bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS | |
134 | }) | |
135 | ||
136 | return destination | |
137 | } | |
138 | ||
cfd57d2c C |
139 | // --------------------------------------------------------------------------- |
140 | ||
9ab330b9 C |
141 | function getWebTorrentFileReadStream (options: { |
142 | filename: string | |
143 | rangeHeader: string | |
144 | }) { | |
145 | const { filename, rangeHeader } = options | |
146 | ||
147 | const key = generateWebTorrentObjectStorageKey(filename) | |
148 | ||
149 | return createObjectReadStream({ | |
150 | key, | |
151 | bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS, | |
152 | rangeHeader | |
153 | }) | |
154 | } | |
155 | ||
156 | function getHLSFileReadStream (options: { | |
157 | playlist: MStreamingPlaylistVideo | |
158 | filename: string | |
159 | rangeHeader: string | |
160 | }) { | |
161 | const { playlist, filename, rangeHeader } = options | |
162 | ||
163 | const key = generateHLSObjectStorageKey(playlist, filename) | |
164 | ||
165 | return createObjectReadStream({ | |
166 | key, | |
167 | bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS, | |
168 | rangeHeader | |
169 | }) | |
170 | } | |
171 | ||
172 | // --------------------------------------------------------------------------- | |
173 | ||
0305db28 | 174 | export { |
cfd57d2c C |
175 | listHLSFileKeysOf, |
176 | ||
0305db28 | 177 | storeWebTorrentFile, |
cfd57d2c C |
178 | storeHLSFileFromFilename, |
179 | storeHLSFileFromPath, | |
34023e12 | 180 | storeHLSFileFromContent, |
0305db28 | 181 | |
9ab330b9 C |
182 | updateWebTorrentFileACL, |
183 | updateHLSFilesACL, | |
184 | ||
0305db28 | 185 | removeHLSObjectStorage, |
aa887096 C |
186 | removeHLSFileObjectStorageByFilename, |
187 | removeHLSFileObjectStorageByPath, | |
188 | removeHLSFileObjectStorageByFullKey, | |
189 | ||
0305db28 JB |
190 | removeWebTorrentObjectStorage, |
191 | ||
192 | makeWebTorrentFileAvailable, | |
9ab330b9 C |
193 | makeHLSFileAvailable, |
194 | ||
195 | getWebTorrentFileReadStream, | |
196 | getHLSFileReadStream | |
0305db28 | 197 | } |