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