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