1
0
Fork 0
peertube/server/lib/job-queue/handlers/video-file-import.ts

88 lines
3.0 KiB
TypeScript
Raw Normal View History

2019-03-19 16:10:53 +00:00
import * as Bull from 'bull'
import { copy, stat } from 'fs-extra'
import { getLowercaseExtension } from '@server/helpers/core-utils'
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
2021-07-22 12:28:03 +00:00
import { generateWebTorrentVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
2021-05-11 09:15:29 +00:00
import { UserModel } from '@server/models/user/user'
2021-02-17 08:36:09 +00:00
import { MVideoFullLight } from '@server/types/models'
2020-04-23 07:32:53 +00:00
import { VideoFileImportPayload } from '@shared/models'
2020-11-20 16:16:55 +00: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 14:58:17 +00:00
import { onNewWebTorrentFileResolution } from './video-transcoding'
2019-03-19 16:10:53 +00: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 13:00:46 +00:00
const data = await getVideoFileResolution(payload.filePath)
2019-03-19 16:10:53 +00:00
await updateVideoFile(video, payload.filePath)
const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
2021-02-02 13:00:46 +00: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 16:10:53 +00:00
return video
}
// ---------------------------------------------------------------------------
export {
processVideoFileImport
}
// ---------------------------------------------------------------------------
async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
2019-03-19 16:10:53 +00:00
const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
const { size } = await stat(inputFilePath)
const fps = await getVideoFileFPS(inputFilePath)
const fileExt = getLowercaseExtension(inputFilePath)
2019-03-19 16:10:53 +00:00
2021-02-17 08:36:09 +00:00
const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution)
2019-03-19 16:10:53 +00:00
if (currentVideoFile) {
// Remove old file and old torrent
2021-07-23 09:20:00 +00:00
await video.removeFileAndTorrent(currentVideoFile)
2019-03-19 16:10:53 +00:00
// Remove the old video file from the array
video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
2021-02-17 08:36:09 +00:00
await currentVideoFile.destroy()
2019-03-19 16:10:53 +00:00
}
2021-02-17 08:36:09 +00:00
const newVideoFile = new VideoFileModel({
resolution: videoFileResolution,
extname: fileExt,
2021-07-22 12:28:03 +00:00
filename: generateWebTorrentVideoFilename(videoFileResolution, fileExt),
2021-02-17 08:36:09 +00:00
size,
fps,
videoId: video.id
})
2019-03-19 16:10:53 +00:00
2021-02-17 08:36:09 +00:00
const outputPath = getVideoFilePath(video, newVideoFile)
await copy(inputFilePath, outputPath)
2019-03-19 16:10:53 +00:00
2021-02-17 08:36:09 +00:00
video.VideoFiles.push(newVideoFile)
2021-02-18 10:28:00 +00:00
await createTorrentAndSetInfoHash(video, newVideoFile)
2019-03-19 16:10:53 +00:00
2021-02-17 08:36:09 +00:00
await newVideoFile.save()
2019-03-19 16:10:53 +00:00
}