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