]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-file-import.ts
Add video edition finished notification
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file-import.ts
CommitLineData
41fb13c3 1import { Job } from 'bull'
30842128 2import { copy, stat } from 'fs-extra'
d7a25329 3import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
0305db28
JB
4import { CONFIG } from '@server/initializers/config'
5import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
6import { generateWebTorrentVideoFilename } from '@server/lib/paths'
7import { addMoveToObjectStorageJob } from '@server/lib/video'
8import { VideoPathManager } from '@server/lib/video-path-manager'
c729caf6
C
9import { VideoModel } from '@server/models/video/video'
10import { VideoFileModel } from '@server/models/video/video-file'
2451916e 11import { MVideoFullLight } from '@server/types/models'
c729caf6 12import { getLowercaseExtension } from '@shared/core-utils'
0305db28 13import { VideoFileImportPayload, VideoStorage } from '@shared/models'
c729caf6 14import { getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg'
daf6e480 15import { logger } from '../../../helpers/logger'
30842128 16
41fb13c3 17async function processVideoFileImport (job: Job) {
30842128
C
18 const payload = job.data as VideoFileImportPayload
19 logger.info('Processing video file import in job %d.', job.id)
20
21 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
22 // No video, maybe deleted?
23 if (!video) {
24 logger.info('Do not process job %d, video does not exist.', job.id)
25 return undefined
26 }
27
28 await updateVideoFile(video, payload.filePath)
29
0305db28 30 if (CONFIG.OBJECT_STORAGE.ENABLED) {
1808a1f8 31 await addMoveToObjectStorageJob({ video, previousVideoState: video.state })
0305db28
JB
32 } else {
33 await federateVideoIfNeeded(video, false)
69eddafb 34 }
77d7e851 35
30842128
C
36 return video
37}
38
39// ---------------------------------------------------------------------------
40
41export {
42 processVideoFileImport
43}
44
45// ---------------------------------------------------------------------------
46
90a8bd30 47async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
c729caf6 48 const { resolution } = await getVideoStreamDimensionsInfo(inputFilePath)
30842128 49 const { size } = await stat(inputFilePath)
c729caf6 50 const fps = await getVideoStreamFPS(inputFilePath)
30842128 51
ea54cd04 52 const fileExt = getLowercaseExtension(inputFilePath)
30842128 53
679c12e6 54 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)
30842128
C
55
56 if (currentVideoFile) {
57 // Remove old file and old torrent
b46cf4b9 58 await video.removeWebTorrentFileAndTorrent(currentVideoFile)
30842128
C
59 // Remove the old video file from the array
60 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
61
2451916e 62 await currentVideoFile.destroy()
30842128
C
63 }
64
2451916e 65 const newVideoFile = new VideoFileModel({
679c12e6 66 resolution,
2451916e 67 extname: fileExt,
679c12e6 68 filename: generateWebTorrentVideoFilename(resolution, fileExt),
0305db28 69 storage: VideoStorage.FILE_SYSTEM,
2451916e
C
70 size,
71 fps,
72 videoId: video.id
73 })
30842128 74
0305db28 75 const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
2451916e 76 await copy(inputFilePath, outputPath)
30842128 77
2451916e 78 video.VideoFiles.push(newVideoFile)
8efc27bf 79 await createTorrentAndSetInfoHash(video, newVideoFile)
30842128 80
2451916e 81 await newVideoFile.save()
30842128 82}