]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/manage-video-torrent.ts
Use worker thread to send HTTP requests
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / manage-video-torrent.ts
CommitLineData
5a921e7b 1import { Job } from 'bullmq'
f012319a
C
2import { createTorrentAndSetInfoHash, updateTorrentMetadata } from '@server/helpers/webtorrent'
3import { VideoModel } from '@server/models/video/video'
4import { VideoFileModel } from '@server/models/video/video-file'
5import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
6import { ManageVideoTorrentPayload } from '@shared/models'
7import { logger } from '../../../helpers/logger'
8
9async function processManageVideoTorrent (job: Job) {
10 const payload = job.data as ManageVideoTorrentPayload
bd911b54 11 logger.info('Processing torrent in job %s.', job.id)
f012319a
C
12
13 if (payload.action === 'create') return doCreateAction(payload)
14 if (payload.action === 'update-metadata') return doUpdateMetadataAction(payload)
15}
16
17// ---------------------------------------------------------------------------
18
19export {
20 processManageVideoTorrent
21}
22
23// ---------------------------------------------------------------------------
24
25async function doCreateAction (payload: ManageVideoTorrentPayload & { action: 'create' }) {
26 const [ video, file ] = await Promise.all([
27 loadVideoOrLog(payload.videoId),
28 loadFileOrLog(payload.videoFileId)
29 ])
30
52fe4b67
C
31 if (!video || !file) return
32
f012319a
C
33 await createTorrentAndSetInfoHash(video, file)
34
35 // Refresh videoFile because the createTorrentAndSetInfoHash could be long
36 const refreshedFile = await VideoFileModel.loadWithVideo(file.id)
37 // File does not exist anymore, remove the generated torrent
38 if (!refreshedFile) return file.removeTorrent()
39
40 refreshedFile.infoHash = file.infoHash
41 refreshedFile.torrentFilename = file.torrentFilename
42
43 return refreshedFile.save()
44}
45
46async function doUpdateMetadataAction (payload: ManageVideoTorrentPayload & { action: 'update-metadata' }) {
47 const [ video, streamingPlaylist, file ] = await Promise.all([
48 loadVideoOrLog(payload.videoId),
49 loadStreamingPlaylistOrLog(payload.streamingPlaylistId),
50 loadFileOrLog(payload.videoFileId)
51 ])
52
52fe4b67
C
53 if ((!video && !streamingPlaylist) || !file) return
54
f012319a
C
55 await updateTorrentMetadata(video || streamingPlaylist, file)
56
57 await file.save()
58}
59
60async function loadVideoOrLog (videoId: number) {
61 if (!videoId) return undefined
62
63 const video = await VideoModel.load(videoId)
64 if (!video) {
65 logger.debug('Do not process torrent for video %d: does not exist anymore.', videoId)
66 }
67
68 return video
69}
70
71async function loadStreamingPlaylistOrLog (streamingPlaylistId: number) {
72 if (!streamingPlaylistId) return undefined
73
74 const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId)
75 if (!streamingPlaylist) {
76 logger.debug('Do not process torrent for streaming playlist %d: does not exist anymore.', streamingPlaylistId)
77 }
78
79 return streamingPlaylist
80}
81
82async function loadFileOrLog (videoFileId: number) {
83 if (!videoFileId) return undefined
84
85 const file = await VideoFileModel.loadWithVideo(videoFileId)
86
87 if (!file) {
88 logger.debug('Do not process torrent for file %d: does not exist anymore.', videoFileId)
89 }
90
91 return file
92}