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