]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/move-to-object-storage.ts
Fix infohash with object storage
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / move-to-object-storage.ts
CommitLineData
41fb13c3 1import { Job } from 'bull'
0305db28
JB
2import { remove } from 'fs-extra'
3import { join } from 'path'
4import { logger } from '@server/helpers/logger'
1f6125be 5import { updateTorrentUrls } from '@server/helpers/webtorrent'
0305db28 6import { CONFIG } from '@server/initializers/config'
fb72d2e1 7import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
0305db28
JB
8import { storeHLSFile, storeWebTorrentFile } from '@server/lib/object-storage'
9import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
10import { moveToNextState } from '@server/lib/video-state'
11import { VideoModel } from '@server/models/video/video'
12import { VideoJobInfoModel } from '@server/models/video/video-job-info'
13import { MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoWithAllFiles } from '@server/types/models'
14import { MoveObjectStoragePayload, VideoStorage } from '../../../../shared'
15
41fb13c3 16export async function processMoveToObjectStorage (job: Job) {
0305db28
JB
17 const payload = job.data as MoveObjectStoragePayload
18 logger.info('Moving video %s in job %d.', payload.videoUUID, job.id)
19
20 const video = await VideoModel.loadWithFiles(payload.videoUUID)
21 // No video, maybe deleted?
22 if (!video) {
23 logger.info('Can\'t process job %d, video does not exist.', job.id)
24 return undefined
25 }
26
27 if (video.VideoFiles) {
28 await moveWebTorrentFiles(video)
29 }
30
31 if (video.VideoStreamingPlaylists) {
32 await moveHLSFiles(video)
33 }
34
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)
39 }
40
41 return payload.videoUUID
42}
43
44// ---------------------------------------------------------------------------
45
46async function moveWebTorrentFiles (video: MVideoWithAllFiles) {
47 for (const file of video.VideoFiles) {
48 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
49
50 const fileUrl = await storeWebTorrentFile(file.filename)
51
52 const oldPath = join(CONFIG.STORAGE.VIDEOS_DIR, file.filename)
53 await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath })
54 }
55}
56
57async function moveHLSFiles (video: MVideoWithAllFiles) {
58 for (const playlist of video.VideoStreamingPlaylists) {
59
60 for (const file of playlist.VideoFiles) {
61 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
62
63 // Resolution playlist
64 const playlistFilename = getHlsResolutionPlaylistFilename(file.filename)
65 await storeHLSFile(playlist, video, playlistFilename)
66
67 // Resolution fragmented file
68 const fileUrl = await storeHLSFile(playlist, video, file.filename)
69
70 const oldPath = join(getHLSDirectory(video), file.filename)
71
72 await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath })
73 }
74 }
75}
76
77async function doAfterLastJob (video: MVideoWithAllFiles, isNewVideo: boolean) {
78 for (const playlist of video.VideoStreamingPlaylists) {
79 if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
80
81 // Master playlist
82 playlist.playlistUrl = await storeHLSFile(playlist, video, playlist.playlistFilename)
83 // Sha256 segments file
84 playlist.segmentsSha256Url = await storeHLSFile(playlist, video, playlist.segmentsSha256Filename)
85
86 playlist.storage = VideoStorage.OBJECT_STORAGE
87
fb72d2e1
C
88 playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
89 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
90
0305db28
JB
91 await playlist.save()
92 }
93
94 // Remove empty hls video directory
95 if (video.VideoStreamingPlaylists) {
96 await remove(getHLSDirectory(video))
97 }
98
99 await moveToNextState(video, isNewVideo)
100}
101
102async function onFileMoved (options: {
103 videoOrPlaylist: MVideo | MStreamingPlaylistVideo
104 file: MVideoFile
105 fileUrl: string
106 oldPath: string
107}) {
108 const { videoOrPlaylist, file, fileUrl, oldPath } = options
109
110 file.fileUrl = fileUrl
111 file.storage = VideoStorage.OBJECT_STORAGE
112
1f6125be 113 await updateTorrentUrls(videoOrPlaylist, file)
0305db28
JB
114 await file.save()
115
116 logger.debug('Removing %s because it\'s now on object storage', oldPath)
117 await remove(oldPath)
118}