]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-file-import.ts
Correctly remove torrents with HLS only
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file-import.ts
1 import * as Bull from 'bull'
2 import { copy, stat } from 'fs-extra'
3 import { extname } from 'path'
4 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
5 import { getVideoFilePath } from '@server/lib/video-paths'
6 import { UserModel } from '@server/models/account/user'
7 import { MVideoFile, MVideoWithFile } from '@server/types/models'
8 import { VideoFileImportPayload } from '@shared/models'
9 import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
10 import { logger } from '../../../helpers/logger'
11 import { VideoModel } from '../../../models/video/video'
12 import { VideoFileModel } from '../../../models/video/video-file'
13 import { onNewWebTorrentFileResolution } from './video-transcoding'
14
15 async function processVideoFileImport (job: Bull.Job) {
16 const payload = job.data as VideoFileImportPayload
17 logger.info('Processing video file import in job %d.', job.id)
18
19 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
20 // No video, maybe deleted?
21 if (!video) {
22 logger.info('Do not process job %d, video does not exist.', job.id)
23 return undefined
24 }
25
26 await updateVideoFile(video, payload.filePath)
27
28 const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
29 await onNewWebTorrentFileResolution(video, user)
30
31 return video
32 }
33
34 // ---------------------------------------------------------------------------
35
36 export {
37 processVideoFileImport
38 }
39
40 // ---------------------------------------------------------------------------
41
42 async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) {
43 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
44 const { size } = await stat(inputFilePath)
45 const fps = await getVideoFileFPS(inputFilePath)
46
47 let updatedVideoFile = new VideoFileModel({
48 resolution: videoFileResolution,
49 extname: extname(inputFilePath),
50 size,
51 fps,
52 videoId: video.id
53 }) as MVideoFile
54
55 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
56
57 if (currentVideoFile) {
58 // Remove old file and old torrent
59 await video.removeFile(currentVideoFile)
60 await video.removeTorrent(currentVideoFile)
61 // Remove the old video file from the array
62 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
63
64 // Update the database
65 currentVideoFile.extname = updatedVideoFile.extname
66 currentVideoFile.size = updatedVideoFile.size
67 currentVideoFile.fps = updatedVideoFile.fps
68
69 updatedVideoFile = currentVideoFile
70 }
71
72 const outputPath = getVideoFilePath(video, updatedVideoFile)
73 await copy(inputFilePath, outputPath)
74
75 await createTorrentAndSetInfoHash(video, updatedVideoFile)
76
77 await updatedVideoFile.save()
78
79 video.VideoFiles.push(updatedVideoFile)
80 }