PeerTube/server/lib/job-queue/handlers/video-file-import.ts

89 lines
3.0 KiB
TypeScript
Raw Normal View History

2019-03-19 17:10:53 +01:00
import * as Bull from 'bull'
import { copy, stat } from 'fs-extra'
import { extname } from 'path'
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
2021-05-11 11:15:29 +02:00
import { UserModel } from '@server/models/user/user'
2021-02-17 09:36:09 +01:00
import { MVideoFullLight } from '@server/types/models'
2020-04-23 09:32:53 +02:00
import { VideoFileImportPayload } from '@shared/models'
2020-11-20 17:16:55 +01:00
import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
import { logger } from '../../../helpers/logger'
import { VideoModel } from '../../../models/video/video'
import { VideoFileModel } from '../../../models/video/video-file'
2021-01-21 15:58:17 +01:00
import { onNewWebTorrentFileResolution } from './video-transcoding'
2019-03-19 17:10:53 +01:00
async function processVideoFileImport (job: Bull.Job) {
const payload = job.data as VideoFileImportPayload
logger.info('Processing video file import in job %d.', job.id)
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
// No video, maybe deleted?
if (!video) {
logger.info('Do not process job %d, video does not exist.', job.id)
return undefined
}
2021-02-02 14:00:46 +01:00
const data = await getVideoFileResolution(payload.filePath)
2019-03-19 17:10:53 +01:00
await updateVideoFile(video, payload.filePath)
const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
2021-02-02 14:00:46 +01:00
const newResolutionPayload = {
type: 'new-resolution-to-webtorrent' as 'new-resolution-to-webtorrent',
videoUUID: video.uuid,
resolution: data.videoFileResolution,
isPortraitMode: data.isPortraitMode,
copyCodecs: false,
isNewVideo: false
}
await onNewWebTorrentFileResolution(video, user, newResolutionPayload)
2019-03-19 17:10:53 +01:00
return video
}
// ---------------------------------------------------------------------------
export {
processVideoFileImport
}
// ---------------------------------------------------------------------------
async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
2019-03-19 17:10:53 +01:00
const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
const { size } = await stat(inputFilePath)
const fps = await getVideoFileFPS(inputFilePath)
const fileExt = extname(inputFilePath)
2019-03-19 17:10:53 +01:00
2021-02-17 09:36:09 +01:00
const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution)
2019-03-19 17:10:53 +01:00
if (currentVideoFile) {
// Remove old file and old torrent
await video.removeFile(currentVideoFile)
await currentVideoFile.removeTorrent()
2019-03-19 17:10:53 +01:00
// Remove the old video file from the array
video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
2021-02-17 09:36:09 +01:00
await currentVideoFile.destroy()
2019-03-19 17:10:53 +01:00
}
2021-02-17 09:36:09 +01:00
const newVideoFile = new VideoFileModel({
resolution: videoFileResolution,
extname: fileExt,
filename: generateVideoFilename(video, false, videoFileResolution, fileExt),
size,
fps,
videoId: video.id
})
2019-03-19 17:10:53 +01:00
2021-02-17 09:36:09 +01:00
const outputPath = getVideoFilePath(video, newVideoFile)
await copy(inputFilePath, outputPath)
2019-03-19 17:10:53 +01:00
2021-02-17 09:36:09 +01:00
video.VideoFiles.push(newVideoFile)
2021-02-18 11:28:00 +01:00
await createTorrentAndSetInfoHash(video, newVideoFile)
2019-03-19 17:10:53 +01:00
2021-02-17 09:36:09 +01:00
await newVideoFile.save()
2019-03-19 17:10:53 +01:00
}