2022-08-08 04:42:08 -04:00
|
|
|
import { Job } from 'bullmq'
|
2019-03-19 12:10:53 -04:00
|
|
|
import { copy, stat } from 'fs-extra'
|
2019-11-15 09:06:03 -05:00
|
|
|
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
|
|
|
|
import { generateWebTorrentVideoFilename } from '@server/lib/paths'
|
2022-08-08 09:48:17 -04:00
|
|
|
import { buildMoveToObjectStorageJob } from '@server/lib/video'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager'
|
2022-02-11 04:51:33 -05:00
|
|
|
import { VideoModel } from '@server/models/video/video'
|
|
|
|
import { VideoFileModel } from '@server/models/video/video-file'
|
2021-02-17 03:36:09 -05:00
|
|
|
import { MVideoFullLight } from '@server/types/models'
|
2022-02-11 04:51:33 -05:00
|
|
|
import { getLowercaseExtension } from '@shared/core-utils'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { VideoFileImportPayload, VideoStorage } from '@shared/models'
|
2022-02-11 04:51:33 -05:00
|
|
|
import { getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg'
|
2020-11-20 11:16:55 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2022-08-08 09:48:17 -04:00
|
|
|
import { JobQueue } from '../job-queue'
|
2019-03-19 12:10:53 -04:00
|
|
|
|
2021-08-27 08:32:44 -04:00
|
|
|
async function processVideoFileImport (job: Job) {
|
2019-03-19 12:10:53 -04:00
|
|
|
const payload = job.data as VideoFileImportPayload
|
2022-08-08 09:48:17 -04:00
|
|
|
logger.info('Processing video file import in job %s.', job.id)
|
2019-03-19 12:10:53 -04:00
|
|
|
|
2022-06-28 08:57:51 -04:00
|
|
|
const video = await VideoModel.loadFull(payload.videoUUID)
|
2019-03-19 12:10:53 -04:00
|
|
|
// 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 02:26:20 -04:00
|
|
|
if (CONFIG.OBJECT_STORAGE.ENABLED) {
|
2022-08-08 09:48:17 -04:00
|
|
|
await JobQueue.Instance.createJob(await buildMoveToObjectStorageJob({ video, previousVideoState: video.state }))
|
2021-08-17 02:26:20 -04:00
|
|
|
} else {
|
|
|
|
await federateVideoIfNeeded(video, false)
|
2021-02-02 08:00:46 -05:00
|
|
|
}
|
2021-01-21 10:57:21 -05:00
|
|
|
|
2019-03-19 12:10:53 -04:00
|
|
|
return video
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processVideoFileImport
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-02-16 10:25:53 -05:00
|
|
|
async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
|
2022-02-11 04:51:33 -05:00
|
|
|
const { resolution } = await getVideoStreamDimensionsInfo(inputFilePath)
|
2019-03-19 12:10:53 -04:00
|
|
|
const { size } = await stat(inputFilePath)
|
2022-02-11 04:51:33 -05:00
|
|
|
const fps = await getVideoStreamFPS(inputFilePath)
|
2019-03-19 12:10:53 -04:00
|
|
|
|
2021-06-08 03:33:03 -04:00
|
|
|
const fileExt = getLowercaseExtension(inputFilePath)
|
2019-03-19 12:10:53 -04:00
|
|
|
|
2021-08-06 07:35:25 -04:00
|
|
|
const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)
|
2019-03-19 12:10:53 -04:00
|
|
|
|
|
|
|
if (currentVideoFile) {
|
|
|
|
// Remove old file and old torrent
|
2022-07-29 08:50:41 -04:00
|
|
|
await video.removeWebTorrentFile(currentVideoFile)
|
2019-03-19 12:10:53 -04:00
|
|
|
// Remove the old video file from the array
|
|
|
|
video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
|
|
|
|
|
2021-02-17 03:36:09 -05:00
|
|
|
await currentVideoFile.destroy()
|
2019-03-19 12:10:53 -04:00
|
|
|
}
|
|
|
|
|
2021-02-17 03:36:09 -05:00
|
|
|
const newVideoFile = new VideoFileModel({
|
2021-08-06 07:35:25 -04:00
|
|
|
resolution,
|
2021-02-17 03:36:09 -05:00
|
|
|
extname: fileExt,
|
2021-08-06 07:35:25 -04:00
|
|
|
filename: generateWebTorrentVideoFilename(resolution, fileExt),
|
2021-08-17 02:26:20 -04:00
|
|
|
storage: VideoStorage.FILE_SYSTEM,
|
2021-02-17 03:36:09 -05:00
|
|
|
size,
|
|
|
|
fps,
|
|
|
|
videoId: video.id
|
|
|
|
})
|
2019-03-19 12:10:53 -04:00
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
|
2021-02-17 03:36:09 -05:00
|
|
|
await copy(inputFilePath, outputPath)
|
2019-03-19 12:10:53 -04:00
|
|
|
|
2021-02-17 03:36:09 -05:00
|
|
|
video.VideoFiles.push(newVideoFile)
|
2021-02-18 05:28:00 -05:00
|
|
|
await createTorrentAndSetInfoHash(video, newVideoFile)
|
2019-03-19 12:10:53 -04:00
|
|
|
|
2021-02-17 03:36:09 -05:00
|
|
|
await newVideoFile.save()
|
2019-03-19 12:10:53 -04:00
|
|
|
}
|