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