2022-10-12 16:09:02 +02:00
|
|
|
import { Mutex } from 'async-mutex'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { remove } from 'fs-extra/esm'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { extname, join } from 'path'
|
2024-02-12 10:47:52 +01:00
|
|
|
import { FileStorage } from '@peertube/peertube-models'
|
2023-07-31 14:34:36 +02: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,
|
|
|
|
MVideoFileVideo
|
|
|
|
} from '@server/types/models/index.js'
|
|
|
|
import { buildUUID } from '@peertube/peertube-node-utils'
|
|
|
|
import { makeHLSFileAvailable, makeWebVideoFileAvailable } from './object-storage/index.js'
|
|
|
|
import { getHLSDirectory, getHLSRedundancyDirectory, getHlsResolutionPlaylistFilename } from './paths.js'
|
|
|
|
import { isVideoInPrivateDirectory } from './video-privacy.js'
|
2021-08-17 08:26:20 +02:00
|
|
|
|
|
|
|
type MakeAvailableCB <T> = (path: string) => Promise<T> | T
|
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
const lTags = loggerTagsFactory('video-path-manager')
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
class VideoPathManager {
|
|
|
|
|
|
|
|
private static instance: VideoPathManager
|
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
// Key is a video UUID
|
|
|
|
private readonly videoFileMutexStore = new Map<string, Mutex>()
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
private constructor () {}
|
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
getFSHLSOutputPath (video: MVideo, filename?: string) {
|
2021-08-17 08:26:20 +02: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 16:09:02 +02:00
|
|
|
const video = extractVideo(videoOrPlaylist)
|
2021-08-17 08:26:20 +02:00
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
if (videoFile.isHLS()) {
|
2021-08-17 08:26:20 +02:00
|
|
|
return join(getHLSDirectory(video), videoFile.filename)
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
if (isVideoInPrivateDirectory(video.privacy)) {
|
|
|
|
return join(DIRECTORIES.VIDEOS.PRIVATE, videoFile.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(DIRECTORIES.VIDEOS.PUBLIC, videoFile.filename)
|
2021-08-17 08:26:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:35:08 +01:00
|
|
|
async makeAvailableVideoFile <T> (videoFile: MVideoFileVideo | MVideoFileStreamingPlaylistVideo, cb: MakeAvailableCB<T>) {
|
2024-02-12 10:47:52 +01:00
|
|
|
if (videoFile.storage === FileStorage.FILE_SYSTEM) {
|
2021-08-17 08:26:20 +02:00
|
|
|
return this.makeAvailableFactory(
|
2021-11-18 14:35:08 +01:00
|
|
|
() => this.getFSVideoFileOutputPath(videoFile.getVideoOrStreamingPlaylist(), videoFile),
|
2021-08-17 08:26:20 +02:00
|
|
|
false,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const destination = this.buildTMPDestination(videoFile.filename)
|
|
|
|
|
|
|
|
if (videoFile.isHLS()) {
|
2021-11-18 14:35:08 +01:00
|
|
|
const playlist = (videoFile as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist
|
2021-08-17 08:26:20 +02:00
|
|
|
|
|
|
|
return this.makeAvailableFactory(
|
2021-11-18 14:35:08 +01:00
|
|
|
() => makeHLSFileAvailable(playlist, videoFile.filename, destination),
|
2021-08-17 08:26:20 +02:00
|
|
|
true,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.makeAvailableFactory(
|
2023-07-11 09:21:13 +02:00
|
|
|
() => makeWebVideoFileAvailable(videoFile.filename, destination),
|
2021-08-17 08:26:20 +02:00
|
|
|
true,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:35:08 +01:00
|
|
|
async makeAvailableResolutionPlaylistFile <T> (videoFile: MVideoFileStreamingPlaylistVideo, cb: MakeAvailableCB<T>) {
|
2021-08-17 08:26:20 +02:00
|
|
|
const filename = getHlsResolutionPlaylistFilename(videoFile.filename)
|
|
|
|
|
2024-02-12 10:47:52 +01:00
|
|
|
if (videoFile.storage === FileStorage.FILE_SYSTEM) {
|
2021-08-17 08:26:20 +02:00
|
|
|
return this.makeAvailableFactory(
|
2021-11-18 14:35:08 +01:00
|
|
|
() => join(getHLSDirectory(videoFile.getVideo()), filename),
|
2021-08-17 08:26:20 +02:00
|
|
|
false,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:35:08 +01:00
|
|
|
const playlist = videoFile.VideoStreamingPlaylist
|
2021-08-17 08:26:20 +02:00
|
|
|
return this.makeAvailableFactory(
|
2021-11-18 14:35:08 +01:00
|
|
|
() => makeHLSFileAvailable(playlist, filename, this.buildTMPDestination(filename)),
|
2021-08-17 08:26:20 +02:00
|
|
|
true,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async makeAvailablePlaylistFile <T> (playlist: MStreamingPlaylistVideo, filename: string, cb: MakeAvailableCB<T>) {
|
2024-02-12 10:47:52 +01:00
|
|
|
if (playlist.storage === FileStorage.FILE_SYSTEM) {
|
2021-08-17 08:26:20 +02:00
|
|
|
return this.makeAvailableFactory(
|
|
|
|
() => join(getHLSDirectory(playlist.Video), filename),
|
|
|
|
false,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.makeAvailableFactory(
|
2021-11-18 14:35:08 +01:00
|
|
|
() => makeHLSFileAvailable(playlist, filename, this.buildTMPDestination(filename)),
|
2021-08-17 08:26:20 +02:00
|
|
|
true,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:09:02 +02: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))
|
|
|
|
}
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
private async makeAvailableFactory <T> (method: () => Promise<string> | string, clean: boolean, cb: MakeAvailableCB<T>) {
|
|
|
|
let result: T
|
|
|
|
|
|
|
|
const destination = await method()
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = await cb(destination)
|
|
|
|
} catch (err) {
|
|
|
|
if (destination && clean) await remove(destination)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clean) await remove(destination)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildTMPDestination (filename: string) {
|
|
|
|
return join(CONFIG.STORAGE.TMP_DIR, buildUUID() + extname(filename))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
VideoPathManager
|
|
|
|
}
|