1 import { Job } from 'bull'
2 import { remove } from 'fs-extra'
3 import { join } from 'path'
4 import { logger } from '@server/helpers/logger'
5 import { updateTorrentUrls } from '@server/helpers/webtorrent'
6 import { CONFIG } from '@server/initializers/config'
7 import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
8 import { storeHLSFile, storeWebTorrentFile } from '@server/lib/object-storage'
9 import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
10 import { 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, VideoStorage } from '../../../../shared'
16 export async function processMoveToObjectStorage (job: Job) {
17 const payload = job.data as MoveObjectStoragePayload
18 logger.info('Moving video %s in job %d.', payload.videoUUID, job.id)
20 const video = await VideoModel.loadWithFiles(payload.videoUUID)
21 // No video, maybe deleted?
23 logger.info('Can\'t process job %d, video does not exist.', job.id)
27 if (video.VideoFiles) {
28 await moveWebTorrentFiles(video)
31 if (video.VideoStreamingPlaylists) {
32 await moveHLSFiles(video)
35 const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove')
36 if (pendingMove === 0) {
37 logger.info('Running cleanup after moving files to object storage (video %s in job %d)', video.uuid, job.id)
38 await doAfterLastJob(video, payload.isNewVideo)
41 return payload.videoUUID
44 // ---------------------------------------------------------------------------
46 async function moveWebTorrentFiles (video: MVideoWithAllFiles) {
47 for (const file of video.VideoFiles) {
48 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
50 const fileUrl = await storeWebTorrentFile(file.filename)
52 const oldPath = join(CONFIG.STORAGE.VIDEOS_DIR, file.filename)
53 await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath })
57 async function moveHLSFiles (video: MVideoWithAllFiles) {
58 for (const playlist of video.VideoStreamingPlaylists) {
60 for (const file of playlist.VideoFiles) {
61 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
63 // Resolution playlist
64 const playlistFilename = getHlsResolutionPlaylistFilename(file.filename)
65 await storeHLSFile(playlist, video, playlistFilename)
67 // Resolution fragmented file
68 const fileUrl = await storeHLSFile(playlist, video, file.filename)
70 const oldPath = join(getHLSDirectory(video), file.filename)
72 await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath })
77 async function doAfterLastJob (video: MVideoWithAllFiles, isNewVideo: boolean) {
78 for (const playlist of video.VideoStreamingPlaylists) {
79 if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
82 playlist.playlistUrl = await storeHLSFile(playlist, video, playlist.playlistFilename)
83 // Sha256 segments file
84 playlist.segmentsSha256Url = await storeHLSFile(playlist, video, playlist.segmentsSha256Filename)
86 playlist.storage = VideoStorage.OBJECT_STORAGE
88 playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
89 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
94 // Remove empty hls video directory
95 if (video.VideoStreamingPlaylists) {
96 await remove(getHLSDirectory(video))
99 await moveToNextState(video, isNewVideo)
102 async function onFileMoved (options: {
103 videoOrPlaylist: MVideo | MStreamingPlaylistVideo
108 const { videoOrPlaylist, file, fileUrl, oldPath } = options
110 file.fileUrl = fileUrl
111 file.storage = VideoStorage.OBJECT_STORAGE
113 await updateTorrentUrls(videoOrPlaylist, file)
116 logger.debug('Removing %s because it\'s now on object storage', oldPath)
117 await remove(oldPath)