aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/job-queue/handlers/video-file-import.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-03-19 17:10:53 +0100
committerChocobozzz <me@florianbigard.com>2019-03-19 17:10:53 +0100
commit308421283adf8df1a6a1972cd0efe198b0d93435 (patch)
tree073e197fe55ac9a5be9c8aadba9fb945295c1c8a /server/lib/job-queue/handlers/video-file-import.ts
parenta0327eedb0136c4ba7358df80b75cc56bd25ffb8 (diff)
downloadPeerTube-308421283adf8df1a6a1972cd0efe198b0d93435.tar.gz
PeerTube-308421283adf8df1a6a1972cd0efe198b0d93435.tar.zst
PeerTube-308421283adf8df1a6a1972cd0efe198b0d93435.zip
Move video file import in its own file
Diffstat (limited to 'server/lib/job-queue/handlers/video-file-import.ts')
-rw-r--r--server/lib/job-queue/handlers/video-file-import.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/server/lib/job-queue/handlers/video-file-import.ts b/server/lib/job-queue/handlers/video-file-import.ts
new file mode 100644
index 000000000..921d9a083
--- /dev/null
+++ b/server/lib/job-queue/handlers/video-file-import.ts
@@ -0,0 +1,78 @@
1import * as Bull from 'bull'
2import { logger } from '../../../helpers/logger'
3import { VideoModel } from '../../../models/video/video'
4import { publishVideoIfNeeded } from './video-transcoding'
5import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
6import { copy, stat } from 'fs-extra'
7import { VideoFileModel } from '../../../models/video/video-file'
8import { extname } from 'path'
9
10export type VideoFileImportPayload = {
11 videoUUID: string,
12 filePath: string
13}
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
28 await publishVideoIfNeeded(video)
29 return video
30}
31
32// ---------------------------------------------------------------------------
33
34export {
35 processVideoFileImport
36}
37
38// ---------------------------------------------------------------------------
39
40async function updateVideoFile (video: VideoModel, inputFilePath: string) {
41 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
42 const { size } = await stat(inputFilePath)
43 const fps = await getVideoFileFPS(inputFilePath)
44
45 let updatedVideoFile = new VideoFileModel({
46 resolution: videoFileResolution,
47 extname: extname(inputFilePath),
48 size,
49 fps,
50 videoId: video.id
51 })
52
53 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
54
55 if (currentVideoFile) {
56 // Remove old file and old torrent
57 await video.removeFile(currentVideoFile)
58 await video.removeTorrent(currentVideoFile)
59 // Remove the old video file from the array
60 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
61
62 // Update the database
63 currentVideoFile.set('extname', updatedVideoFile.extname)
64 currentVideoFile.set('size', updatedVideoFile.size)
65 currentVideoFile.set('fps', updatedVideoFile.fps)
66
67 updatedVideoFile = currentVideoFile
68 }
69
70 const outputPath = video.getVideoFilePath(updatedVideoFile)
71 await copy(inputFilePath, outputPath)
72
73 await video.createTorrentAndSetInfoHash(updatedVideoFile)
74
75 await updatedVideoFile.save()
76
77 video.VideoFiles.push(updatedVideoFile)
78}