2022-10-04 04:03:17 -04:00
|
|
|
import { basename, join } from 'path'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { logger } from '@server/helpers/logger'
|
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2022-10-12 10:09:02 -04:00
|
|
|
import { MStreamingPlaylistVideo, MVideo, MVideoFile } from '@server/types/models'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { getHLSDirectory } from '../paths'
|
2022-10-12 10:09:02 -04:00
|
|
|
import { VideoPathManager } from '../video-path-manager'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { generateHLSObjectBaseStorageKey, generateHLSObjectStorageKey, generateWebTorrentObjectStorageKey } from './keys'
|
2022-10-19 04:43:53 -04:00
|
|
|
import {
|
|
|
|
createObjectReadStream,
|
|
|
|
listKeysOfPrefix,
|
|
|
|
lTags,
|
|
|
|
makeAvailable,
|
|
|
|
removeObject,
|
2022-10-25 08:18:59 -04:00
|
|
|
removeObjectByFullKey,
|
2022-10-19 04:43:53 -04:00
|
|
|
removePrefix,
|
|
|
|
storeObject,
|
|
|
|
updateObjectACL,
|
|
|
|
updatePrefixACL
|
|
|
|
} from './shared'
|
2021-08-17 02:26:20 -04:00
|
|
|
|
2022-10-04 04:03:17 -04:00
|
|
|
function listHLSFileKeysOf (playlist: MStreamingPlaylistVideo) {
|
|
|
|
return listKeysOfPrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function storeHLSFileFromFilename (playlist: MStreamingPlaylistVideo, filename: string) {
|
2021-08-17 02:26:20 -04:00
|
|
|
return storeObject({
|
2022-10-04 04:03:17 -04:00
|
|
|
inputPath: join(getHLSDirectory(playlist.Video), filename),
|
2021-11-18 08:35:08 -05:00
|
|
|
objectStorageKey: generateHLSObjectStorageKey(playlist, filename),
|
2022-10-19 04:43:53 -04:00
|
|
|
bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
|
|
|
|
isPrivate: playlist.Video.hasPrivateStaticPath()
|
2021-08-17 02:26:20 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:03:17 -04:00
|
|
|
function storeHLSFileFromPath (playlist: MStreamingPlaylistVideo, path: string) {
|
|
|
|
return storeObject({
|
|
|
|
inputPath: path,
|
|
|
|
objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)),
|
2022-10-19 04:43:53 -04:00
|
|
|
bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
|
|
|
|
isPrivate: playlist.Video.hasPrivateStaticPath()
|
2022-10-04 04:03:17 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-10-12 10:09:02 -04:00
|
|
|
function storeWebTorrentFile (video: MVideo, file: MVideoFile) {
|
2021-08-17 02:26:20 -04:00
|
|
|
return storeObject({
|
2022-10-12 10:09:02 -04:00
|
|
|
inputPath: VideoPathManager.Instance.getFSVideoFileOutputPath(video, file),
|
|
|
|
objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
|
2022-10-19 04:43:53 -04:00
|
|
|
bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
|
|
|
|
isPrivate: video.hasPrivateStaticPath()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function updateWebTorrentFileACL (video: MVideo, file: MVideoFile) {
|
|
|
|
return updateObjectACL({
|
|
|
|
objectStorageKey: generateWebTorrentObjectStorageKey(file.filename),
|
|
|
|
bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
|
|
|
|
isPrivate: video.hasPrivateStaticPath()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateHLSFilesACL (playlist: MStreamingPlaylistVideo) {
|
|
|
|
return updatePrefixACL({
|
|
|
|
prefix: generateHLSObjectBaseStorageKey(playlist),
|
|
|
|
bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
|
|
|
|
isPrivate: playlist.Video.hasPrivateStaticPath()
|
2021-08-17 02:26:20 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:03:17 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-11-18 08:35:08 -05:00
|
|
|
function removeHLSObjectStorage (playlist: MStreamingPlaylistVideo) {
|
|
|
|
return removePrefix(generateHLSObjectBaseStorageKey(playlist), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
|
2021-08-17 02:26:20 -04:00
|
|
|
}
|
|
|
|
|
2022-10-25 08:18:59 -04:00
|
|
|
function removeHLSFileObjectStorageByFilename (playlist: MStreamingPlaylistVideo, filename: string) {
|
2022-07-25 04:57:16 -04:00
|
|
|
return removeObject(generateHLSObjectStorageKey(playlist, filename), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
|
|
|
|
}
|
|
|
|
|
2022-10-25 08:18:59 -04:00
|
|
|
function removeHLSFileObjectStorageByPath (playlist: MStreamingPlaylistVideo, path: string) {
|
|
|
|
return removeObject(generateHLSObjectStorageKey(playlist, basename(path)), CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeHLSFileObjectStorageByFullKey (key: string) {
|
|
|
|
return removeObjectByFullKey(key, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:03:17 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
function removeWebTorrentObjectStorage (videoFile: MVideoFile) {
|
|
|
|
return removeObject(generateWebTorrentObjectStorageKey(videoFile.filename), CONFIG.OBJECT_STORAGE.VIDEOS)
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:03:17 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-11-18 08:35:08 -05:00
|
|
|
async function makeHLSFileAvailable (playlist: MStreamingPlaylistVideo, filename: string, destination: string) {
|
|
|
|
const key = generateHLSObjectStorageKey(playlist, filename)
|
2021-08-17 02:26:20 -04:00
|
|
|
|
|
|
|
logger.info('Fetching HLS file %s from object storage to %s.', key, destination, lTags())
|
|
|
|
|
|
|
|
await makeAvailable({
|
|
|
|
key,
|
|
|
|
destination,
|
|
|
|
bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS
|
|
|
|
})
|
|
|
|
|
|
|
|
return destination
|
|
|
|
}
|
|
|
|
|
|
|
|
async function makeWebTorrentFileAvailable (filename: string, destination: string) {
|
|
|
|
const key = generateWebTorrentObjectStorageKey(filename)
|
|
|
|
|
|
|
|
logger.info('Fetching WebTorrent file %s from object storage to %s.', key, destination, lTags())
|
|
|
|
|
|
|
|
await makeAvailable({
|
|
|
|
key,
|
|
|
|
destination,
|
|
|
|
bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS
|
|
|
|
})
|
|
|
|
|
|
|
|
return destination
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:03:17 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
function getWebTorrentFileReadStream (options: {
|
|
|
|
filename: string
|
|
|
|
rangeHeader: string
|
|
|
|
}) {
|
|
|
|
const { filename, rangeHeader } = options
|
|
|
|
|
|
|
|
const key = generateWebTorrentObjectStorageKey(filename)
|
|
|
|
|
|
|
|
return createObjectReadStream({
|
|
|
|
key,
|
|
|
|
bucketInfo: CONFIG.OBJECT_STORAGE.VIDEOS,
|
|
|
|
rangeHeader
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHLSFileReadStream (options: {
|
|
|
|
playlist: MStreamingPlaylistVideo
|
|
|
|
filename: string
|
|
|
|
rangeHeader: string
|
|
|
|
}) {
|
|
|
|
const { playlist, filename, rangeHeader } = options
|
|
|
|
|
|
|
|
const key = generateHLSObjectStorageKey(playlist, filename)
|
|
|
|
|
|
|
|
return createObjectReadStream({
|
|
|
|
key,
|
|
|
|
bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
|
|
|
|
rangeHeader
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
export {
|
2022-10-04 04:03:17 -04:00
|
|
|
listHLSFileKeysOf,
|
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
storeWebTorrentFile,
|
2022-10-04 04:03:17 -04:00
|
|
|
storeHLSFileFromFilename,
|
|
|
|
storeHLSFileFromPath,
|
2021-08-17 02:26:20 -04:00
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
updateWebTorrentFileACL,
|
|
|
|
updateHLSFilesACL,
|
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
removeHLSObjectStorage,
|
2022-10-25 08:18:59 -04:00
|
|
|
removeHLSFileObjectStorageByFilename,
|
|
|
|
removeHLSFileObjectStorageByPath,
|
|
|
|
removeHLSFileObjectStorageByFullKey,
|
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
removeWebTorrentObjectStorage,
|
|
|
|
|
|
|
|
makeWebTorrentFileAvailable,
|
2022-10-19 04:43:53 -04:00
|
|
|
makeHLSFileAvailable,
|
|
|
|
|
|
|
|
getWebTorrentFileReadStream,
|
|
|
|
getHLSFileReadStream
|
2021-08-17 02:26:20 -04:00
|
|
|
}
|