]>
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' | |
9 | ||
10 | export type VideoFileImportPayload = { | |
11 | videoUUID: string, | |
12 | filePath: string | |
13 | } | |
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 | await updateVideoFile(video, payload.filePath) | |
27 | ||
536598cf | 28 | await publishNewResolutionIfNeeded(video) |
30842128 C |
29 | return video |
30 | } | |
31 | ||
32 | // --------------------------------------------------------------------------- | |
33 | ||
34 | export { | |
35 | processVideoFileImport | |
36 | } | |
37 | ||
38 | // --------------------------------------------------------------------------- | |
39 | ||
40 | async function updateVideoFile (video: VideoModel, inputFilePath: string) { | |
41 | const { videoFileResolution } = await getVideoFileResolution(inputFilePath) | |
42 | const { size } = await stat(inputFilePath) | |
43 | const fps = await getVideoFileFPS(inputFilePath) | |
44 | ||
45 | let updatedVideoFile = new VideoFileModel({ | |
46 | resolution: videoFileResolution, | |
47 | extname: extname(inputFilePath), | |
48 | size, | |
49 | fps, | |
50 | videoId: video.id | |
51 | }) | |
52 | ||
53 | const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution) | |
54 | ||
55 | if (currentVideoFile) { | |
56 | // Remove old file and old torrent | |
57 | await video.removeFile(currentVideoFile) | |
58 | await video.removeTorrent(currentVideoFile) | |
59 | // Remove the old video file from the array | |
60 | video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) | |
61 | ||
62 | // Update the database | |
63 | currentVideoFile.set('extname', updatedVideoFile.extname) | |
64 | currentVideoFile.set('size', updatedVideoFile.size) | |
65 | currentVideoFile.set('fps', updatedVideoFile.fps) | |
66 | ||
67 | updatedVideoFile = currentVideoFile | |
68 | } | |
69 | ||
70 | const outputPath = video.getVideoFilePath(updatedVideoFile) | |
71 | await copy(inputFilePath, outputPath) | |
72 | ||
73 | await video.createTorrentAndSetInfoHash(updatedVideoFile) | |
74 | ||
75 | await updatedVideoFile.save() | |
76 | ||
77 | video.VideoFiles.push(updatedVideoFile) | |
78 | } |