]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/job-queue/handlers/video-file-import.ts
Cleanup unavailable remote AP resource
[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 { getLowercaseExtension } from '@shared/core-utils'
4import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
5import { CONFIG } from '@server/initializers/config'
6import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
7import { generateWebTorrentVideoFilename } from '@server/lib/paths'
8import { addMoveToObjectStorageJob } from '@server/lib/video'
9import { VideoPathManager } from '@server/lib/video-path-manager'
10import { MVideoFullLight } from '@server/types/models'
11import { VideoFileImportPayload, VideoStorage } from '@shared/models'
12import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
13import { logger } from '../../../helpers/logger'
14import { VideoModel } from '../../../models/video/video'
15import { VideoFileModel } from '../../../models/video/video-file'
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.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
30 if (CONFIG.OBJECT_STORAGE.ENABLED) {
31 await addMoveToObjectStorageJob(video)
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 getVideoFileResolution(inputFilePath)
49 const { size } = await stat(inputFilePath)
50 const fps = await getVideoFileFPS(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.removeWebTorrentFileAndTorrent(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}