2024-02-12 03:47:52 -06:00
|
|
|
import { FileStorage } from '@peertube/peertube-models'
|
2024-03-15 09:47:18 -05:00
|
|
|
import { buildUUID } from '@peertube/peertube-node-utils'
|
2024-07-23 09:38:51 -05:00
|
|
|
import { Awaitable } from '@peertube/peertube-typescript-utils'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
|
|
|
import { extractVideo } from '@server/helpers/video.js'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { DIRECTORIES } from '@server/initializers/constants.js'
|
|
|
|
import {
|
|
|
|
MStreamingPlaylistVideo,
|
|
|
|
MVideo,
|
|
|
|
MVideoFile,
|
|
|
|
MVideoFileStreamingPlaylistVideo,
|
2024-07-23 09:38:51 -05:00
|
|
|
MVideoFileVideo,
|
|
|
|
MVideoWithFile
|
2023-07-31 07:34:36 -05:00
|
|
|
} from '@server/types/models/index.js'
|
2024-03-15 09:47:18 -05:00
|
|
|
import { Mutex } from 'async-mutex'
|
|
|
|
import { remove } from 'fs-extra/esm'
|
|
|
|
import { extname, join } from 'path'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { makeHLSFileAvailable, makeWebVideoFileAvailable } from './object-storage/index.js'
|
|
|
|
import { getHLSDirectory, getHLSRedundancyDirectory, getHlsResolutionPlaylistFilename } from './paths.js'
|
|
|
|
import { isVideoInPrivateDirectory } from './video-privacy.js'
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
type MakeAvailableCB <T> = (path: string) => Awaitable<T>
|
|
|
|
type MakeAvailableMultipleCB <T> = (paths: string[]) => Awaitable<T>
|
|
|
|
type MakeAvailableCreateMethod = { method: () => Awaitable<string>, clean: boolean }
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
const lTags = loggerTagsFactory('video-path-manager')
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
class VideoPathManager {
|
|
|
|
|
|
|
|
private static instance: VideoPathManager
|
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
// Key is a video UUID
|
|
|
|
private readonly videoFileMutexStore = new Map<string, Mutex>()
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
private constructor () {}
|
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
getFSHLSOutputPath (video: MVideo, filename?: string) {
|
2021-08-17 01:26:20 -05:00
|
|
|
const base = getHLSDirectory(video)
|
|
|
|
if (!filename) return base
|
|
|
|
|
|
|
|
return join(base, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
getFSRedundancyVideoFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
|
|
|
|
if (videoFile.isHLS()) {
|
|
|
|
const video = extractVideo(videoOrPlaylist)
|
|
|
|
|
|
|
|
return join(getHLSRedundancyDirectory(video), videoFile.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(CONFIG.STORAGE.REDUNDANCY_DIR, videoFile.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
getFSVideoFileOutputPath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
|
2022-10-12 09:09:02 -05:00
|
|
|
const video = extractVideo(videoOrPlaylist)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
if (videoFile.isHLS()) {
|
2021-08-17 01:26:20 -05:00
|
|
|
return join(getHLSDirectory(video), videoFile.filename)
|
|
|
|
}
|
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
if (isVideoInPrivateDirectory(video.privacy)) {
|
2024-03-15 09:47:18 -05:00
|
|
|
return join(DIRECTORIES.WEB_VIDEOS.PRIVATE, videoFile.filename)
|
2022-10-12 09:09:02 -05:00
|
|
|
}
|
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
return join(DIRECTORIES.WEB_VIDEOS.PUBLIC, videoFile.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
getFSOriginalVideoFilePath (filename: string) {
|
|
|
|
return join(DIRECTORIES.ORIGINAL_VIDEOS, filename)
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
async makeAvailableVideoFiles <T> (videoFiles: (MVideoFileVideo | MVideoFileStreamingPlaylistVideo)[], cb: MakeAvailableMultipleCB<T>) {
|
|
|
|
const createMethods: MakeAvailableCreateMethod[] = []
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
for (const videoFile of videoFiles) {
|
|
|
|
if (videoFile.storage === FileStorage.FILE_SYSTEM) {
|
|
|
|
createMethods.push({
|
|
|
|
method: () => this.getFSVideoFileOutputPath(videoFile.getVideoOrStreamingPlaylist(), videoFile),
|
|
|
|
clean: false
|
|
|
|
})
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const destination = this.buildTMPDestination(videoFile.filename)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
if (videoFile.isHLS()) {
|
|
|
|
const playlist = (videoFile as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist
|
|
|
|
|
|
|
|
createMethods.push({
|
|
|
|
method: () => makeHLSFileAvailable(playlist, videoFile.filename, destination),
|
|
|
|
clean: true
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
createMethods.push({
|
|
|
|
method: () => makeWebVideoFileAvailable(videoFile.filename, destination),
|
|
|
|
clean: true
|
|
|
|
})
|
|
|
|
}
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
return this.makeAvailableFactory({ createMethods, cbContext: cb })
|
|
|
|
}
|
|
|
|
|
|
|
|
async makeAvailableVideoFile <T> (videoFile: MVideoFileVideo | MVideoFileStreamingPlaylistVideo, cb: MakeAvailableCB<T>) {
|
|
|
|
return this.makeAvailableVideoFiles([ videoFile ], paths => cb(paths[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
async makeAvailableMaxQualityFiles <T> (
|
|
|
|
video: MVideoWithFile,
|
|
|
|
cb: (options: { videoPath: string, separatedAudioPath: string }) => Awaitable<T>
|
|
|
|
) {
|
|
|
|
const { videoFile, separatedAudioFile } = video.getMaxQualityAudioAndVideoFiles()
|
|
|
|
|
|
|
|
const files = [ videoFile ]
|
|
|
|
if (separatedAudioFile) files.push(separatedAudioFile)
|
|
|
|
|
|
|
|
return this.makeAvailableVideoFiles(files, ([ videoPath, separatedAudioPath ]) => {
|
|
|
|
return cb({ videoPath, separatedAudioPath })
|
|
|
|
})
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-11-18 07:35:08 -06:00
|
|
|
async makeAvailableResolutionPlaylistFile <T> (videoFile: MVideoFileStreamingPlaylistVideo, cb: MakeAvailableCB<T>) {
|
2021-08-17 01:26:20 -05:00
|
|
|
const filename = getHlsResolutionPlaylistFilename(videoFile.filename)
|
|
|
|
|
2024-02-12 03:47:52 -06:00
|
|
|
if (videoFile.storage === FileStorage.FILE_SYSTEM) {
|
2024-07-23 09:38:51 -05:00
|
|
|
return this.makeAvailableFactory({
|
|
|
|
createMethods: [
|
|
|
|
{
|
|
|
|
method: () => join(getHLSDirectory(videoFile.getVideo()), filename),
|
|
|
|
clean: false
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cbContext: paths => cb(paths[0])
|
|
|
|
})
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 07:35:08 -06:00
|
|
|
const playlist = videoFile.VideoStreamingPlaylist
|
2024-07-23 09:38:51 -05:00
|
|
|
return this.makeAvailableFactory({
|
|
|
|
createMethods: [
|
|
|
|
{
|
|
|
|
method: () => makeHLSFileAvailable(playlist, filename, this.buildTMPDestination(filename)),
|
|
|
|
clean: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cbContext: paths => cb(paths[0])
|
|
|
|
})
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async makeAvailablePlaylistFile <T> (playlist: MStreamingPlaylistVideo, filename: string, cb: MakeAvailableCB<T>) {
|
2024-02-12 03:47:52 -06:00
|
|
|
if (playlist.storage === FileStorage.FILE_SYSTEM) {
|
2024-07-23 09:38:51 -05:00
|
|
|
return this.makeAvailableFactory({
|
|
|
|
createMethods: [
|
|
|
|
{
|
|
|
|
method: () => join(getHLSDirectory(playlist.Video), filename),
|
|
|
|
clean: false
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cbContext: paths => cb(paths[0])
|
|
|
|
})
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
return this.makeAvailableFactory({
|
|
|
|
createMethods: [
|
|
|
|
{
|
|
|
|
method: () => makeHLSFileAvailable(playlist, filename, this.buildTMPDestination(filename)),
|
|
|
|
clean: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cbContext: paths => cb(paths[0])
|
|
|
|
})
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
async lockFiles (videoUUID: string) {
|
|
|
|
if (!this.videoFileMutexStore.has(videoUUID)) {
|
|
|
|
this.videoFileMutexStore.set(videoUUID, new Mutex())
|
|
|
|
}
|
|
|
|
|
|
|
|
const mutex = this.videoFileMutexStore.get(videoUUID)
|
|
|
|
const releaser = await mutex.acquire()
|
|
|
|
|
|
|
|
logger.debug('Locked files of %s.', videoUUID, lTags(videoUUID))
|
|
|
|
|
|
|
|
return releaser
|
|
|
|
}
|
|
|
|
|
|
|
|
unlockFiles (videoUUID: string) {
|
|
|
|
const mutex = this.videoFileMutexStore.get(videoUUID)
|
|
|
|
|
|
|
|
mutex.release()
|
|
|
|
|
|
|
|
logger.debug('Released lockfiles of %s.', videoUUID, lTags(videoUUID))
|
|
|
|
}
|
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
private async makeAvailableFactory <T> (options: {
|
|
|
|
createMethods: MakeAvailableCreateMethod[]
|
|
|
|
cbContext: MakeAvailableMultipleCB<T>
|
|
|
|
}) {
|
|
|
|
const { cbContext, createMethods } = options
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
let result: T
|
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
const created: { destination: string, clean: boolean }[] = []
|
|
|
|
|
|
|
|
const cleanup = async () => {
|
|
|
|
for (const { destination, clean } of created) {
|
|
|
|
if (!destination || !clean) continue
|
|
|
|
|
|
|
|
try {
|
|
|
|
await remove(destination)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot remove ' + destination, { err })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const { method, clean } of createMethods) {
|
|
|
|
created.push({
|
|
|
|
destination: await method(),
|
|
|
|
clean
|
|
|
|
})
|
|
|
|
}
|
2021-08-17 01:26:20 -05:00
|
|
|
|
|
|
|
try {
|
2024-07-23 09:38:51 -05:00
|
|
|
result = await cbContext(created.map(c => c.destination))
|
2021-08-17 01:26:20 -05:00
|
|
|
} catch (err) {
|
2024-07-23 09:38:51 -05:00
|
|
|
await cleanup()
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
await cleanup()
|
2021-08-17 01:26:20 -05:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-07-23 09:38:51 -05:00
|
|
|
buildTMPDestination (filename: string) {
|
2021-08-17 01:26:20 -05:00
|
|
|
return join(CONFIG.STORAGE.TMP_DIR, buildUUID() + extname(filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
VideoPathManager
|
|
|
|
}
|