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,
22 function listHLSFileKeysOf (playlist: MStreamingPlaylistVideo) {
23 return listKeysOfPrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
26 // ---------------------------------------------------------------------------
28 function storeHLSFileFromFilename (playlist: MStreamingPlaylistVideo, filename: string) {
30 inputPath: join(getHLSDirectory(playlist.Video), filename),
31 objectStorageKey: generateHLSObjectStorageKey(playlist, filename),
32 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
33 isPrivate: playlist.Video.hasPrivateStaticPath()
37 function storeHLSFileFromPath (playlist: MStreamingPlaylistVideo, path: string) {
40 objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)),
41 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
42 isPrivate: playlist.Video.hasPrivateStaticPath()
46 function storeHLSFileFromContent (playlist: MStreamingPlaylistVideo, path: string, content: string) {
50 objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)),
51 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
52 isPrivate: playlist.Video.hasPrivateStaticPath()
56 // ---------------------------------------------------------------------------
58 function storeWebTorrentFile (video: MVideo, file: MVideoFile) {
60 inputPath: VideoPathManager.Instance.getFSVideoFileOutputPath(video, file),
61 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
62 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
63 isPrivate: video.hasPrivateStaticPath()
67 // ---------------------------------------------------------------------------
69 async function updateWebTorrentFileACL (video: MVideo, file: MVideoFile) {
70 await updateObjectACL({
71 objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
72 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
73 isPrivate: video.hasPrivateStaticPath()
77 async function updateHLSFilesACL (playlist: MStreamingPlaylistVideo) {
78 await updatePrefixACL({
79 prefix: generateHLSObjectBaseStorageKey(playlist),
80 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
81 isPrivate: playlist.Video.hasPrivateStaticPath()
85 // ---------------------------------------------------------------------------
87 function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) {
88 return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
91 function removeHLSFileObjectStorageByFilename (playlist: MStreamingPlaylistVideo, filename: string) {
92 return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
95 function removeHLSFileObjectStorageByPath (playlist: MStreamingPlaylistVideo, path: string) {
96 return removeObject(generateHLSObjectStorageKey(playlist, basename(path)), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
99 function removeHLSFileObjectStorageByFullKey (key: string) {
100 return removeObjectByFullKey(key, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
103 // ---------------------------------------------------------------------------
105 function removeWebTorrentObjectStorage (videoFile: MVideoFile) {
106 return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS)
109 // ---------------------------------------------------------------------------
111 async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) {
112 const key = generateHLSObjectStorageKey(playlist, filename)
114 logger.info('Fetching HLS file %s from object storage to %s.', key, destination, lTags())
116 await makeAvailable({
119 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS
125 async function makeWebTorrentFileAvailable (filename: string, destination: string) {
126 const key = generateWebTorrentObjectStorageKey(filename)
128 logger.info('Fetching WebTorrent file %s from object storage to %s.', key, destination, lTags())
130 await makeAvailable({
133 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS
139 // ---------------------------------------------------------------------------
141 function getWebTorrentFileReadStream (options: {
145 const { filename, rangeHeader } = options
147 const key = generateWebTorrentObjectStorageKey(filename)
149 return createObjectReadStream({
151 bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
156 function getHLSFileReadStream (options: {
157 playlist: MStreamingPlaylistVideo
161 const { playlist, filename, rangeHeader } = options
163 const key = generateHLSObjectStorageKey(playlist, filename)
165 return createObjectReadStream({
167 bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
172 // ---------------------------------------------------------------------------
178 storeHLSFileFromFilename,
179 storeHLSFileFromPath,
180 storeHLSFileFromContent,
182 updateWebTorrentFileACL,
185 removeHLSObjectStorage,
186 removeHLSFileObjectStorageByFilename,
187 removeHLSFileObjectStorageByPath,
188 removeHLSFileObjectStorageByFullKey,
190 removeWebTorrentObjectStorage,
192 makeWebTorrentFileAvailable,
193 makeHLSFileAvailable,
195 getWebTorrentFileReadStream,