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,
20 function listHLSFileKeysOf (playlist: MStreamingPlaylistVideo) {
21 return listKeysOfPrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
24 // ---------------------------------------------------------------------------
26 function storeHLSFileFromFilename (playlist: MStreamingPlaylistVideo, filename: string) {
28 inputPath: join(getHLSDirectory(playlist.Video), filename),
29 objectStorageKey: generateHLSObjectStorageKey(playlist, filename),
30 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
31 isPrivate: playlist.Video.hasPrivateStaticPath()
35 function storeHLSFileFromPath (playlist: MStreamingPlaylistVideo, path: string) {
38 objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)),
39 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
40 isPrivate: playlist.Video.hasPrivateStaticPath()
44 // ---------------------------------------------------------------------------
46 function storeWebTorrentFile (video: MVideo, file: MVideoFile) {
48 inputPath: VideoPathManager.Instance.getFSVideoFileOutputPath(video, file),
49 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
50 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
51 isPrivate: video.hasPrivateStaticPath()
55 // ---------------------------------------------------------------------------
57 function updateWebTorrentFileACL (video: MVideo, file: MVideoFile) {
58 return updateObjectACL({
59 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
60 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
61 isPrivate: video.hasPrivateStaticPath()
65 function updateHLSFilesACL (playlist: MStreamingPlaylistVideo) {
66 return updatePrefixACL({
67 prefix: generateHLSObjectBaseStorageKey(playlist),
68 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
69 isPrivate: playlist.Video.hasPrivateStaticPath()
73 // ---------------------------------------------------------------------------
75 function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) {
76 return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
79 function removeHLSFileObjectStorage (playlist: MStreamingPlaylistVideo, filename: string) {
80 return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
83 // ---------------------------------------------------------------------------
85 function removeWebTorrentObjectStorage (videoFile: MVideoFile) {
86 return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS)
89 // ---------------------------------------------------------------------------
91 async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) {
92 const key = generateHLSObjectStorageKey(playlist, filename)
94 logger.info('Fetching HLS file %s from object storage to %s.', key, destination, lTags())
99 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS
105 async function makeWebTorrentFileAvailable (filename: string, destination: string) {
106 const key = generateWebTorrentObjectStorageKey(filename)
108 logger.info('Fetching WebTorrent file %s from object storage to %s.', key, destination, lTags())
110 await makeAvailable({
113 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS
119 // ---------------------------------------------------------------------------
121 function getWebTorrentFileReadStream (options: {
125 const { filename, rangeHeader } = options
127 const key = generateWebTorrentObjectStorageKey(filename)
129 return createObjectReadStream({
131 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
136 function getHLSFileReadStream (options: {
137 playlist: MStreamingPlaylistVideo
141 const { playlist, filename, rangeHeader } = options
143 const key = generateHLSObjectStorageKey(playlist, filename)
145 return createObjectReadStream({
147 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
152 // ---------------------------------------------------------------------------
158 storeHLSFileFromFilename,
159 storeHLSFileFromPath,
161 updateWebTorrentFileACL,
164 removeHLSObjectStorage,
165 removeHLSFileObjectStorage,
166 removeWebTorrentObjectStorage,
168 makeWebTorrentFileAvailable,
169 makeHLSFileAvailable,
171 getWebTorrentFileReadStream,