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 // ---------------------------------------------------------------------------
47 function storeWebTorrentFile (video: MVideo, file: MVideoFile) {
49 inputPath: VideoPathManager.Instance.getFSVideoFileOutputPath(video, file),
50 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
51 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
52 isPrivate: video.hasPrivateStaticPath()
56 // ---------------------------------------------------------------------------
58 async function updateWebTorrentFileACL (video: MVideo, file: MVideoFile) {
59 await updateObjectACL({
60 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
61 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
62 isPrivate: video.hasPrivateStaticPath()
66 async function updateHLSFilesACL (playlist: MStreamingPlaylistVideo) {
67 await updatePrefixACL({
68 prefix: generateHLSObjectBaseStorageKey(playlist),
69 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
70 isPrivate: playlist.Video.hasPrivateStaticPath()
74 // ---------------------------------------------------------------------------
76 function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) {
77 return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
80 function removeHLSFileObjectStorageByFilename (playlist: MStreamingPlaylistVideo, filename: string) {
81 return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
84 function removeHLSFileObjectStorageByPath (playlist: MStreamingPlaylistVideo, path: string) {
85 return removeObject(generateHLSObjectStorageKey(playlist, basename(path)), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
88 function removeHLSFileObjectStorageByFullKey (key: string) {
89 return removeObjectByFullKey(key, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
92 // ---------------------------------------------------------------------------
94 function removeWebTorrentObjectStorage (videoFile: MVideoFile) {
95 return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS)
98 // ---------------------------------------------------------------------------
100 async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) {
101 const key = generateHLSObjectStorageKey(playlist, filename)
103 logger.info('Fetching HLS file %s from object storage to %s.', key, destination, lTags())
105 await makeAvailable({
108 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS
114 async function makeWebTorrentFileAvailable (filename: string, destination: string) {
115 const key = generateWebTorrentObjectStorageKey(filename)
117 logger.info('Fetching WebTorrent file %s from object storage to %s.', key, destination, lTags())
119 await makeAvailable({
122 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS
128 // ---------------------------------------------------------------------------
130 function getWebTorrentFileReadStream (options: {
134 const { filename, rangeHeader } = options
136 const key = generateWebTorrentObjectStorageKey(filename)
138 return createObjectReadStream({
140 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
145 function getHLSFileReadStream (options: {
146 playlist: MStreamingPlaylistVideo
150 const { playlist, filename, rangeHeader } = options
152 const key = generateHLSObjectStorageKey(playlist, filename)
154 return createObjectReadStream({
156 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
161 // ---------------------------------------------------------------------------
167 storeHLSFileFromFilename,
168 storeHLSFileFromPath,
170 updateWebTorrentFileACL,
173 removeHLSObjectStorage,
174 removeHLSFileObjectStorageByFilename,
175 removeHLSFileObjectStorageByPath,
176 removeHLSFileObjectStorageByFullKey,
178 removeWebTorrentObjectStorage,
180 makeWebTorrentFileAvailable,
181 makeHLSFileAvailable,
183 getWebTorrentFileReadStream,