]>
Commit | Line | Data |
---|---|---|
30842128 | 1 | import * as Bull from 'bull' |
30842128 | 2 | import { copy, stat } from 'fs-extra' |
30842128 | 3 | import { extname } from 'path' |
d7a25329 | 4 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' |
90a8bd30 | 5 | import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths' |
77d7e851 | 6 | import { UserModel } from '@server/models/account/user' |
2451916e | 7 | import { MVideoFullLight } from '@server/types/models' |
8dc8a34e | 8 | import { VideoFileImportPayload } from '@shared/models' |
daf6e480 C |
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' | |
24516aa2 | 13 | import { onNewWebTorrentFileResolution } from './video-transcoding' |
30842128 C |
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 | ||
69eddafb C |
26 | const data = await getVideoFileResolution(payload.filePath) |
27 | ||
30842128 C |
28 | await updateVideoFile(video, payload.filePath) |
29 | ||
77d7e851 | 30 | const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId) |
69eddafb C |
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) | |
77d7e851 | 41 | |
30842128 C |
42 | return video |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | export { | |
48 | processVideoFileImport | |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
90a8bd30 | 53 | async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) { |
30842128 C |
54 | const { videoFileResolution } = await getVideoFileResolution(inputFilePath) |
55 | const { size } = await stat(inputFilePath) | |
56 | const fps = await getVideoFileFPS(inputFilePath) | |
57 | ||
90a8bd30 | 58 | const fileExt = extname(inputFilePath) |
30842128 | 59 | |
2451916e | 60 | const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution) |
30842128 C |
61 | |
62 | if (currentVideoFile) { | |
63 | // Remove old file and old torrent | |
64 | await video.removeFile(currentVideoFile) | |
90a8bd30 | 65 | await currentVideoFile.removeTorrent() |
30842128 C |
66 | // Remove the old video file from the array |
67 | video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) | |
68 | ||
2451916e | 69 | await currentVideoFile.destroy() |
30842128 C |
70 | } |
71 | ||
2451916e C |
72 | const newVideoFile = new VideoFileModel({ |
73 | resolution: videoFileResolution, | |
74 | extname: fileExt, | |
75 | filename: generateVideoFilename(video, false, videoFileResolution, fileExt), | |
76 | size, | |
77 | fps, | |
78 | videoId: video.id | |
79 | }) | |
30842128 | 80 | |
2451916e C |
81 | const outputPath = getVideoFilePath(video, newVideoFile) |
82 | await copy(inputFilePath, outputPath) | |
30842128 | 83 | |
2451916e C |
84 | video.VideoFiles.push(newVideoFile) |
85 | await createTorrentAndSetInfoHash(video, video, newVideoFile) | |
30842128 | 86 | |
2451916e | 87 | await newVideoFile.save() |
30842128 | 88 | } |