2022-08-08 10:42:08 +02:00
|
|
|
import { Job } from 'bullmq'
|
2022-03-16 15:34:21 +01:00
|
|
|
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 15:48:17 +02:00
|
|
|
logger.info('Processing torrent in job %s.', job.id)
|
2022-03-16 15:34:21 +01:00
|
|
|
|
|
|
|
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 18:21:36 +01:00
|
|
|
if (!video || !file) return
|
|
|
|
|
2022-03-16 15:34:21 +01:00
|
|
|
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 18:21:36 +01:00
|
|
|
if ((!video && !streamingPlaylist) || !file) return
|
|
|
|
|
2022-03-16 15:34:21 +01:00
|
|
|
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
|
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
const file = await VideoFileModel.load(videoFileId)
|
2022-03-16 15:34:21 +01:00
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
logger.debug('Do not process torrent for file %d: does not exist anymore.', videoFileId)
|
|
|
|
}
|
|
|
|
|
|
|
|
return file
|
|
|
|
}
|