]>
Commit | Line | Data |
---|---|---|
fbad87b0 C |
1 | import * as Bull from 'bull' |
2 | import { logger } from '../../../helpers/logger' | |
3 | import { downloadYoutubeDLVideo } from '../../../helpers/youtube-dl' | |
4 | import { VideoImportModel } from '../../../models/video/video-import' | |
5 | import { VideoImportState } from '../../../../shared/models/videos' | |
6 | import { getDurationFromVideoFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' | |
7 | import { extname, join } from 'path' | |
8 | import { VideoFileModel } from '../../../models/video/video-file' | |
58d515e3 | 9 | import { CONFIG, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE, VIDEO_IMPORT_TIMEOUT } from '../../../initializers' |
6040f87d | 10 | import { downloadImage } from '../../../helpers/requests' |
fbad87b0 C |
11 | import { VideoState } from '../../../../shared' |
12 | import { JobQueue } from '../index' | |
13 | import { federateVideoIfNeeded } from '../../activitypub' | |
516df59b | 14 | import { VideoModel } from '../../../models/video/video' |
ce33919c | 15 | import { downloadWebTorrentVideo } from '../../../helpers/webtorrent' |
990b6a0b | 16 | import { getSecureTorrentName } from '../../../helpers/utils' |
e95e0463 | 17 | import { remove, rename, stat } from 'fs-extra' |
fbad87b0 | 18 | |
ce33919c | 19 | type VideoImportYoutubeDLPayload = { |
fbad87b0 C |
20 | type: 'youtube-dl' |
21 | videoImportId: number | |
ce33919c | 22 | |
fbad87b0 C |
23 | thumbnailUrl: string |
24 | downloadThumbnail: boolean | |
25 | downloadPreview: boolean | |
26 | } | |
27 | ||
ce33919c | 28 | type VideoImportTorrentPayload = { |
990b6a0b | 29 | type: 'magnet-uri' | 'torrent-file' |
ce33919c C |
30 | videoImportId: number |
31 | } | |
32 | ||
33 | export type VideoImportPayload = VideoImportYoutubeDLPayload | VideoImportTorrentPayload | |
34 | ||
fbad87b0 C |
35 | async function processVideoImport (job: Bull.Job) { |
36 | const payload = job.data as VideoImportPayload | |
fbad87b0 | 37 | |
ce33919c | 38 | if (payload.type === 'youtube-dl') return processYoutubeDLImport(job, payload) |
990b6a0b | 39 | if (payload.type === 'magnet-uri' || payload.type === 'torrent-file') return processTorrentImport(job, payload) |
ce33919c C |
40 | } |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | export { | |
45 | processVideoImport | |
46 | } | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | async function processTorrentImport (job: Bull.Job, payload: VideoImportTorrentPayload) { | |
51 | logger.info('Processing torrent video import in job %d.', job.id) | |
52 | ||
53 | const videoImport = await getVideoImportOrDie(payload.videoImportId) | |
990b6a0b | 54 | |
ce33919c C |
55 | const options = { |
56 | videoImportId: payload.videoImportId, | |
57 | ||
58 | downloadThumbnail: false, | |
59 | downloadPreview: false, | |
60 | ||
61 | generateThumbnail: true, | |
62 | generatePreview: true | |
63 | } | |
990b6a0b C |
64 | const target = { |
65 | torrentName: videoImport.torrentName ? getSecureTorrentName(videoImport.torrentName) : undefined, | |
66 | magnetUri: videoImport.magnetUri | |
67 | } | |
cf9166cf | 68 | return processFile(() => downloadWebTorrentVideo(target, VIDEO_IMPORT_TIMEOUT), videoImport, options) |
ce33919c C |
69 | } |
70 | ||
71 | async function processYoutubeDLImport (job: Bull.Job, payload: VideoImportYoutubeDLPayload) { | |
72 | logger.info('Processing youtubeDL video import in job %d.', job.id) | |
73 | ||
74 | const videoImport = await getVideoImportOrDie(payload.videoImportId) | |
75 | const options = { | |
76 | videoImportId: videoImport.id, | |
77 | ||
78 | downloadThumbnail: payload.downloadThumbnail, | |
79 | downloadPreview: payload.downloadPreview, | |
80 | thumbnailUrl: payload.thumbnailUrl, | |
81 | ||
82 | generateThumbnail: false, | |
83 | generatePreview: false | |
84 | } | |
85 | ||
cf9166cf | 86 | return processFile(() => downloadYoutubeDLVideo(videoImport.targetUrl, VIDEO_IMPORT_TIMEOUT), videoImport, options) |
ce33919c C |
87 | } |
88 | ||
89 | async function getVideoImportOrDie (videoImportId: number) { | |
90 | const videoImport = await VideoImportModel.loadAndPopulateVideo(videoImportId) | |
516df59b C |
91 | if (!videoImport || !videoImport.Video) { |
92 | throw new Error('Cannot import video %s: the video import or video linked to this import does not exist anymore.') | |
93 | } | |
fbad87b0 | 94 | |
ce33919c C |
95 | return videoImport |
96 | } | |
97 | ||
98 | type ProcessFileOptions = { | |
99 | videoImportId: number | |
100 | ||
101 | downloadThumbnail: boolean | |
102 | downloadPreview: boolean | |
103 | thumbnailUrl?: string | |
104 | ||
105 | generateThumbnail: boolean | |
106 | generatePreview: boolean | |
107 | } | |
108 | async function processFile (downloader: () => Promise<string>, videoImport: VideoImportModel, options: ProcessFileOptions) { | |
fbad87b0 | 109 | let tempVideoPath: string |
516df59b C |
110 | let videoDestFile: string |
111 | let videoFile: VideoFileModel | |
6040f87d | 112 | |
fbad87b0 C |
113 | try { |
114 | // Download video from youtubeDL | |
ce33919c | 115 | tempVideoPath = await downloader() |
fbad87b0 C |
116 | |
117 | // Get information about this video | |
62689b94 | 118 | const stats = await stat(tempVideoPath) |
3e17515e | 119 | const isAble = await videoImport.User.isAbleToUploadVideo({ size: stats.size }) |
a84b8fa5 C |
120 | if (isAble === false) { |
121 | throw new Error('The user video quota is exceeded with this video to import.') | |
122 | } | |
123 | ||
fbad87b0 | 124 | const { videoFileResolution } = await getVideoFileResolution(tempVideoPath) |
d7f83948 | 125 | const fps = await getVideoFileFPS(tempVideoPath) |
fbad87b0 C |
126 | const duration = await getDurationFromVideoFile(tempVideoPath) |
127 | ||
128 | // Create video file object in database | |
129 | const videoFileData = { | |
130 | extname: extname(tempVideoPath), | |
131 | resolution: videoFileResolution, | |
3e17515e | 132 | size: stats.size, |
fbad87b0 C |
133 | fps, |
134 | videoId: videoImport.videoId | |
135 | } | |
516df59b | 136 | videoFile = new VideoFileModel(videoFileData) |
58d515e3 | 137 | // To clean files if the import fails |
516df59b | 138 | videoImport.Video.VideoFiles = [ videoFile ] |
fbad87b0 C |
139 | |
140 | // Move file | |
516df59b | 141 | videoDestFile = join(CONFIG.STORAGE.VIDEOS_DIR, videoImport.Video.getVideoFilename(videoFile)) |
62689b94 | 142 | await rename(tempVideoPath, videoDestFile) |
516df59b | 143 | tempVideoPath = null // This path is not used anymore |
fbad87b0 C |
144 | |
145 | // Process thumbnail | |
ce33919c C |
146 | if (options.downloadThumbnail) { |
147 | if (options.thumbnailUrl) { | |
6040f87d | 148 | await downloadImage(options.thumbnailUrl, CONFIG.STORAGE.THUMBNAILS_DIR, videoImport.Video.getThumbnailName(), THUMBNAILS_SIZE) |
fbad87b0 C |
149 | } else { |
150 | await videoImport.Video.createThumbnail(videoFile) | |
151 | } | |
ce33919c C |
152 | } else if (options.generateThumbnail) { |
153 | await videoImport.Video.createThumbnail(videoFile) | |
fbad87b0 C |
154 | } |
155 | ||
156 | // Process preview | |
ce33919c C |
157 | if (options.downloadPreview) { |
158 | if (options.thumbnailUrl) { | |
6040f87d | 159 | await downloadImage(options.thumbnailUrl, CONFIG.STORAGE.PREVIEWS_DIR, videoImport.Video.getPreviewName(), PREVIEWS_SIZE) |
fbad87b0 C |
160 | } else { |
161 | await videoImport.Video.createPreview(videoFile) | |
162 | } | |
ce33919c C |
163 | } else if (options.generatePreview) { |
164 | await videoImport.Video.createPreview(videoFile) | |
fbad87b0 C |
165 | } |
166 | ||
167 | // Create torrent | |
168 | await videoImport.Video.createTorrentAndSetInfoHash(videoFile) | |
169 | ||
170 | const videoImportUpdated: VideoImportModel = await sequelizeTypescript.transaction(async t => { | |
516df59b C |
171 | // Refresh video |
172 | const video = await VideoModel.load(videoImport.videoId, t) | |
173 | if (!video) throw new Error('Video linked to import ' + videoImport.videoId + ' does not exist anymore.') | |
174 | videoImport.Video = video | |
175 | ||
176 | const videoFileCreated = await videoFile.save({ transaction: t }) | |
177 | video.VideoFiles = [ videoFileCreated ] | |
fbad87b0 C |
178 | |
179 | // Update video DB object | |
516df59b C |
180 | video.duration = duration |
181 | video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED | |
182 | const videoUpdated = await video.save({ transaction: t }) | |
fbad87b0 | 183 | |
590fb506 | 184 | // Now we can federate the video (reload from database, we need more attributes) |
627621c1 | 185 | const videoForFederation = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) |
590fb506 | 186 | await federateVideoIfNeeded(videoForFederation, true, t) |
fbad87b0 C |
187 | |
188 | // Update video import object | |
189 | videoImport.state = VideoImportState.SUCCESS | |
190 | const videoImportUpdated = await videoImport.save({ transaction: t }) | |
191 | ||
541006e3 | 192 | logger.info('Video %s imported.', video.uuid) |
fbad87b0 C |
193 | |
194 | videoImportUpdated.Video = videoUpdated | |
195 | return videoImportUpdated | |
196 | }) | |
197 | ||
198 | // Create transcoding jobs? | |
199 | if (videoImportUpdated.Video.state === VideoState.TO_TRANSCODE) { | |
200 | // Put uuid because we don't have id auto incremented for now | |
201 | const dataInput = { | |
202 | videoUUID: videoImportUpdated.Video.uuid, | |
203 | isNewVideo: true | |
204 | } | |
205 | ||
206 | await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) | |
207 | } | |
208 | ||
209 | } catch (err) { | |
210 | try { | |
e95e0463 | 211 | if (tempVideoPath) await remove(tempVideoPath) |
fbad87b0 | 212 | } catch (errUnlink) { |
516df59b | 213 | logger.warn('Cannot cleanup files after a video import error.', { err: errUnlink }) |
fbad87b0 C |
214 | } |
215 | ||
d7f83948 | 216 | videoImport.error = err.message |
fbad87b0 C |
217 | videoImport.state = VideoImportState.FAILED |
218 | await videoImport.save() | |
219 | ||
220 | throw err | |
221 | } | |
222 | } |