2024-02-12 10:47:52 +01:00
|
|
|
import { FileStorage } from '@peertube/peertube-models'
|
2024-03-15 15:47:18 +01:00
|
|
|
import { buildUUID } from '@peertube/peertube-node-utils'
|
2024-07-23 16:38:51 +02:00
|
|
|
import { Awaitable } from '@peertube/peertube-typescript-utils'
|
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,
|
2024-07-23 16:38:51 +02:00
|
|
|
MVideoFileVideo,
|
|
|
|
MVideoWithFile
|
2023-07-31 14:34:36 +02:00
|
|
|
} from '@server/types/models/index.js'
|
2024-03-15 15:47:18 +01:00
|
|
|
import { Mutex } from 'async-mutex'
|
|
|
|
import { remove } from 'fs-extra/esm'
|
|
|
|
import { extname, join } from 'path'
|
2023-07-31 14:34:36 +02: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 08:26:20 +02:00
|
|
|
|
2024-07-23 16:38:51 +02: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 08:26:20 +02:00
|
|
|
|
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)) {
|
2024-03-15 15:47:18 +01:00
|
|
|
return join(DIRECTORIES.WEB_VIDEOS.PRIVATE, videoFile.filename)
|
2022-10-12 16:09:02 +02:00
|
|
|
}
|
|
|
|
|
2024-03-15 15:47:18 +01:00
|
|
|
return join(DIRECTORIES.WEB_VIDEOS.PUBLIC, videoFile.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
getFSOriginalVideoFilePath (filename: string) {
|
|
|
|
return join(DIRECTORIES.ORIGINAL_VIDEOS, filename)
|
2021-08-17 08:26:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
2021-08-17 08:26:20 +02:00
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
async makeAvailableVideoFiles <T> (videoFiles: (MVideoFileVideo | MVideoFileStreamingPlaylistVideo)[], cb: MakeAvailableMultipleCB<T>) {
|
|
|
|
const createMethods: MakeAvailableCreateMethod[] = []
|
2021-08-17 08:26:20 +02:00
|
|
|
|
2024-07-23 16:38:51 +02: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 08:26:20 +02:00
|
|
|
|
2024-07-23 16:38:51 +02: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 08:26:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02: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 08:26:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
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) {
|
2024-07-23 16:38:51 +02:00
|
|
|
return this.makeAvailableFactory({
|
|
|
|
createMethods: [
|
|
|
|
{
|
|
|
|
method: () => join(getHLSDirectory(videoFile.getVideo()), filename),
|
|
|
|
clean: false
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cbContext: paths => cb(paths[0])
|
|
|
|
})
|
2021-08-17 08:26:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:35:08 +01:00
|
|
|
const playlist = videoFile.VideoStreamingPlaylist
|
2024-07-23 16:38:51 +02:00
|
|
|
return this.makeAvailableFactory({
|
|
|
|
createMethods: [
|
|
|
|
{
|
|
|
|
method: () => makeHLSFileAvailable(playlist, filename, this.buildTMPDestination(filename)),
|
|
|
|
clean: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cbContext: paths => cb(paths[0])
|
|
|
|
})
|
2021-08-17 08:26:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async makeAvailablePlaylistFile <T> (playlist: MStreamingPlaylistVideo, filename: string, cb: MakeAvailableCB<T>) {
|
2024-02-12 10:47:52 +01:00
|
|
|
if (playlist.storage === FileStorage.FILE_SYSTEM) {
|
2024-07-23 16:38:51 +02:00
|
|
|
return this.makeAvailableFactory({
|
|
|
|
createMethods: [
|
|
|
|
{
|
|
|
|
method: () => join(getHLSDirectory(playlist.Video), filename),
|
|
|
|
clean: false
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cbContext: paths => cb(paths[0])
|
|
|
|
})
|
2021-08-17 08:26:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
return this.makeAvailableFactory({
|
|
|
|
createMethods: [
|
|
|
|
{
|
|
|
|
method: () => makeHLSFileAvailable(playlist, filename, this.buildTMPDestination(filename)),
|
|
|
|
clean: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cbContext: paths => cb(paths[0])
|
|
|
|
})
|
2021-08-17 08:26:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
private async makeAvailableFactory <T> (options: {
|
|
|
|
createMethods: MakeAvailableCreateMethod[]
|
|
|
|
cbContext: MakeAvailableMultipleCB<T>
|
|
|
|
}) {
|
|
|
|
const { cbContext, createMethods } = options
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
let result: T
|
|
|
|
|
2024-07-23 16:38:51 +02: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 08:26:20 +02:00
|
|
|
|
|
|
|
try {
|
2024-07-23 16:38:51 +02:00
|
|
|
result = await cbContext(created.map(c => c.destination))
|
2021-08-17 08:26:20 +02:00
|
|
|
} catch (err) {
|
2024-07-23 16:38:51 +02:00
|
|
|
await cleanup()
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
await cleanup()
|
2021-08-17 08:26:20 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
buildTMPDestination (filename: string) {
|
2021-08-17 08:26:20 +02:00
|
|
|
return join(CONFIG.STORAGE.TMP_DIR, buildUUID() + extname(filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
VideoPathManager
|
|
|
|
}
|