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