]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/job-queue/handlers/video-file-import.ts
Add ability to delete a specific video file
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file-import.ts
... / ...
CommitLineData
1import { Job } from 'bull'
2import { copy, stat } from 'fs-extra'
3import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
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'
9import { VideoModel } from '@server/models/video/video'
10import { VideoFileModel } from '@server/models/video/video-file'
11import { MVideoFullLight } from '@server/types/models'
12import { getLowercaseExtension } from '@shared/core-utils'
13import { VideoFileImportPayload, VideoStorage } from '@shared/models'
14import { getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg'
15import { logger } from '../../../helpers/logger'
16
17async function processVideoFileImport (job: Job) {
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.loadFull(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
30 if (CONFIG.OBJECT_STORAGE.ENABLED) {
31 await addMoveToObjectStorageJob({ video, previousVideoState: video.state })
32 } else {
33 await federateVideoIfNeeded(video, false)
34 }
35
36 return video
37}
38
39// ---------------------------------------------------------------------------
40
41export {
42 processVideoFileImport
43}
44
45// ---------------------------------------------------------------------------
46
47async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
48 const { resolution } = await getVideoStreamDimensionsInfo(inputFilePath)
49 const { size } = await stat(inputFilePath)
50 const fps = await getVideoStreamFPS(inputFilePath)
51
52 const fileExt = getLowercaseExtension(inputFilePath)
53
54 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)
55
56 if (currentVideoFile) {
57 // Remove old file and old torrent
58 await video.removeWebTorrentFile(currentVideoFile)
59 // Remove the old video file from the array
60 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
61
62 await currentVideoFile.destroy()
63 }
64
65 const newVideoFile = new VideoFileModel({
66 resolution,
67 extname: fileExt,
68 filename: generateWebTorrentVideoFilename(resolution, fileExt),
69 storage: VideoStorage.FILE_SYSTEM,
70 size,
71 fps,
72 videoId: video.id
73 })
74
75 const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
76 await copy(inputFilePath, outputPath)
77
78 video.VideoFiles.push(newVideoFile)
79 await createTorrentAndSetInfoHash(video, newVideoFile)
80
81 await newVideoFile.save()
82}