2022-08-08 03:42:08 -05:00
|
|
|
import { Job } from 'bullmq'
|
2019-03-19 11:10:53 -05:00
|
|
|
import { copy, stat } from 'fs-extra'
|
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'
|
2022-08-08 08:48:17 -05:00
|
|
|
import { buildMoveToObjectStorageJob } from '@server/lib/video'
|
2021-08-17 01:26:20 -05:00
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager'
|
2022-02-11 03:51:33 -06:00
|
|
|
import { VideoModel } from '@server/models/video/video'
|
|
|
|
import { VideoFileModel } from '@server/models/video/video-file'
|
2021-02-17 02:36:09 -06:00
|
|
|
import { MVideoFullLight } from '@server/types/models'
|
2022-02-11 03:51:33 -06:00
|
|
|
import { getLowercaseExtension } from '@shared/core-utils'
|
2023-04-21 07:55:10 -05:00
|
|
|
import { getVideoStreamDimensionsInfo, getVideoStreamFPS } from '@shared/ffmpeg'
|
2021-08-17 01:26:20 -05:00
|
|
|
import { VideoFileImportPayload, VideoStorage } from '@shared/models'
|
2020-11-20 10:16:55 -06:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2022-08-08 08:48:17 -05:00
|
|
|
import { JobQueue } from '../job-queue'
|
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
|
2022-08-08 08:48:17 -05:00
|
|
|
logger.info('Processing video file import in job %s.', job.id)
|
2019-03-19 11:10:53 -05:00
|
|
|
|
2022-06-28 07:57:51 -05:00
|
|
|
const video = await VideoModel.loadFull(payload.videoUUID)
|
2019-03-19 11:10:53 -05: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 01:26:20 -05:00
|
|
|
if (CONFIG.OBJECT_STORAGE.ENABLED) {
|
2022-08-08 08:48:17 -05:00
|
|
|
await JobQueue.Instance.createJob(await buildMoveToObjectStorageJob({ video, previousVideoState: video.state }))
|
2021-08-17 01:26:20 -05:00
|
|
|
} 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) {
|
2022-02-11 03:51:33 -06:00
|
|
|
const { resolution } = await getVideoStreamDimensionsInfo(inputFilePath)
|
2019-03-19 11:10:53 -05:00
|
|
|
const { size } = await stat(inputFilePath)
|
2022-02-11 03:51:33 -06:00
|
|
|
const fps = await getVideoStreamFPS(inputFilePath)
|
2019-03-19 11:10:53 -05:00
|
|
|
|
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
|
2022-07-29 07:50:41 -05:00
|
|
|
await video.removeWebTorrentFile(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
|
|
|
}
|