]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-import.ts
Add tests regarding video import
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-import.ts
CommitLineData
fbad87b0
C
1import * as Bull from 'bull'
2import { logger } from '../../../helpers/logger'
3import { downloadYoutubeDLVideo } from '../../../helpers/youtube-dl'
4import { VideoImportModel } from '../../../models/video/video-import'
5import { VideoImportState } from '../../../../shared/models/videos'
6import { getDurationFromVideoFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
7import { extname, join } from 'path'
8import { VideoFileModel } from '../../../models/video/video-file'
9import { renamePromise, statPromise, unlinkPromise } from '../../../helpers/core-utils'
10import { CONFIG, sequelizeTypescript } from '../../../initializers'
11import { doRequestAndSaveToFile } from '../../../helpers/requests'
12import { VideoState } from '../../../../shared'
13import { JobQueue } from '../index'
14import { federateVideoIfNeeded } from '../../activitypub'
516df59b 15import { VideoModel } from '../../../models/video/video'
fbad87b0
C
16
17export type VideoImportPayload = {
18 type: 'youtube-dl'
19 videoImportId: number
20 thumbnailUrl: string
21 downloadThumbnail: boolean
22 downloadPreview: boolean
23}
24
25async function processVideoImport (job: Bull.Job) {
26 const payload = job.data as VideoImportPayload
27 logger.info('Processing video import in job %d.', job.id)
28
29 const videoImport = await VideoImportModel.loadAndPopulateVideo(payload.videoImportId)
516df59b
C
30 if (!videoImport || !videoImport.Video) {
31 throw new Error('Cannot import video %s: the video import or video linked to this import does not exist anymore.')
32 }
fbad87b0
C
33
34 let tempVideoPath: string
516df59b
C
35 let videoDestFile: string
36 let videoFile: VideoFileModel
fbad87b0
C
37 try {
38 // Download video from youtubeDL
39 tempVideoPath = await downloadYoutubeDLVideo(videoImport.targetUrl)
40
41 // Get information about this video
42 const { videoFileResolution } = await getVideoFileResolution(tempVideoPath)
d7f83948 43 const fps = await getVideoFileFPS(tempVideoPath)
fbad87b0
C
44 const stats = await statPromise(tempVideoPath)
45 const duration = await getDurationFromVideoFile(tempVideoPath)
46
47 // Create video file object in database
48 const videoFileData = {
49 extname: extname(tempVideoPath),
50 resolution: videoFileResolution,
51 size: stats.size,
52 fps,
53 videoId: videoImport.videoId
54 }
516df59b
C
55 videoFile = new VideoFileModel(videoFileData)
56 // Import if the import fails, to clean files
57 videoImport.Video.VideoFiles = [ videoFile ]
fbad87b0
C
58
59 // Move file
516df59b
C
60 videoDestFile = join(CONFIG.STORAGE.VIDEOS_DIR, videoImport.Video.getVideoFilename(videoFile))
61 await renamePromise(tempVideoPath, videoDestFile)
62 tempVideoPath = null // This path is not used anymore
fbad87b0
C
63
64 // Process thumbnail
65 if (payload.downloadThumbnail) {
66 if (payload.thumbnailUrl) {
67 const destThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoImport.Video.getThumbnailName())
68 await doRequestAndSaveToFile({ method: 'GET', uri: payload.thumbnailUrl }, destThumbnailPath)
69 } else {
70 await videoImport.Video.createThumbnail(videoFile)
71 }
72 }
73
74 // Process preview
75 if (payload.downloadPreview) {
76 if (payload.thumbnailUrl) {
77 const destPreviewPath = join(CONFIG.STORAGE.PREVIEWS_DIR, videoImport.Video.getPreviewName())
78 await doRequestAndSaveToFile({ method: 'GET', uri: payload.thumbnailUrl }, destPreviewPath)
79 } else {
80 await videoImport.Video.createPreview(videoFile)
81 }
82 }
83
84 // Create torrent
85 await videoImport.Video.createTorrentAndSetInfoHash(videoFile)
86
87 const videoImportUpdated: VideoImportModel = await sequelizeTypescript.transaction(async t => {
516df59b
C
88 // Refresh video
89 const video = await VideoModel.load(videoImport.videoId, t)
90 if (!video) throw new Error('Video linked to import ' + videoImport.videoId + ' does not exist anymore.')
91 videoImport.Video = video
92
93 const videoFileCreated = await videoFile.save({ transaction: t })
94 video.VideoFiles = [ videoFileCreated ]
fbad87b0
C
95
96 // Update video DB object
516df59b
C
97 video.duration = duration
98 video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED
99 const videoUpdated = await video.save({ transaction: t })
fbad87b0 100
590fb506
C
101 // Now we can federate the video (reload from database, we need more attributes)
102 const videoForFederation = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
103 await federateVideoIfNeeded(videoForFederation, true, t)
fbad87b0
C
104
105 // Update video import object
106 videoImport.state = VideoImportState.SUCCESS
107 const videoImportUpdated = await videoImport.save({ transaction: t })
108
109 logger.info('Video %s imported.', videoImport.targetUrl)
110
111 videoImportUpdated.Video = videoUpdated
112 return videoImportUpdated
113 })
114
115 // Create transcoding jobs?
116 if (videoImportUpdated.Video.state === VideoState.TO_TRANSCODE) {
117 // Put uuid because we don't have id auto incremented for now
118 const dataInput = {
119 videoUUID: videoImportUpdated.Video.uuid,
120 isNewVideo: true
121 }
122
123 await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
124 }
125
126 } catch (err) {
127 try {
128 if (tempVideoPath) await unlinkPromise(tempVideoPath)
129 } catch (errUnlink) {
516df59b 130 logger.warn('Cannot cleanup files after a video import error.', { err: errUnlink })
fbad87b0
C
131 }
132
d7f83948 133 videoImport.error = err.message
fbad87b0
C
134 videoImport.state = VideoImportState.FAILED
135 await videoImport.save()
136
137 throw err
138 }
139}
140
141// ---------------------------------------------------------------------------
142
143export {
144 processVideoImport
145}