1 import { Job } from 'bullmq'
2 import { remove } from 'fs-extra'
3 import { join } from 'path'
4 import { logger, loggerTagsFactory } from '@server/helpers/logger'
5 import { updateTorrentMetadata } from '@server/helpers/webtorrent'
6 import { CONFIG } from '@server/initializers/config'
7 import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
8 import { storeHLSFileFromFilename, storeWebTorrentFile } from '@server/lib/object-storage'
9 import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
10 import { moveToFailedMoveToObjectStorageState, moveToNextState } from '@server/lib/video-state'
11 import { VideoModel } from '@server/models/video/video'
12 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
13 import { MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoWithAllFiles } from '@server/types/models'
14 import { MoveObjectStoragePayload, VideoState, VideoStorage } from '@shared/models'
16 const lTagsBase = loggerTagsFactory('move-object-storage')
18 export async function processMoveToObjectStorage (job: Job) {
19 const payload = job.data as MoveObjectStoragePayload
20 logger.info('Moving video %s in job %s.', payload.videoUUID, job.id)
22 const video = await VideoModel.loadWithFiles(payload.videoUUID)
23 // No video, maybe deleted?
25 logger.info('Can\'t process job %d, video does not exist.', job.id, lTagsBase(payload.videoUUID))
29 const lTags = lTagsBase(video.uuid, video.url)
32 if (video.VideoFiles) {
33 logger.debug('Moving %d webtorrent files for video %s.', video.VideoFiles.length, video.uuid, lTags)
35 await moveWebTorrentFiles(video)
38 if (video.VideoStreamingPlaylists) {
39 logger.debug('Moving HLS playlist of %s.', video.uuid)
41 await moveHLSFiles(video)
44 const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove')
45 if (pendingMove === 0) {
46 logger.info('Running cleanup after moving files to object storage (video %s in job %s)', video.uuid, job.id, lTags)
48 await doAfterLastJob({ video, previousVideoState: payload.previousVideoState, isNewVideo: payload.isNewVideo })
51 await onMoveToObjectStorageFailure(job, err)
54 return payload.videoUUID
57 export async function onMoveToObjectStorageFailure (job: Job, err: any) {
58 const payload = job.data as MoveObjectStoragePayload
60 const video = await VideoModel.loadWithFiles(payload.videoUUID)
63 logger.error('Cannot move video %s to object storage.', video.url, { err, ...lTagsBase(video.uuid, video.url) })
65 await moveToFailedMoveToObjectStorageState(video)
66 await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingMove')
69 // ---------------------------------------------------------------------------
71 async function moveWebTorrentFiles (video: MVideoWithAllFiles) {
72 for (const file of video.VideoFiles) {
73 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
75 const fileUrl = await storeWebTorrentFile(file.filename)
77 const oldPath = join(CONFIG.STORAGE.VIDEOS_DIR, file.filename)
78 await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath })
82 async function moveHLSFiles (video: MVideoWithAllFiles) {
83 for (const playlist of video.VideoStreamingPlaylists) {
84 const playlistWithVideo = playlist.withVideo(video)
86 for (const file of playlist.VideoFiles) {
87 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
89 // Resolution playlist
90 const playlistFilename = getHlsResolutionPlaylistFilename(file.filename)
91 await storeHLSFileFromFilename(playlistWithVideo, playlistFilename)
93 // Resolution fragmented file
94 const fileUrl = await storeHLSFileFromFilename(playlistWithVideo, file.filename)
96 const oldPath = join(getHLSDirectory(video), file.filename)
98 await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath })
103 async function doAfterLastJob (options: {
104 video: MVideoWithAllFiles
105 previousVideoState: VideoState
108 const { video, previousVideoState, isNewVideo } = options
110 for (const playlist of video.VideoStreamingPlaylists) {
111 if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
113 const playlistWithVideo = playlist.withVideo(video)
116 playlist.playlistUrl = await storeHLSFileFromFilename(playlistWithVideo, playlist.playlistFilename)
117 // Sha256 segments file
118 playlist.segmentsSha256Url = await storeHLSFileFromFilename(playlistWithVideo, playlist.segmentsSha256Filename)
120 playlist.storage = VideoStorage.OBJECT_STORAGE
122 playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
123 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
125 await playlist.save()
128 // Remove empty hls video directory
129 if (video.VideoStreamingPlaylists) {
130 await remove(getHLSDirectory(video))
133 await moveToNextState({ video, previousVideoState, isNewVideo })
136 async function onFileMoved (options: {
137 videoOrPlaylist: MVideo | MStreamingPlaylistVideo
142 const { videoOrPlaylist, file, fileUrl, oldPath } = options
144 file.fileUrl = fileUrl
145 file.storage = VideoStorage.OBJECT_STORAGE
147 await updateTorrentMetadata(videoOrPlaylist, file)
150 logger.debug('Removing %s because it\'s now on object storage', oldPath)
151 await remove(oldPath)