]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import { Job } from 'bull' |
2158ac90 | 2 | import { move, remove, stat } from 'fs-extra' |
44d1f7f2 | 3 | import { retryTransactionWrapper } from '@server/helpers/database-utils' |
62549e6c | 4 | import { YoutubeDLWrapper } from '@server/helpers/youtube-dl' |
2158ac90 | 5 | import { isPostImportVideoAccepted } from '@server/lib/moderation' |
0305db28 | 6 | import { generateWebTorrentVideoFilename } from '@server/lib/paths' |
2158ac90 | 7 | import { Hooks } from '@server/lib/plugins/hooks' |
2539932e | 8 | import { ServerConfigManager } from '@server/lib/server-config-manager' |
fb719404 | 9 | import { isAbleToUploadVideo } from '@server/lib/user' |
0305db28 JB |
10 | import { addMoveToObjectStorageJob, addOptimizeOrMergeAudioJob } from '@server/lib/video' |
11 | import { VideoPathManager } from '@server/lib/video-path-manager' | |
12 | import { buildNextVideoState } from '@server/lib/video-state' | |
44d1f7f2 | 13 | import { ThumbnailModel } from '@server/models/video/thumbnail' |
26d6bf65 | 14 | import { MVideoImportDefault, MVideoImportDefaultFiles, MVideoImportVideo } from '@server/types/models/video/video-import' |
d17c7b4e C |
15 | import { getLowercaseExtension } from '@shared/core-utils' |
16 | import { isAudioFile } from '@shared/extra-utils' | |
2158ac90 | 17 | import { |
d17c7b4e | 18 | ThumbnailType, |
2158ac90 | 19 | VideoImportPayload, |
d17c7b4e | 20 | VideoImportState, |
2158ac90 RK |
21 | VideoImportTorrentPayload, |
22 | VideoImportTorrentPayloadType, | |
23 | VideoImportYoutubeDLPayload, | |
24 | VideoImportYoutubeDLPayloadType, | |
d17c7b4e | 25 | VideoResolution, |
2158ac90 | 26 | VideoState |
d17c7b4e | 27 | } from '@shared/models' |
482b2623 | 28 | import { ffprobePromise, getDurationFromVideoFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' |
2158ac90 | 29 | import { logger } from '../../../helpers/logger' |
990b6a0b | 30 | import { getSecureTorrentName } from '../../../helpers/utils' |
2158ac90 | 31 | import { createTorrentAndSetInfoHash, downloadWebTorrentVideo } from '../../../helpers/webtorrent' |
2158ac90 | 32 | import { VIDEO_IMPORT_TIMEOUT } from '../../../initializers/constants' |
74dc3bca | 33 | import { sequelizeTypescript } from '../../../initializers/database' |
2158ac90 RK |
34 | import { VideoModel } from '../../../models/video/video' |
35 | import { VideoFileModel } from '../../../models/video/video-file' | |
36 | import { VideoImportModel } from '../../../models/video/video-import' | |
26d6bf65 | 37 | import { MThumbnail } from '../../../types/models/video/thumbnail' |
2158ac90 RK |
38 | import { federateVideoIfNeeded } from '../../activitypub/videos' |
39 | import { Notifier } from '../../notifier' | |
40 | import { generateVideoMiniature } from '../../thumbnail' | |
ce33919c | 41 | |
41fb13c3 | 42 | async function processVideoImport (job: Job) { |
fbad87b0 | 43 | const payload = job.data as VideoImportPayload |
fbad87b0 | 44 | |
ce33919c | 45 | if (payload.type === 'youtube-dl') return processYoutubeDLImport(job, payload) |
990b6a0b | 46 | if (payload.type === 'magnet-uri' || payload.type === 'torrent-file') return processTorrentImport(job, payload) |
ce33919c C |
47 | } |
48 | ||
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | export { | |
52 | processVideoImport | |
53 | } | |
54 | ||
55 | // --------------------------------------------------------------------------- | |
56 | ||
41fb13c3 | 57 | async function processTorrentImport (job: Job, payload: VideoImportTorrentPayload) { |
ce33919c C |
58 | logger.info('Processing torrent video import in job %d.', job.id) |
59 | ||
60 | const videoImport = await getVideoImportOrDie(payload.videoImportId) | |
990b6a0b | 61 | |
ce33919c | 62 | const options = { |
2158ac90 | 63 | type: payload.type, |
e3b4c084 | 64 | videoImportId: payload.videoImportId |
ce33919c | 65 | } |
990b6a0b C |
66 | const target = { |
67 | torrentName: videoImport.torrentName ? getSecureTorrentName(videoImport.torrentName) : undefined, | |
02b286f8 | 68 | uri: videoImport.magnetUri |
990b6a0b | 69 | } |
cf9166cf | 70 | return processFile(() => downloadWebTorrentVideo(target, VIDEO_IMPORT_TIMEOUT), videoImport, options) |
ce33919c C |
71 | } |
72 | ||
41fb13c3 | 73 | async function processYoutubeDLImport (job: Job, payload: VideoImportYoutubeDLPayload) { |
ce33919c C |
74 | logger.info('Processing youtubeDL video import in job %d.', job.id) |
75 | ||
76 | const videoImport = await getVideoImportOrDie(payload.videoImportId) | |
77 | const options = { | |
2158ac90 | 78 | type: payload.type, |
e3b4c084 | 79 | videoImportId: videoImport.id |
ce33919c C |
80 | } |
81 | ||
62549e6c | 82 | const youtubeDL = new YoutubeDLWrapper(videoImport.targetUrl, ServerConfigManager.Instance.getEnabledResolutions('vod')) |
1bcb03a1 | 83 | |
454c20fa | 84 | return processFile( |
62549e6c | 85 | () => youtubeDL.downloadVideo(payload.fileExt, VIDEO_IMPORT_TIMEOUT), |
454c20fa RK |
86 | videoImport, |
87 | options | |
88 | ) | |
ce33919c C |
89 | } |
90 | ||
91 | async function getVideoImportOrDie (videoImportId: number) { | |
92 | const videoImport = await VideoImportModel.loadAndPopulateVideo(videoImportId) | |
516df59b C |
93 | if (!videoImport || !videoImport.Video) { |
94 | throw new Error('Cannot import video %s: the video import or video linked to this import does not exist anymore.') | |
95 | } | |
fbad87b0 | 96 | |
ce33919c C |
97 | return videoImport |
98 | } | |
99 | ||
100 | type ProcessFileOptions = { | |
2158ac90 | 101 | type: VideoImportYoutubeDLPayloadType | VideoImportTorrentPayloadType |
ce33919c | 102 | videoImportId: number |
ce33919c | 103 | } |
0283eaac | 104 | async function processFile (downloader: () => Promise<string>, videoImport: MVideoImportDefault, options: ProcessFileOptions) { |
fbad87b0 | 105 | let tempVideoPath: string |
516df59b | 106 | let videoFile: VideoFileModel |
6040f87d | 107 | |
fbad87b0 C |
108 | try { |
109 | // Download video from youtubeDL | |
ce33919c | 110 | tempVideoPath = await downloader() |
fbad87b0 C |
111 | |
112 | // Get information about this video | |
62689b94 | 113 | const stats = await stat(tempVideoPath) |
fb719404 | 114 | const isAble = await isAbleToUploadVideo(videoImport.User.id, stats.size) |
a84b8fa5 C |
115 | if (isAble === false) { |
116 | throw new Error('The user video quota is exceeded with this video to import.') | |
117 | } | |
118 | ||
482b2623 C |
119 | const probe = await ffprobePromise(tempVideoPath) |
120 | ||
121 | const { resolution } = await isAudioFile(tempVideoPath, probe) | |
5354af75 C |
122 | ? { resolution: VideoResolution.H_NOVIDEO } |
123 | : await getVideoFileResolution(tempVideoPath) | |
482b2623 C |
124 | |
125 | const fps = await getVideoFileFPS(tempVideoPath, probe) | |
126 | const duration = await getDurationFromVideoFile(tempVideoPath, probe) | |
fbad87b0 | 127 | |
2158ac90 | 128 | // Prepare video file object for creation in database |
ea54cd04 | 129 | const fileExt = getLowercaseExtension(tempVideoPath) |
fbad87b0 | 130 | const videoFileData = { |
90a8bd30 | 131 | extname: fileExt, |
679c12e6 | 132 | resolution, |
3e17515e | 133 | size: stats.size, |
679c12e6 | 134 | filename: generateWebTorrentVideoFilename(resolution, fileExt), |
fbad87b0 C |
135 | fps, |
136 | videoId: videoImport.videoId | |
137 | } | |
516df59b | 138 | videoFile = new VideoFileModel(videoFileData) |
0283eaac | 139 | |
2158ac90 RK |
140 | const hookName = options.type === 'youtube-dl' |
141 | ? 'filter:api.video.post-import-url.accept.result' | |
142 | : 'filter:api.video.post-import-torrent.accept.result' | |
143 | ||
144 | // Check we accept this video | |
145 | const acceptParameters = { | |
146 | videoImport, | |
147 | video: videoImport.Video, | |
148 | videoFilePath: tempVideoPath, | |
149 | videoFile, | |
150 | user: videoImport.User | |
151 | } | |
152 | const acceptedResult = await Hooks.wrapFun(isPostImportVideoAccepted, acceptParameters, hookName) | |
153 | ||
154 | if (acceptedResult.accepted !== true) { | |
155 | logger.info('Refused imported video.', { acceptedResult, acceptParameters }) | |
156 | ||
157 | videoImport.state = VideoImportState.REJECTED | |
158 | await videoImport.save() | |
159 | ||
160 | throw new Error(acceptedResult.errorMessage) | |
161 | } | |
162 | ||
163 | // Video is accepted, resuming preparation | |
d7a25329 | 164 | const videoWithFiles = Object.assign(videoImport.Video, { VideoFiles: [ videoFile ], VideoStreamingPlaylists: [] }) |
58d515e3 | 165 | // To clean files if the import fails |
0283eaac | 166 | const videoImportWithFiles: MVideoImportDefaultFiles = Object.assign(videoImport, { Video: videoWithFiles }) |
fbad87b0 C |
167 | |
168 | // Move file | |
0305db28 | 169 | const videoDestFile = VideoPathManager.Instance.getFSVideoFileOutputPath(videoImportWithFiles.Video, videoFile) |
f481c4f9 | 170 | await move(tempVideoPath, videoDestFile) |
516df59b | 171 | tempVideoPath = null // This path is not used anymore |
fbad87b0 | 172 | |
e3b4c084 | 173 | // Generate miniature if the import did not created it |
453e83ea | 174 | let thumbnailModel: MThumbnail |
44d1f7f2 | 175 | let thumbnailSave: object |
e3b4c084 | 176 | if (!videoImportWithFiles.Video.getMiniature()) { |
a35a2279 C |
177 | thumbnailModel = await generateVideoMiniature({ |
178 | video: videoImportWithFiles.Video, | |
179 | videoFile, | |
180 | type: ThumbnailType.MINIATURE | |
181 | }) | |
44d1f7f2 | 182 | thumbnailSave = thumbnailModel.toJSON() |
fbad87b0 C |
183 | } |
184 | ||
e3b4c084 | 185 | // Generate preview if the import did not created it |
453e83ea | 186 | let previewModel: MThumbnail |
44d1f7f2 | 187 | let previewSave: object |
e3b4c084 | 188 | if (!videoImportWithFiles.Video.getPreview()) { |
a35a2279 C |
189 | previewModel = await generateVideoMiniature({ |
190 | video: videoImportWithFiles.Video, | |
191 | videoFile, | |
192 | type: ThumbnailType.PREVIEW | |
193 | }) | |
44d1f7f2 | 194 | previewSave = previewModel.toJSON() |
fbad87b0 C |
195 | } |
196 | ||
197 | // Create torrent | |
8efc27bf | 198 | await createTorrentAndSetInfoHash(videoImportWithFiles.Video, videoFile) |
fbad87b0 | 199 | |
44d1f7f2 | 200 | const videoFileSave = videoFile.toJSON() |
453e83ea | 201 | |
44d1f7f2 C |
202 | const { videoImportUpdated, video } = await retryTransactionWrapper(() => { |
203 | return sequelizeTypescript.transaction(async t => { | |
204 | const videoImportToUpdate = videoImportWithFiles as MVideoImportVideo | |
516df59b | 205 | |
44d1f7f2 C |
206 | // Refresh video |
207 | const video = await VideoModel.load(videoImportToUpdate.videoId, t) | |
208 | if (!video) throw new Error('Video linked to import ' + videoImportToUpdate.videoId + ' does not exist anymore.') | |
fbad87b0 | 209 | |
44d1f7f2 | 210 | const videoFileCreated = await videoFile.save({ transaction: t }) |
fbad87b0 | 211 | |
44d1f7f2 C |
212 | // Update video DB object |
213 | video.duration = duration | |
0305db28 | 214 | video.state = buildNextVideoState(video.state) |
44d1f7f2 | 215 | await video.save({ transaction: t }) |
e8bafea3 | 216 | |
44d1f7f2 C |
217 | if (thumbnailModel) await video.addAndSaveThumbnail(thumbnailModel, t) |
218 | if (previewModel) await video.addAndSaveThumbnail(previewModel, t) | |
fbad87b0 | 219 | |
44d1f7f2 C |
220 | // Now we can federate the video (reload from database, we need more attributes) |
221 | const videoForFederation = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | |
222 | await federateVideoIfNeeded(videoForFederation, true, t) | |
fbad87b0 | 223 | |
44d1f7f2 C |
224 | // Update video import object |
225 | videoImportToUpdate.state = VideoImportState.SUCCESS | |
226 | const videoImportUpdated = await videoImportToUpdate.save({ transaction: t }) as MVideoImportVideo | |
227 | videoImportUpdated.Video = video | |
fbad87b0 | 228 | |
44d1f7f2 C |
229 | videoImportToUpdate.Video = Object.assign(video, { VideoFiles: [ videoFileCreated ] }) |
230 | ||
231 | logger.info('Video %s imported.', video.uuid) | |
232 | ||
233 | return { videoImportUpdated, video: videoForFederation } | |
234 | }).catch(err => { | |
235 | // Reset fields | |
236 | if (thumbnailModel) thumbnailModel = new ThumbnailModel(thumbnailSave) | |
237 | if (previewModel) previewModel = new ThumbnailModel(previewSave) | |
238 | ||
239 | videoFile = new VideoFileModel(videoFileSave) | |
240 | ||
241 | throw err | |
242 | }) | |
fbad87b0 C |
243 | }) |
244 | ||
d26836cd | 245 | Notifier.Instance.notifyOnFinishedVideoImport({ videoImport: videoImportUpdated, success: true }) |
e8d246d5 | 246 | |
453e83ea | 247 | if (video.isBlacklisted()) { |
8424c402 C |
248 | const videoBlacklist = Object.assign(video.VideoBlacklist, { Video: video }) |
249 | ||
250 | Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist) | |
7ccddd7b | 251 | } else { |
453e83ea | 252 | Notifier.Instance.notifyOnNewVideoIfNeeded(video) |
7ccddd7b JM |
253 | } |
254 | ||
0305db28 JB |
255 | if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { |
256 | return addMoveToObjectStorageJob(videoImportUpdated.Video) | |
257 | } | |
258 | ||
fbad87b0 | 259 | // Create transcoding jobs? |
453e83ea | 260 | if (video.state === VideoState.TO_TRANSCODE) { |
77d7e851 | 261 | await addOptimizeOrMergeAudioJob(videoImportUpdated.Video, videoFile, videoImport.User) |
fbad87b0 C |
262 | } |
263 | ||
264 | } catch (err) { | |
265 | try { | |
e95e0463 | 266 | if (tempVideoPath) await remove(tempVideoPath) |
fbad87b0 | 267 | } catch (errUnlink) { |
516df59b | 268 | logger.warn('Cannot cleanup files after a video import error.', { err: errUnlink }) |
fbad87b0 C |
269 | } |
270 | ||
d7f83948 | 271 | videoImport.error = err.message |
2158ac90 RK |
272 | if (videoImport.state !== VideoImportState.REJECTED) { |
273 | videoImport.state = VideoImportState.FAILED | |
274 | } | |
fbad87b0 C |
275 | await videoImport.save() |
276 | ||
d26836cd | 277 | Notifier.Instance.notifyOnFinishedVideoImport({ videoImport, success: false }) |
dc133480 | 278 | |
fbad87b0 C |
279 | throw err |
280 | } | |
281 | } |