2019-03-19 17:10:53 +01:00
|
|
|
import * as Bull from 'bull'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2019-05-16 16:55:34 +02:00
|
|
|
import { publishNewResolutionIfNeeded } from './video-transcoding'
|
2019-03-19 17:10:53 +01:00
|
|
|
import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
|
|
|
|
import { copy, stat } from 'fs-extra'
|
|
|
|
import { VideoFileModel } from '../../../models/video/video-file'
|
|
|
|
import { extname } from 'path'
|
2019-08-15 11:53:26 +02:00
|
|
|
import { MVideoFile, MVideoWithFile } from '@server/typings/models'
|
2019-11-15 15:06:03 +01:00
|
|
|
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
|
|
|
|
import { getVideoFilePath } from '@server/lib/video-paths'
|
2019-03-19 17:10:53 +01:00
|
|
|
|
|
|
|
export type VideoFileImportPayload = {
|
2020-01-31 16:56:52 +01:00
|
|
|
videoUUID: string
|
2019-03-19 17:10:53 +01:00
|
|
|
filePath: string
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
await updateVideoFile(video, payload.filePath)
|
|
|
|
|
2019-05-16 16:55:34 +02:00
|
|
|
await publishNewResolutionIfNeeded(video)
|
2019-03-19 17:10:53 +01:00
|
|
|
return video
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processVideoFileImport
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
async function updateVideoFile (video: MVideoWithFile, 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)
|
|
|
|
|
|
|
|
let updatedVideoFile = new VideoFileModel({
|
|
|
|
resolution: videoFileResolution,
|
|
|
|
extname: extname(inputFilePath),
|
|
|
|
size,
|
|
|
|
fps,
|
|
|
|
videoId: video.id
|
2019-08-15 11:53:26 +02:00
|
|
|
}) as MVideoFile
|
2019-03-19 17:10:53 +01:00
|
|
|
|
|
|
|
const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
|
|
|
|
|
|
|
|
if (currentVideoFile) {
|
|
|
|
// Remove old file and old torrent
|
|
|
|
await video.removeFile(currentVideoFile)
|
|
|
|
await video.removeTorrent(currentVideoFile)
|
|
|
|
// Remove the old video file from the array
|
|
|
|
video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
|
|
|
|
|
|
|
|
// Update the database
|
2019-08-15 11:53:26 +02:00
|
|
|
currentVideoFile.extname = updatedVideoFile.extname
|
|
|
|
currentVideoFile.size = updatedVideoFile.size
|
|
|
|
currentVideoFile.fps = updatedVideoFile.fps
|
2019-03-19 17:10:53 +01:00
|
|
|
|
|
|
|
updatedVideoFile = currentVideoFile
|
|
|
|
}
|
|
|
|
|
2019-11-15 15:06:03 +01:00
|
|
|
const outputPath = getVideoFilePath(video, updatedVideoFile)
|
2019-03-19 17:10:53 +01:00
|
|
|
await copy(inputFilePath, outputPath)
|
|
|
|
|
2019-11-15 15:06:03 +01:00
|
|
|
await createTorrentAndSetInfoHash(video, updatedVideoFile)
|
2019-03-19 17:10:53 +01:00
|
|
|
|
|
|
|
await updatedVideoFile.save()
|
|
|
|
|
|
|
|
video.VideoFiles.push(updatedVideoFile)
|
|
|
|
}
|