1 import { basename, join } from 'path'
2 import { logger } from '@server/helpers/logger'
3 import { CONFIG } from '@server/initializers/config'
4 import { MStreamingPlaylistVideo, MVideo, MVideoFile } from '@server/types/models'
5 import { getHLSDirectory } from '../paths'
6 import { VideoPathManager } from '../video-path-manager'
7 import { generateHLSObjectBaseStorageKey, generateHLSObjectStorageKey, generateWebTorrentObjectStorageKey } from './keys'
9 createObjectReadStream,
14 removeObjectByFullKey,
21 function listHLSFileKeysOf (playlist: MStreamingPlaylistVideo) {
22 return listKeysOfPrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
25 // ---------------------------------------------------------------------------
27 function storeHLSFileFromFilename (playlist: MStreamingPlaylistVideo, filename: string) {
29 inputPath: join(getHLSDirectory(playlist.Video), filename),
30 objectStorageKey: generateHLSObjectStorageKey(playlist, filename),
31 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
32 isPrivate: playlist.Video.hasPrivateStaticPath()
36 function storeHLSFileFromPath (playlist: MStreamingPlaylistVideo, path: string) {
39 objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)),
40 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
41 isPrivate: playlist.Video.hasPrivateStaticPath()
45 function storeHLSFileFromContent (playlist: MStreamingPlaylistVideo, path: string, content: string) {
48 objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)),
49 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
50 isPrivate: playlist.Video.hasPrivateStaticPath()
54 // ---------------------------------------------------------------------------
56 function storeWebTorrentFile (video: MVideo, file: MVideoFile) {
58 inputPath: VideoPathManager.Instance.getFSVideoFileOutputPath(video, file),
59 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
60 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
61 isPrivate: video.hasPrivateStaticPath()
65 // ---------------------------------------------------------------------------
67 async function updateWebTorrentFileACL (video: MVideo, file: MVideoFile) {
68 await updateObjectACL({
69 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
70 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
71 isPrivate: video.hasPrivateStaticPath()
75 async function updateHLSFilesACL (playlist: MStreamingPlaylistVideo) {
76 await updatePrefixACL({
77 prefix: generateHLSObjectBaseStorageKey(playlist),
78 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
79 isPrivate: playlist.Video.hasPrivateStaticPath()
83 // ---------------------------------------------------------------------------
85 function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) {
86 return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
89 function removeHLSFileObjectStorageByFilename (playlist: MStreamingPlaylistVideo, filename: string) {
90 return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
93 function removeHLSFileObjectStorageByPath (playlist: MStreamingPlaylistVideo, path: string) {
94 return removeObject(generateHLSObjectStorageKey(playlist, basename(path)), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
97 function removeHLSFileObjectStorageByFullKey (key: string) {
98 return removeObjectByFullKey(key, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
101 // ---------------------------------------------------------------------------
103 function removeWebTorrentObjectStorage (videoFile: MVideoFile) {
104 return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS)
107 // ---------------------------------------------------------------------------
109 async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) {
110 const key = generateHLSObjectStorageKey(playlist, filename)
112 logger.info('Fetching HLS file %s from object storage to %s.', key, destination, lTags())
114 await makeAvailable({
117 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS
123 async function makeWebTorrentFileAvailable (filename: string, destination: string) {
124 const key = generateWebTorrentObjectStorageKey(filename)
126 logger.info('Fetching WebTorrent file %s from object storage to %s.', key, destination, lTags())
128 await makeAvailable({
131 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS
137 // ---------------------------------------------------------------------------
139 function getWebTorrentFileReadStream (options: {
143 const { filename, rangeHeader } = options
145 const key = generateWebTorrentObjectStorageKey(filename)
147 return createObjectReadStream({
149 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
154 function getHLSFileReadStream (options: {
155 playlist: MStreamingPlaylistVideo
159 const { playlist, filename, rangeHeader } = options
161 const key = generateHLSObjectStorageKey(playlist, filename)
163 return createObjectReadStream({
165 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
170 // ---------------------------------------------------------------------------
176 storeHLSFileFromFilename,
177 storeHLSFileFromPath,
178 storeHLSFileFromContent,
180 updateWebTorrentFileACL,
183 removeHLSObjectStorage,
184 removeHLSFileObjectStorageByFilename,
185 removeHLSFileObjectStorageByPath,
186 removeHLSFileObjectStorageByFullKey,
188 removeWebTorrentObjectStorage,
190 makeWebTorrentFileAvailable,
191 makeHLSFileAvailable,
193 getWebTorrentFileReadStream,