]>
Commit | Line | Data |
---|---|---|
30842128 | 1 | import * as Bull from 'bull' |
30842128 | 2 | import { copy, stat } from 'fs-extra' |
ea54cd04 | 3 | import { getLowercaseExtension } from '@server/helpers/core-utils' |
d7a25329 | 4 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' |
0305db28 JB |
5 | import { CONFIG } from '@server/initializers/config' |
6 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' | |
7 | import { generateWebTorrentVideoFilename } from '@server/lib/paths' | |
8 | import { addMoveToObjectStorageJob } from '@server/lib/video' | |
9 | import { VideoPathManager } from '@server/lib/video-path-manager' | |
7d9ba5c0 | 10 | import { UserModel } from '@server/models/user/user' |
2451916e | 11 | import { MVideoFullLight } from '@server/types/models' |
0305db28 | 12 | import { VideoFileImportPayload, VideoStorage } from '@shared/models' |
daf6e480 C |
13 | import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' |
14 | import { logger } from '../../../helpers/logger' | |
15 | import { VideoModel } from '../../../models/video/video' | |
16 | import { VideoFileModel } from '../../../models/video/video-file' | |
0305db28 | 17 | import { createHlsJobIfEnabled } from './video-transcoding' |
30842128 C |
18 | |
19 | async function processVideoFileImport (job: Bull.Job) { | |
20 | const payload = job.data as VideoFileImportPayload | |
21 | logger.info('Processing video file import in job %d.', job.id) | |
22 | ||
23 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
24 | // No video, maybe deleted? | |
25 | if (!video) { | |
26 | logger.info('Do not process job %d, video does not exist.', job.id) | |
27 | return undefined | |
28 | } | |
29 | ||
69eddafb C |
30 | const data = await getVideoFileResolution(payload.filePath) |
31 | ||
30842128 C |
32 | await updateVideoFile(video, payload.filePath) |
33 | ||
77d7e851 | 34 | const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId) |
69eddafb | 35 | |
0305db28 | 36 | await createHlsJobIfEnabled(user, { |
69eddafb | 37 | videoUUID: video.uuid, |
679c12e6 | 38 | resolution: data.resolution, |
69eddafb | 39 | isPortraitMode: data.isPortraitMode, |
0305db28 JB |
40 | copyCodecs: true, |
41 | isMaxQuality: false | |
42 | }) | |
43 | ||
44 | if (CONFIG.OBJECT_STORAGE.ENABLED) { | |
45 | await addMoveToObjectStorageJob(video) | |
46 | } else { | |
47 | await federateVideoIfNeeded(video, false) | |
69eddafb | 48 | } |
77d7e851 | 49 | |
30842128 C |
50 | return video |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | export { | |
56 | processVideoFileImport | |
57 | } | |
58 | ||
59 | // --------------------------------------------------------------------------- | |
60 | ||
90a8bd30 | 61 | async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) { |
679c12e6 | 62 | const { resolution } = await getVideoFileResolution(inputFilePath) |
30842128 C |
63 | const { size } = await stat(inputFilePath) |
64 | const fps = await getVideoFileFPS(inputFilePath) | |
65 | ||
ea54cd04 | 66 | const fileExt = getLowercaseExtension(inputFilePath) |
30842128 | 67 | |
679c12e6 | 68 | const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution) |
30842128 C |
69 | |
70 | if (currentVideoFile) { | |
71 | // Remove old file and old torrent | |
764b1a14 | 72 | await video.removeFileAndTorrent(currentVideoFile) |
30842128 C |
73 | // Remove the old video file from the array |
74 | video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) | |
75 | ||
2451916e | 76 | await currentVideoFile.destroy() |
30842128 C |
77 | } |
78 | ||
2451916e | 79 | const newVideoFile = new VideoFileModel({ |
679c12e6 | 80 | resolution, |
2451916e | 81 | extname: fileExt, |
679c12e6 | 82 | filename: generateWebTorrentVideoFilename(resolution, fileExt), |
0305db28 | 83 | storage: VideoStorage.FILE_SYSTEM, |
2451916e C |
84 | size, |
85 | fps, | |
86 | videoId: video.id | |
87 | }) | |
30842128 | 88 | |
0305db28 | 89 | const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile) |
2451916e | 90 | await copy(inputFilePath, outputPath) |
30842128 | 91 | |
2451916e | 92 | video.VideoFiles.push(newVideoFile) |
8efc27bf | 93 | await createTorrentAndSetInfoHash(video, newVideoFile) |
30842128 | 94 | |
2451916e | 95 | await newVideoFile.save() |
30842128 | 96 | } |