]>
Commit | Line | Data |
---|---|---|
1 | import * as Bull from 'bull' | |
2 | import { copy, stat } from 'fs-extra' | |
3 | import { getLowercaseExtension } from '@server/helpers/core-utils' | |
4 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | |
5 | import { generateWebTorrentVideoFilename, getVideoFilePath } from '@server/lib/video-paths' | |
6 | import { UserModel } from '@server/models/user/user' | |
7 | import { MVideoFullLight } from '@server/types/models' | |
8 | import { VideoFileImportPayload } from '@shared/models' | |
9 | import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' | |
10 | import { logger } from '../../../helpers/logger' | |
11 | import { VideoModel } from '../../../models/video/video' | |
12 | import { VideoFileModel } from '../../../models/video/video-file' | |
13 | import { onNewWebTorrentFileResolution } from './video-transcoding' | |
14 | ||
15 | async function processVideoFileImport (job: Bull.Job) { | |
16 | const payload = job.data as VideoFileImportPayload | |
17 | logger.info('Processing video file import in job %d.', job.id) | |
18 | ||
19 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
20 | // No video, maybe deleted? | |
21 | if (!video) { | |
22 | logger.info('Do not process job %d, video does not exist.', job.id) | |
23 | return undefined | |
24 | } | |
25 | ||
26 | const data = await getVideoFileResolution(payload.filePath) | |
27 | ||
28 | await updateVideoFile(video, payload.filePath) | |
29 | ||
30 | const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId) | |
31 | ||
32 | const newResolutionPayload = { | |
33 | type: 'new-resolution-to-webtorrent' as 'new-resolution-to-webtorrent', | |
34 | videoUUID: video.uuid, | |
35 | resolution: data.videoFileResolution, | |
36 | isPortraitMode: data.isPortraitMode, | |
37 | copyCodecs: false, | |
38 | isNewVideo: false | |
39 | } | |
40 | await onNewWebTorrentFileResolution(video, user, newResolutionPayload) | |
41 | ||
42 | return video | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | export { | |
48 | processVideoFileImport | |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) { | |
54 | const { videoFileResolution } = await getVideoFileResolution(inputFilePath) | |
55 | const { size } = await stat(inputFilePath) | |
56 | const fps = await getVideoFileFPS(inputFilePath) | |
57 | ||
58 | const fileExt = getLowercaseExtension(inputFilePath) | |
59 | ||
60 | const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution) | |
61 | ||
62 | if (currentVideoFile) { | |
63 | // Remove old file and old torrent | |
64 | await video.removeFileAndTorrent(currentVideoFile) | |
65 | // Remove the old video file from the array | |
66 | video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) | |
67 | ||
68 | await currentVideoFile.destroy() | |
69 | } | |
70 | ||
71 | const newVideoFile = new VideoFileModel({ | |
72 | resolution: videoFileResolution, | |
73 | extname: fileExt, | |
74 | filename: generateWebTorrentVideoFilename(videoFileResolution, fileExt), | |
75 | size, | |
76 | fps, | |
77 | videoId: video.id | |
78 | }) | |
79 | ||
80 | const outputPath = getVideoFilePath(video, newVideoFile) | |
81 | await copy(inputFilePath, outputPath) | |
82 | ||
83 | video.VideoFiles.push(newVideoFile) | |
84 | await createTorrentAndSetInfoHash(video, newVideoFile) | |
85 | ||
86 | await newVideoFile.save() | |
87 | } |