]>
Commit | Line | Data |
---|---|---|
fbad87b0 | 1 | import * as Bull from 'bull' |
2158ac90 RK |
2 | import { move, remove, stat } from 'fs-extra' |
3 | import { extname } from 'path' | |
44d1f7f2 | 4 | import { retryTransactionWrapper } from '@server/helpers/database-utils' |
2158ac90 RK |
5 | import { isPostImportVideoAccepted } from '@server/lib/moderation' |
6 | import { Hooks } from '@server/lib/plugins/hooks' | |
fb719404 | 7 | import { isAbleToUploadVideo } from '@server/lib/user' |
77d7e851 | 8 | import { addOptimizeOrMergeAudioJob } from '@server/lib/video' |
2158ac90 | 9 | import { getVideoFilePath } from '@server/lib/video-paths' |
44d1f7f2 | 10 | import { ThumbnailModel } from '@server/models/video/thumbnail' |
26d6bf65 | 11 | import { MVideoImportDefault, MVideoImportDefaultFiles, MVideoImportVideo } from '@server/types/models/video/video-import' |
2158ac90 RK |
12 | import { |
13 | VideoImportPayload, | |
14 | VideoImportTorrentPayload, | |
15 | VideoImportTorrentPayloadType, | |
16 | VideoImportYoutubeDLPayload, | |
17 | VideoImportYoutubeDLPayloadType, | |
18 | VideoState | |
19 | } from '../../../../shared' | |
fbad87b0 | 20 | import { VideoImportState } from '../../../../shared/models/videos' |
2158ac90 | 21 | import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' |
daf6e480 | 22 | import { getDurationFromVideoFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' |
2158ac90 | 23 | import { logger } from '../../../helpers/logger' |
990b6a0b | 24 | import { getSecureTorrentName } from '../../../helpers/utils' |
2158ac90 RK |
25 | import { createTorrentAndSetInfoHash, downloadWebTorrentVideo } from '../../../helpers/webtorrent' |
26 | import { downloadYoutubeDLVideo } from '../../../helpers/youtube-dl' | |
6dd9de95 | 27 | import { CONFIG } from '../../../initializers/config' |
2158ac90 | 28 | import { VIDEO_IMPORT_TIMEOUT } from '../../../initializers/constants' |
74dc3bca | 29 | import { sequelizeTypescript } from '../../../initializers/database' |
2158ac90 RK |
30 | import { VideoModel } from '../../../models/video/video' |
31 | import { VideoFileModel } from '../../../models/video/video-file' | |
32 | import { VideoImportModel } from '../../../models/video/video-import' | |
26d6bf65 | 33 | import { MThumbnail } from '../../../types/models/video/thumbnail' |
2158ac90 RK |
34 | import { federateVideoIfNeeded } from '../../activitypub/videos' |
35 | import { Notifier } from '../../notifier' | |
36 | import { generateVideoMiniature } from '../../thumbnail' | |
ce33919c | 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 | 58 | const options = { |
2158ac90 | 59 | type: payload.type, |
ce33919c C |
60 | videoImportId: payload.videoImportId, |
61 | ||
ce33919c C |
62 | generateThumbnail: true, |
63 | generatePreview: true | |
64 | } | |
990b6a0b C |
65 | const target = { |
66 | torrentName: videoImport.torrentName ? getSecureTorrentName(videoImport.torrentName) : undefined, | |
67 | magnetUri: videoImport.magnetUri | |
68 | } | |
cf9166cf | 69 | return processFile(() => downloadWebTorrentVideo(target, VIDEO_IMPORT_TIMEOUT), videoImport, options) |
ce33919c C |
70 | } |
71 | ||
72 | async function processYoutubeDLImport (job: Bull.Job, payload: VideoImportYoutubeDLPayload) { | |
73 | logger.info('Processing youtubeDL video import in job %d.', job.id) | |
74 | ||
75 | const videoImport = await getVideoImportOrDie(payload.videoImportId) | |
76 | const options = { | |
2158ac90 | 77 | type: payload.type, |
ce33919c C |
78 | videoImportId: videoImport.id, |
79 | ||
b1770a0a K |
80 | generateThumbnail: payload.generateThumbnail, |
81 | generatePreview: payload.generatePreview | |
ce33919c C |
82 | } |
83 | ||
454c20fa | 84 | return processFile( |
805b8619 | 85 | () => downloadYoutubeDLVideo(videoImport.targetUrl, 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 C |
102 | videoImportId: number |
103 | ||
ce33919c C |
104 | generateThumbnail: boolean |
105 | generatePreview: boolean | |
106 | } | |
0283eaac | 107 | async function processFile (downloader: () => Promise<string>, videoImport: MVideoImportDefault, options: ProcessFileOptions) { |
fbad87b0 | 108 | let tempVideoPath: string |
516df59b C |
109 | let videoDestFile: string |
110 | let videoFile: VideoFileModel | |
6040f87d | 111 | |
fbad87b0 C |
112 | try { |
113 | // Download video from youtubeDL | |
ce33919c | 114 | tempVideoPath = await downloader() |
fbad87b0 C |
115 | |
116 | // Get information about this video | |
62689b94 | 117 | const stats = await stat(tempVideoPath) |
fb719404 | 118 | const isAble = await isAbleToUploadVideo(videoImport.User.id, stats.size) |
a84b8fa5 C |
119 | if (isAble === false) { |
120 | throw new Error('The user video quota is exceeded with this video to import.') | |
121 | } | |
122 | ||
fbad87b0 | 123 | const { videoFileResolution } = await getVideoFileResolution(tempVideoPath) |
d7f83948 | 124 | const fps = await getVideoFileFPS(tempVideoPath) |
fbad87b0 C |
125 | const duration = await getDurationFromVideoFile(tempVideoPath) |
126 | ||
2158ac90 | 127 | // Prepare video file object for creation in database |
fbad87b0 C |
128 | const videoFileData = { |
129 | extname: extname(tempVideoPath), | |
130 | resolution: videoFileResolution, | |
3e17515e | 131 | size: stats.size, |
fbad87b0 C |
132 | fps, |
133 | videoId: videoImport.videoId | |
134 | } | |
516df59b | 135 | videoFile = new VideoFileModel(videoFileData) |
0283eaac | 136 | |
2158ac90 RK |
137 | const hookName = options.type === 'youtube-dl' |
138 | ? 'filter:api.video.post-import-url.accept.result' | |
139 | : 'filter:api.video.post-import-torrent.accept.result' | |
140 | ||
141 | // Check we accept this video | |
142 | const acceptParameters = { | |
143 | videoImport, | |
144 | video: videoImport.Video, | |
145 | videoFilePath: tempVideoPath, | |
146 | videoFile, | |
147 | user: videoImport.User | |
148 | } | |
149 | const acceptedResult = await Hooks.wrapFun(isPostImportVideoAccepted, acceptParameters, hookName) | |
150 | ||
151 | if (acceptedResult.accepted !== true) { | |
152 | logger.info('Refused imported video.', { acceptedResult, acceptParameters }) | |
153 | ||
154 | videoImport.state = VideoImportState.REJECTED | |
155 | await videoImport.save() | |
156 | ||
157 | throw new Error(acceptedResult.errorMessage) | |
158 | } | |
159 | ||
160 | // Video is accepted, resuming preparation | |
d7a25329 | 161 | const videoWithFiles = Object.assign(videoImport.Video, { VideoFiles: [ videoFile ], VideoStreamingPlaylists: [] }) |
58d515e3 | 162 | // To clean files if the import fails |
0283eaac | 163 | const videoImportWithFiles: MVideoImportDefaultFiles = Object.assign(videoImport, { Video: videoWithFiles }) |
fbad87b0 C |
164 | |
165 | // Move file | |
d7a25329 | 166 | videoDestFile = getVideoFilePath(videoImportWithFiles.Video, videoFile) |
f481c4f9 | 167 | await move(tempVideoPath, videoDestFile) |
516df59b | 168 | tempVideoPath = null // This path is not used anymore |
fbad87b0 C |
169 | |
170 | // Process thumbnail | |
453e83ea | 171 | let thumbnailModel: MThumbnail |
44d1f7f2 | 172 | let thumbnailSave: object |
b1770a0a | 173 | if (options.generateThumbnail) { |
0283eaac | 174 | thumbnailModel = await generateVideoMiniature(videoImportWithFiles.Video, videoFile, ThumbnailType.MINIATURE) |
44d1f7f2 | 175 | thumbnailSave = thumbnailModel.toJSON() |
fbad87b0 C |
176 | } |
177 | ||
178 | // Process preview | |
453e83ea | 179 | let previewModel: MThumbnail |
44d1f7f2 | 180 | let previewSave: object |
b1770a0a | 181 | if (options.generatePreview) { |
0283eaac | 182 | previewModel = await generateVideoMiniature(videoImportWithFiles.Video, videoFile, ThumbnailType.PREVIEW) |
44d1f7f2 | 183 | previewSave = previewModel.toJSON() |
fbad87b0 C |
184 | } |
185 | ||
186 | // Create torrent | |
d7a25329 | 187 | await createTorrentAndSetInfoHash(videoImportWithFiles.Video, videoFile) |
fbad87b0 | 188 | |
44d1f7f2 | 189 | const videoFileSave = videoFile.toJSON() |
453e83ea | 190 | |
44d1f7f2 C |
191 | const { videoImportUpdated, video } = await retryTransactionWrapper(() => { |
192 | return sequelizeTypescript.transaction(async t => { | |
193 | const videoImportToUpdate = videoImportWithFiles as MVideoImportVideo | |
516df59b | 194 | |
44d1f7f2 C |
195 | // Refresh video |
196 | const video = await VideoModel.load(videoImportToUpdate.videoId, t) | |
197 | if (!video) throw new Error('Video linked to import ' + videoImportToUpdate.videoId + ' does not exist anymore.') | |
fbad87b0 | 198 | |
44d1f7f2 | 199 | const videoFileCreated = await videoFile.save({ transaction: t }) |
fbad87b0 | 200 | |
44d1f7f2 C |
201 | // Update video DB object |
202 | video.duration = duration | |
203 | video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED | |
204 | await video.save({ transaction: t }) | |
e8bafea3 | 205 | |
44d1f7f2 C |
206 | if (thumbnailModel) await video.addAndSaveThumbnail(thumbnailModel, t) |
207 | if (previewModel) await video.addAndSaveThumbnail(previewModel, t) | |
fbad87b0 | 208 | |
44d1f7f2 C |
209 | // Now we can federate the video (reload from database, we need more attributes) |
210 | const videoForFederation = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | |
211 | await federateVideoIfNeeded(videoForFederation, true, t) | |
fbad87b0 | 212 | |
44d1f7f2 C |
213 | // Update video import object |
214 | videoImportToUpdate.state = VideoImportState.SUCCESS | |
215 | const videoImportUpdated = await videoImportToUpdate.save({ transaction: t }) as MVideoImportVideo | |
216 | videoImportUpdated.Video = video | |
fbad87b0 | 217 | |
44d1f7f2 C |
218 | videoImportToUpdate.Video = Object.assign(video, { VideoFiles: [ videoFileCreated ] }) |
219 | ||
220 | logger.info('Video %s imported.', video.uuid) | |
221 | ||
222 | return { videoImportUpdated, video: videoForFederation } | |
223 | }).catch(err => { | |
224 | // Reset fields | |
225 | if (thumbnailModel) thumbnailModel = new ThumbnailModel(thumbnailSave) | |
226 | if (previewModel) previewModel = new ThumbnailModel(previewSave) | |
227 | ||
228 | videoFile = new VideoFileModel(videoFileSave) | |
229 | ||
230 | throw err | |
231 | }) | |
fbad87b0 C |
232 | }) |
233 | ||
dc133480 | 234 | Notifier.Instance.notifyOnFinishedVideoImport(videoImportUpdated, true) |
e8d246d5 | 235 | |
453e83ea | 236 | if (video.isBlacklisted()) { |
8424c402 C |
237 | const videoBlacklist = Object.assign(video.VideoBlacklist, { Video: video }) |
238 | ||
239 | Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist) | |
7ccddd7b | 240 | } else { |
453e83ea | 241 | Notifier.Instance.notifyOnNewVideoIfNeeded(video) |
7ccddd7b JM |
242 | } |
243 | ||
fbad87b0 | 244 | // Create transcoding jobs? |
453e83ea | 245 | if (video.state === VideoState.TO_TRANSCODE) { |
77d7e851 | 246 | await addOptimizeOrMergeAudioJob(videoImportUpdated.Video, videoFile, videoImport.User) |
fbad87b0 C |
247 | } |
248 | ||
249 | } catch (err) { | |
250 | try { | |
e95e0463 | 251 | if (tempVideoPath) await remove(tempVideoPath) |
fbad87b0 | 252 | } catch (errUnlink) { |
516df59b | 253 | logger.warn('Cannot cleanup files after a video import error.', { err: errUnlink }) |
fbad87b0 C |
254 | } |
255 | ||
d7f83948 | 256 | videoImport.error = err.message |
2158ac90 RK |
257 | if (videoImport.state !== VideoImportState.REJECTED) { |
258 | videoImport.state = VideoImportState.FAILED | |
259 | } | |
fbad87b0 C |
260 | await videoImport.save() |
261 | ||
dc133480 C |
262 | Notifier.Instance.notifyOnFinishedVideoImport(videoImport, false) |
263 | ||
fbad87b0 C |
264 | throw err |
265 | } | |
266 | } |