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