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