]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
30842128 1import * as Bull from 'bull'
30842128 2import { copy, stat } from 'fs-extra'
30842128 3import { extname } from 'path'
ea54cd04 4import { getLowercaseExtension } from '@server/helpers/core-utils'
d7a25329 5import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
90a8bd30 6import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
7d9ba5c0 7import { UserModel } from '@server/models/user/user'
2451916e 8import { MVideoFullLight } from '@server/types/models'
8dc8a34e 9import { VideoFileImportPayload } from '@shared/models'
daf6e480
C
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'
24516aa2 14import { onNewWebTorrentFileResolution } from './video-transcoding'
30842128
C
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
69eddafb
C
27 const data = await getVideoFileResolution(payload.filePath)
28
30842128
C
29 await updateVideoFile(video, payload.filePath)
30
77d7e851 31 const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
69eddafb
C
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)
77d7e851 42
30842128
C
43 return video
44}
45
46// ---------------------------------------------------------------------------
47
48export {
49 processVideoFileImport
50}
51
52// ---------------------------------------------------------------------------
53
90a8bd30 54async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
30842128
C
55 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
56 const { size } = await stat(inputFilePath)
57 const fps = await getVideoFileFPS(inputFilePath)
58
ea54cd04 59 const fileExt = getLowercaseExtension(inputFilePath)
30842128 60
2451916e 61 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution)
30842128
C
62
63 if (currentVideoFile) {
64 // Remove old file and old torrent
65 await video.removeFile(currentVideoFile)
90a8bd30 66 await currentVideoFile.removeTorrent()
30842128
C
67 // Remove the old video file from the array
68 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
69
2451916e 70 await currentVideoFile.destroy()
30842128
C
71 }
72
2451916e
C
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 })
30842128 81
2451916e
C
82 const outputPath = getVideoFilePath(video, newVideoFile)
83 await copy(inputFilePath, outputPath)
30842128 84
2451916e 85 video.VideoFiles.push(newVideoFile)
8efc27bf 86 await createTorrentAndSetInfoHash(video, newVideoFile)
30842128 87
2451916e 88 await newVideoFile.save()
30842128 89}