]>
Commit | Line | Data |
---|---|---|
30842128 C |
1 | import * as Bull from 'bull' |
2 | import { logger } from '../../../helpers/logger' | |
3 | import { VideoModel } from '../../../models/video/video' | |
536598cf | 4 | import { publishNewResolutionIfNeeded } from './video-transcoding' |
30842128 C |
5 | import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' |
6 | import { copy, stat } from 'fs-extra' | |
7 | import { VideoFileModel } from '../../../models/video/video-file' | |
8 | import { extname } from 'path' | |
453e83ea | 9 | import { MVideoFile, MVideoWithFile } from '@server/typings/models' |
d7a25329 C |
10 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' |
11 | import { getVideoFilePath } from '@server/lib/video-paths' | |
30842128 C |
12 | |
13 | export type VideoFileImportPayload = { | |
14 | videoUUID: string, | |
15 | filePath: string | |
16 | } | |
17 | ||
18 | async function processVideoFileImport (job: Bull.Job) { | |
19 | const payload = job.data as VideoFileImportPayload | |
20 | logger.info('Processing video file import in job %d.', job.id) | |
21 | ||
22 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
23 | // No video, maybe deleted? | |
24 | if (!video) { | |
25 | logger.info('Do not process job %d, video does not exist.', job.id) | |
26 | return undefined | |
27 | } | |
28 | ||
29 | await updateVideoFile(video, payload.filePath) | |
30 | ||
536598cf | 31 | await publishNewResolutionIfNeeded(video) |
30842128 C |
32 | return video |
33 | } | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | ||
37 | export { | |
38 | processVideoFileImport | |
39 | } | |
40 | ||
41 | // --------------------------------------------------------------------------- | |
42 | ||
453e83ea | 43 | async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) { |
30842128 C |
44 | const { videoFileResolution } = await getVideoFileResolution(inputFilePath) |
45 | const { size } = await stat(inputFilePath) | |
46 | const fps = await getVideoFileFPS(inputFilePath) | |
47 | ||
48 | let updatedVideoFile = new VideoFileModel({ | |
49 | resolution: videoFileResolution, | |
50 | extname: extname(inputFilePath), | |
51 | size, | |
52 | fps, | |
53 | videoId: video.id | |
453e83ea | 54 | }) as MVideoFile |
30842128 C |
55 | |
56 | const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution) | |
57 | ||
58 | if (currentVideoFile) { | |
59 | // Remove old file and old torrent | |
60 | await video.removeFile(currentVideoFile) | |
61 | await video.removeTorrent(currentVideoFile) | |
62 | // Remove the old video file from the array | |
63 | video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) | |
64 | ||
65 | // Update the database | |
453e83ea C |
66 | currentVideoFile.extname = updatedVideoFile.extname |
67 | currentVideoFile.size = updatedVideoFile.size | |
68 | currentVideoFile.fps = updatedVideoFile.fps | |
30842128 C |
69 | |
70 | updatedVideoFile = currentVideoFile | |
71 | } | |
72 | ||
d7a25329 | 73 | const outputPath = getVideoFilePath(video, updatedVideoFile) |
30842128 C |
74 | await copy(inputFilePath, outputPath) |
75 | ||
d7a25329 | 76 | await createTorrentAndSetInfoHash(video, updatedVideoFile) |
30842128 C |
77 | |
78 | await updatedVideoFile.save() | |
79 | ||
80 | video.VideoFiles.push(updatedVideoFile) | |
81 | } |