]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-file-import.ts
Avoir some circular dependencies
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file-import.ts
CommitLineData
30842128
C
1import * as Bull from 'bull'
2import { logger } from '../../../helpers/logger'
3import { VideoModel } from '../../../models/video/video'
536598cf 4import { publishNewResolutionIfNeeded } from './video-transcoding'
30842128
C
5import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
6import { copy, stat } from 'fs-extra'
7import { VideoFileModel } from '../../../models/video/video-file'
8import { extname } from 'path'
453e83ea 9import { MVideoFile, MVideoWithFile } from '@server/typings/models'
d7a25329
C
10import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
11import { getVideoFilePath } from '@server/lib/video-paths'
8dc8a34e 12import { VideoFileImportPayload } from '@shared/models'
30842128
C
13
14async 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
33export {
34 processVideoFileImport
35}
36
37// ---------------------------------------------------------------------------
38
453e83ea 39async 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}