2021-08-27 08:32:44 -04:00
|
|
|
import { Job } from 'bull'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { remove } from 'fs-extra'
|
|
|
|
import { join } from 'path'
|
|
|
|
import { logger } from '@server/helpers/logger'
|
2021-12-08 05:07:19 -05:00
|
|
|
import { updateTorrentMetadata } from '@server/helpers/webtorrent'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2021-09-07 09:16:26 -04:00
|
|
|
import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { storeHLSFile, storeWebTorrentFile } from '@server/lib/object-storage'
|
|
|
|
import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
|
2021-12-23 05:09:31 -05:00
|
|
|
import { moveToFailedMoveToObjectStorageState, moveToNextState } from '@server/lib/video-state'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { VideoModel } from '@server/models/video/video'
|
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info'
|
|
|
|
import { MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoWithAllFiles } from '@server/types/models'
|
|
|
|
import { MoveObjectStoragePayload, VideoStorage } from '../../../../shared'
|
|
|
|
|
2021-08-27 08:32:44 -04:00
|
|
|
export async function processMoveToObjectStorage (job: Job) {
|
2021-08-17 02:26:20 -04:00
|
|
|
const payload = job.data as MoveObjectStoragePayload
|
|
|
|
logger.info('Moving video %s in job %d.', payload.videoUUID, job.id)
|
|
|
|
|
|
|
|
const video = await VideoModel.loadWithFiles(payload.videoUUID)
|
|
|
|
// No video, maybe deleted?
|
|
|
|
if (!video) {
|
|
|
|
logger.info('Can\'t process job %d, video does not exist.', job.id)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:09:31 -05:00
|
|
|
try {
|
|
|
|
if (video.VideoFiles) {
|
|
|
|
await moveWebTorrentFiles(video)
|
|
|
|
}
|
2021-08-17 02:26:20 -04:00
|
|
|
|
2021-12-23 05:09:31 -05:00
|
|
|
if (video.VideoStreamingPlaylists) {
|
|
|
|
await moveHLSFiles(video)
|
|
|
|
}
|
|
|
|
|
|
|
|
const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove')
|
|
|
|
if (pendingMove === 0) {
|
|
|
|
logger.info('Running cleanup after moving files to object storage (video %s in job %d)', video.uuid, job.id)
|
|
|
|
await doAfterLastJob(video, payload.isNewVideo)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot move video %s to object storage.', video.url, { err })
|
2021-08-17 02:26:20 -04:00
|
|
|
|
2021-12-23 05:09:31 -05:00
|
|
|
await moveToFailedMoveToObjectStorageState(video)
|
|
|
|
await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingMove')
|
2021-08-17 02:26:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return payload.videoUUID
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function moveWebTorrentFiles (video: MVideoWithAllFiles) {
|
|
|
|
for (const file of video.VideoFiles) {
|
|
|
|
if (file.storage !== VideoStorage.FILE_SYSTEM) continue
|
|
|
|
|
|
|
|
const fileUrl = await storeWebTorrentFile(file.filename)
|
|
|
|
|
|
|
|
const oldPath = join(CONFIG.STORAGE.VIDEOS_DIR, file.filename)
|
|
|
|
await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function moveHLSFiles (video: MVideoWithAllFiles) {
|
|
|
|
for (const playlist of video.VideoStreamingPlaylists) {
|
2021-11-18 08:35:08 -05:00
|
|
|
const playlistWithVideo = playlist.withVideo(video)
|
2021-08-17 02:26:20 -04:00
|
|
|
|
|
|
|
for (const file of playlist.VideoFiles) {
|
|
|
|
if (file.storage !== VideoStorage.FILE_SYSTEM) continue
|
|
|
|
|
|
|
|
// Resolution playlist
|
|
|
|
const playlistFilename = getHlsResolutionPlaylistFilename(file.filename)
|
2021-11-18 08:35:08 -05:00
|
|
|
await storeHLSFile(playlistWithVideo, playlistFilename)
|
2021-08-17 02:26:20 -04:00
|
|
|
|
|
|
|
// Resolution fragmented file
|
2021-11-18 08:35:08 -05:00
|
|
|
const fileUrl = await storeHLSFile(playlistWithVideo, file.filename)
|
2021-08-17 02:26:20 -04:00
|
|
|
|
|
|
|
const oldPath = join(getHLSDirectory(video), file.filename)
|
|
|
|
|
|
|
|
await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doAfterLastJob (video: MVideoWithAllFiles, isNewVideo: boolean) {
|
|
|
|
for (const playlist of video.VideoStreamingPlaylists) {
|
|
|
|
if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
|
|
|
|
|
2021-11-18 08:35:08 -05:00
|
|
|
const playlistWithVideo = playlist.withVideo(video)
|
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
// Master playlist
|
2021-11-18 08:35:08 -05:00
|
|
|
playlist.playlistUrl = await storeHLSFile(playlistWithVideo, playlist.playlistFilename)
|
2021-08-17 02:26:20 -04:00
|
|
|
// Sha256 segments file
|
2021-11-18 08:35:08 -05:00
|
|
|
playlist.segmentsSha256Url = await storeHLSFile(playlistWithVideo, playlist.segmentsSha256Filename)
|
2021-08-17 02:26:20 -04:00
|
|
|
|
|
|
|
playlist.storage = VideoStorage.OBJECT_STORAGE
|
|
|
|
|
2021-09-07 09:16:26 -04:00
|
|
|
playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
|
|
|
|
playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
|
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
await playlist.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove empty hls video directory
|
|
|
|
if (video.VideoStreamingPlaylists) {
|
|
|
|
await remove(getHLSDirectory(video))
|
|
|
|
}
|
|
|
|
|
|
|
|
await moveToNextState(video, isNewVideo)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onFileMoved (options: {
|
|
|
|
videoOrPlaylist: MVideo | MStreamingPlaylistVideo
|
|
|
|
file: MVideoFile
|
|
|
|
fileUrl: string
|
|
|
|
oldPath: string
|
|
|
|
}) {
|
|
|
|
const { videoOrPlaylist, file, fileUrl, oldPath } = options
|
|
|
|
|
|
|
|
file.fileUrl = fileUrl
|
|
|
|
file.storage = VideoStorage.OBJECT_STORAGE
|
|
|
|
|
2021-12-08 05:07:19 -05:00
|
|
|
await updateTorrentMetadata(videoOrPlaylist, file)
|
2021-08-17 02:26:20 -04:00
|
|
|
await file.save()
|
|
|
|
|
|
|
|
logger.debug('Removing %s because it\'s now on object storage', oldPath)
|
|
|
|
await remove(oldPath)
|
|
|
|
}
|