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