PeerTube/server/lib/job-queue/handlers/manage-video-torrent.ts

93 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-08-08 03:42:08 -05:00
import { Job } from 'bullmq'
import { createTorrentAndSetInfoHash, updateTorrentMetadata } from '@server/helpers/webtorrent'
import { VideoModel } from '@server/models/video/video'
import { VideoFileModel } from '@server/models/video/video-file'
import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
import { ManageVideoTorrentPayload } from '@shared/models'
import { logger } from '../../../helpers/logger'
async function processManageVideoTorrent (job: Job) {
const payload = job.data as ManageVideoTorrentPayload
2022-08-08 08:48:17 -05:00
logger.info('Processing torrent in job %s.', job.id)
if (payload.action === 'create') return doCreateAction(payload)
if (payload.action === 'update-metadata') return doUpdateMetadataAction(payload)
}
// ---------------------------------------------------------------------------
export {
processManageVideoTorrent
}
// ---------------------------------------------------------------------------
async function doCreateAction (payload: ManageVideoTorrentPayload & { action: 'create' }) {
const [ video, file ] = await Promise.all([
loadVideoOrLog(payload.videoId),
loadFileOrLog(payload.videoFileId)
])
2022-03-16 12:21:36 -05:00
if (!video || !file) return
await createTorrentAndSetInfoHash(video, file)
// Refresh videoFile because the createTorrentAndSetInfoHash could be long
const refreshedFile = await VideoFileModel.loadWithVideo(file.id)
// File does not exist anymore, remove the generated torrent
if (!refreshedFile) return file.removeTorrent()
refreshedFile.infoHash = file.infoHash
refreshedFile.torrentFilename = file.torrentFilename
return refreshedFile.save()
}
async function doUpdateMetadataAction (payload: ManageVideoTorrentPayload & { action: 'update-metadata' }) {
const [ video, streamingPlaylist, file ] = await Promise.all([
loadVideoOrLog(payload.videoId),
loadStreamingPlaylistOrLog(payload.streamingPlaylistId),
loadFileOrLog(payload.videoFileId)
])
2022-03-16 12:21:36 -05:00
if ((!video && !streamingPlaylist) || !file) return
await updateTorrentMetadata(video || streamingPlaylist, file)
await file.save()
}
async function loadVideoOrLog (videoId: number) {
if (!videoId) return undefined
const video = await VideoModel.load(videoId)
if (!video) {
logger.debug('Do not process torrent for video %d: does not exist anymore.', videoId)
}
return video
}
async function loadStreamingPlaylistOrLog (streamingPlaylistId: number) {
if (!streamingPlaylistId) return undefined
const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId)
if (!streamingPlaylist) {
logger.debug('Do not process torrent for streaming playlist %d: does not exist anymore.', streamingPlaylistId)
}
return streamingPlaylist
}
async function loadFileOrLog (videoFileId: number) {
if (!videoFileId) return undefined
const file = await VideoFileModel.load(videoFileId)
if (!file) {
logger.debug('Do not process torrent for file %d: does not exist anymore.', videoFileId)
}
return file
}