2021-08-27 14:32:44 +02:00
|
|
|
import { Job } from 'bull'
|
2019-03-19 17:10:53 +01:00
|
|
|
import { copy, stat } from 'fs-extra'
|
2021-11-02 19:11:20 +01:00
|
|
|
import { getLowercaseExtension } from '@shared/core-utils'
|
2019-11-15 15:06:03 +01:00
|
|
|
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
|
|
|
|
import { generateWebTorrentVideoFilename } from '@server/lib/paths'
|
|
|
|
import { addMoveToObjectStorageJob } from '@server/lib/video'
|
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager'
|
2021-02-17 09:36:09 +01:00
|
|
|
import { MVideoFullLight } from '@server/types/models'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { VideoFileImportPayload, VideoStorage } 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'
|
2019-03-19 17:10:53 +01:00
|
|
|
|
2021-08-27 14:32:44 +02:00
|
|
|
async function processVideoFileImport (job: Job) {
|
2019-03-19 17:10:53 +01:00
|
|
|
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)
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
if (CONFIG.OBJECT_STORAGE.ENABLED) {
|
|
|
|
await addMoveToObjectStorageJob(video)
|
|
|
|
} else {
|
|
|
|
await federateVideoIfNeeded(video, false)
|
2021-02-02 14:00:46 +01:00
|
|
|
}
|
2021-01-21 16:57:21 +01:00
|
|
|
|
2019-03-19 17:10:53 +01:00
|
|
|
return video
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processVideoFileImport
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-02-16 16:25:53 +01:00
|
|
|
async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
|
2021-08-06 13:35:25 +02:00
|
|
|
const { resolution } = await getVideoFileResolution(inputFilePath)
|
2019-03-19 17:10:53 +01:00
|
|
|
const { size } = await stat(inputFilePath)
|
|
|
|
const fps = await getVideoFileFPS(inputFilePath)
|
|
|
|
|
2021-06-08 09:33:03 +02:00
|
|
|
const fileExt = getLowercaseExtension(inputFilePath)
|
2019-03-19 17:10:53 +01:00
|
|
|
|
2021-08-06 13:35:25 +02:00
|
|
|
const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)
|
2019-03-19 17:10:53 +01:00
|
|
|
|
|
|
|
if (currentVideoFile) {
|
|
|
|
// Remove old file and old torrent
|
2021-11-17 16:04:53 +01:00
|
|
|
await video.removeWebTorrentFileAndTorrent(currentVideoFile)
|
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({
|
2021-08-06 13:35:25 +02:00
|
|
|
resolution,
|
2021-02-17 09:36:09 +01:00
|
|
|
extname: fileExt,
|
2021-08-06 13:35:25 +02:00
|
|
|
filename: generateWebTorrentVideoFilename(resolution, fileExt),
|
2021-08-17 08:26:20 +02:00
|
|
|
storage: VideoStorage.FILE_SYSTEM,
|
2021-02-17 09:36:09 +01:00
|
|
|
size,
|
|
|
|
fps,
|
|
|
|
videoId: video.id
|
|
|
|
})
|
2019-03-19 17:10:53 +01:00
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
|
2021-02-17 09:36:09 +01:00
|
|
|
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
|
|
|
}
|