]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
30842128 1import * as Bull from 'bull'
30842128 2import { copy, stat } from 'fs-extra'
30842128 3import { extname } from 'path'
d7a25329
C
4import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
5import { getVideoFilePath } from '@server/lib/video-paths'
77d7e851 6import { UserModel } from '@server/models/account/user'
daf6e480 7import { MVideoFile, MVideoWithFile } from '@server/types/models'
8dc8a34e 8import { VideoFileImportPayload } from '@shared/models'
daf6e480
C
9import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
10import { logger } from '../../../helpers/logger'
11import { VideoModel } from '../../../models/video/video'
12import { VideoFileModel } from '../../../models/video/video-file'
24516aa2 13import { onNewWebTorrentFileResolution } from './video-transcoding'
30842128
C
14
15async 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
77d7e851
C
28 const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
29 await onNewWebTorrentFileResolution(video, user)
30
30842128
C
31 return video
32}
33
34// ---------------------------------------------------------------------------
35
36export {
37 processVideoFileImport
38}
39
40// ---------------------------------------------------------------------------
41
453e83ea 42async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) {
30842128
C
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
453e83ea 53 }) as MVideoFile
30842128
C
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
453e83ea
C
65 currentVideoFile.extname = updatedVideoFile.extname
66 currentVideoFile.size = updatedVideoFile.size
67 currentVideoFile.fps = updatedVideoFile.fps
30842128
C
68
69 updatedVideoFile = currentVideoFile
70 }
71
d7a25329 72 const outputPath = getVideoFilePath(video, updatedVideoFile)
30842128
C
73 await copy(inputFilePath, outputPath)
74
d7a25329 75 await createTorrentAndSetInfoHash(video, updatedVideoFile)
30842128
C
76
77 await updatedVideoFile.save()
78
79 video.VideoFiles.push(updatedVideoFile)
80}