2024-06-04 09:08:24 +02:00
|
|
|
import { FileStorage, MoveStoragePayload, VideoStateType } from '@peertube/peertube-models'
|
2023-10-31 12:15:40 +01:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
|
|
|
import { updateTorrentMetadata } from '@server/helpers/webtorrent.js'
|
|
|
|
import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants.js'
|
|
|
|
import {
|
|
|
|
makeHLSFileAvailable,
|
2024-06-04 09:08:24 +02:00
|
|
|
makeOriginalFileAvailable,
|
2023-10-31 12:15:40 +01:00
|
|
|
makeWebVideoFileAvailable,
|
|
|
|
removeHLSFileObjectStorageByFilename,
|
|
|
|
removeHLSObjectStorage,
|
2024-06-04 09:08:24 +02:00
|
|
|
removeOriginalFileObjectStorage,
|
2023-10-31 12:15:40 +01:00
|
|
|
removeWebVideoObjectStorage
|
|
|
|
} from '@server/lib/object-storage/index.js'
|
|
|
|
import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths.js'
|
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager.js'
|
|
|
|
import { moveToFailedMoveToFileSystemState, moveToNextState } from '@server/lib/video-state.js'
|
|
|
|
import { MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoWithAllFiles } from '@server/types/models/index.js'
|
2024-06-04 09:08:24 +02:00
|
|
|
import { MVideoSource } from '@server/types/models/video/video-source.js'
|
|
|
|
import { Job } from 'bullmq'
|
|
|
|
import { join } from 'path'
|
2023-10-31 12:15:40 +01:00
|
|
|
import { moveToJob, onMoveToStorageFailure } from './shared/move-video.js'
|
|
|
|
|
|
|
|
const lTagsBase = loggerTagsFactory('move-file-system')
|
|
|
|
|
|
|
|
export async function processMoveToFileSystem (job: Job) {
|
|
|
|
const payload = job.data as MoveStoragePayload
|
|
|
|
logger.info('Moving video %s to file system in job %s.', payload.videoUUID, job.id)
|
|
|
|
|
|
|
|
await moveToJob({
|
|
|
|
jobId: job.id,
|
|
|
|
videoUUID: payload.videoUUID,
|
|
|
|
loggerTags: lTagsBase().tags,
|
|
|
|
|
|
|
|
moveWebVideoFiles,
|
|
|
|
moveHLSFiles,
|
2024-06-04 09:08:24 +02:00
|
|
|
moveVideoSourceFile,
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
doAfterLastMove: video => doAfterLastMove({ video, previousVideoState: payload.previousVideoState, isNewVideo: payload.isNewVideo }),
|
|
|
|
moveToFailedState: moveToFailedMoveToFileSystemState
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function onMoveToFileSystemFailure (job: Job, err: any) {
|
|
|
|
const payload = job.data as MoveStoragePayload
|
|
|
|
|
|
|
|
await onMoveToStorageFailure({
|
|
|
|
videoUUID: payload.videoUUID,
|
|
|
|
err,
|
|
|
|
lTags: lTagsBase(),
|
|
|
|
moveToFailedState: moveToFailedMoveToFileSystemState
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Private
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2024-06-04 09:08:24 +02:00
|
|
|
async function moveVideoSourceFile (source: MVideoSource) {
|
|
|
|
if (source.storage === FileStorage.FILE_SYSTEM) return
|
|
|
|
|
|
|
|
await makeOriginalFileAvailable(
|
|
|
|
source.keptOriginalFilename,
|
|
|
|
VideoPathManager.Instance.getFSOriginalVideoFilePath(source.keptOriginalFilename)
|
|
|
|
)
|
|
|
|
|
|
|
|
const oldFileUrl = source.fileUrl
|
|
|
|
|
|
|
|
source.fileUrl = null
|
|
|
|
source.storage = FileStorage.FILE_SYSTEM
|
|
|
|
await source.save()
|
|
|
|
|
|
|
|
logger.debug('Removing original video file %s because it\'s now on file system', oldFileUrl, lTagsBase())
|
|
|
|
|
|
|
|
await removeOriginalFileObjectStorage(source)
|
|
|
|
}
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
async function moveWebVideoFiles (video: MVideoWithAllFiles) {
|
|
|
|
for (const file of video.VideoFiles) {
|
2024-02-12 10:47:52 +01:00
|
|
|
if (file.storage === FileStorage.FILE_SYSTEM) continue
|
2023-10-31 12:15:40 +01:00
|
|
|
|
|
|
|
await makeWebVideoFileAvailable(file.filename, VideoPathManager.Instance.getFSVideoFileOutputPath(video, file))
|
2024-06-04 09:08:24 +02:00
|
|
|
await onVideoFileMoved({
|
2023-10-31 12:15:40 +01:00
|
|
|
videoOrPlaylist: video,
|
|
|
|
file,
|
|
|
|
objetStorageRemover: () => removeWebVideoObjectStorage(file)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function moveHLSFiles (video: MVideoWithAllFiles) {
|
|
|
|
for (const playlist of video.VideoStreamingPlaylists) {
|
|
|
|
const playlistWithVideo = playlist.withVideo(video)
|
|
|
|
|
|
|
|
for (const file of playlist.VideoFiles) {
|
2024-02-12 10:47:52 +01:00
|
|
|
if (file.storage === FileStorage.FILE_SYSTEM) continue
|
2023-10-31 12:15:40 +01:00
|
|
|
|
|
|
|
// Resolution playlist
|
|
|
|
const playlistFilename = getHlsResolutionPlaylistFilename(file.filename)
|
|
|
|
await makeHLSFileAvailable(playlistWithVideo, playlistFilename, join(getHLSDirectory(video), playlistFilename))
|
|
|
|
await makeHLSFileAvailable(playlistWithVideo, file.filename, join(getHLSDirectory(video), file.filename))
|
|
|
|
|
2024-06-04 09:08:24 +02:00
|
|
|
await onVideoFileMoved({
|
2023-10-31 12:15:40 +01:00
|
|
|
videoOrPlaylist: playlistWithVideo,
|
|
|
|
file,
|
|
|
|
objetStorageRemover: async () => {
|
|
|
|
await removeHLSFileObjectStorageByFilename(playlistWithVideo, playlistFilename)
|
|
|
|
await removeHLSFileObjectStorageByFilename(playlistWithVideo, file.filename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 09:08:24 +02:00
|
|
|
async function onVideoFileMoved (options: {
|
2023-10-31 12:15:40 +01:00
|
|
|
videoOrPlaylist: MVideo | MStreamingPlaylistVideo
|
|
|
|
file: MVideoFile
|
|
|
|
objetStorageRemover: () => Promise<any>
|
|
|
|
}) {
|
|
|
|
const { videoOrPlaylist, file, objetStorageRemover } = options
|
|
|
|
|
|
|
|
const oldFileUrl = file.fileUrl
|
|
|
|
|
|
|
|
file.fileUrl = null
|
2024-02-12 10:47:52 +01:00
|
|
|
file.storage = FileStorage.FILE_SYSTEM
|
2023-10-31 12:15:40 +01:00
|
|
|
|
|
|
|
await updateTorrentMetadata(videoOrPlaylist, file)
|
|
|
|
await file.save()
|
|
|
|
|
|
|
|
logger.debug('Removing web video file %s because it\'s now on file system', oldFileUrl, lTagsBase())
|
|
|
|
await objetStorageRemover()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doAfterLastMove (options: {
|
|
|
|
video: MVideoWithAllFiles
|
|
|
|
previousVideoState: VideoStateType
|
|
|
|
isNewVideo: boolean
|
|
|
|
}) {
|
|
|
|
const { video, previousVideoState, isNewVideo } = options
|
|
|
|
|
|
|
|
for (const playlist of video.VideoStreamingPlaylists) {
|
2024-02-12 10:47:52 +01:00
|
|
|
if (playlist.storage === FileStorage.FILE_SYSTEM) continue
|
2023-10-31 12:15:40 +01:00
|
|
|
|
|
|
|
const playlistWithVideo = playlist.withVideo(video)
|
|
|
|
|
|
|
|
for (const filename of [ playlist.playlistFilename, playlist.segmentsSha256Filename ]) {
|
|
|
|
await makeHLSFileAvailable(playlistWithVideo, filename, join(getHLSDirectory(video), filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
playlist.playlistUrl = null
|
|
|
|
playlist.segmentsSha256Url = null
|
2024-02-12 10:47:52 +01:00
|
|
|
playlist.storage = FileStorage.FILE_SYSTEM
|
2023-10-31 12:15:40 +01:00
|
|
|
|
|
|
|
playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
|
|
|
|
playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
|
|
|
|
|
|
|
|
await playlist.save()
|
|
|
|
|
|
|
|
await removeHLSObjectStorage(playlistWithVideo)
|
|
|
|
}
|
|
|
|
|
|
|
|
await moveToNextState({ video, previousVideoState, isNewVideo })
|
|
|
|
}
|