]>
Commit | Line | Data |
---|---|---|
fbad87b0 | 1 | import * as express from 'express' |
990b6a0b C |
2 | import * as magnetUtil from 'magnet-uri' |
3 | import 'multer' | |
993cef4b | 4 | import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger' |
7e5f9f00 | 5 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares' |
14e2014a | 6 | import { CONFIG, MIMETYPES, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE } from '../../../initializers' |
fbad87b0 C |
7 | import { getYoutubeDLInfo, YoutubeDLInfo } from '../../../helpers/youtube-dl' |
8 | import { createReqFiles } from '../../../helpers/express-utils' | |
9 | import { logger } from '../../../helpers/logger' | |
10 | import { VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '../../../../shared' | |
11 | import { VideoModel } from '../../../models/video/video' | |
12 | import { getVideoActivityPubUrl } from '../../../lib/activitypub' | |
13 | import { TagModel } from '../../../models/video/tag' | |
14 | import { VideoImportModel } from '../../../models/video/video-import' | |
15 | import { JobQueue } from '../../../lib/job-queue/job-queue' | |
16 | import { processImage } from '../../../helpers/image-utils' | |
17 | import { join } from 'path' | |
ce33919c C |
18 | import { isArray } from '../../../helpers/custom-validators/misc' |
19 | import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model' | |
20 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
7ccddd7b | 21 | import { UserModel } from '../../../models/account/user' |
ce33919c | 22 | import * as Bluebird from 'bluebird' |
990b6a0b | 23 | import * as parseTorrent from 'parse-torrent' |
990b6a0b | 24 | import { getSecureTorrentName } from '../../../helpers/utils' |
f481c4f9 | 25 | import { readFile, move } from 'fs-extra' |
7ccddd7b | 26 | import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist' |
fbad87b0 C |
27 | |
28 | const auditLogger = auditLoggerFactory('video-imports') | |
29 | const videoImportsRouter = express.Router() | |
30 | ||
31 | const reqVideoFileImport = createReqFiles( | |
990b6a0b | 32 | [ 'thumbnailfile', 'previewfile', 'torrentfile' ], |
14e2014a | 33 | Object.assign({}, MIMETYPES.TORRENT.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT), |
fbad87b0 | 34 | { |
6040f87d C |
35 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, |
36 | previewfile: CONFIG.STORAGE.TMP_DIR, | |
37 | torrentfile: CONFIG.STORAGE.TMP_DIR | |
fbad87b0 C |
38 | } |
39 | ) | |
40 | ||
41 | videoImportsRouter.post('/imports', | |
42 | authenticate, | |
43 | reqVideoFileImport, | |
44 | asyncMiddleware(videoImportAddValidator), | |
45 | asyncRetryTransactionMiddleware(addVideoImport) | |
46 | ) | |
47 | ||
fbad87b0 C |
48 | // --------------------------------------------------------------------------- |
49 | ||
50 | export { | |
51 | videoImportsRouter | |
52 | } | |
53 | ||
54 | // --------------------------------------------------------------------------- | |
55 | ||
ce33919c C |
56 | function addVideoImport (req: express.Request, res: express.Response) { |
57 | if (req.body.targetUrl) return addYoutubeDLImport(req, res) | |
58 | ||
a84b8fa5 | 59 | const file = req.files && req.files['torrentfile'] ? req.files['torrentfile'][0] : undefined |
990b6a0b | 60 | if (req.body.magnetUri || file) return addTorrentImport(req, res, file) |
ce33919c C |
61 | } |
62 | ||
990b6a0b | 63 | async function addTorrentImport (req: express.Request, res: express.Response, torrentfile: Express.Multer.File) { |
ce33919c | 64 | const body: VideoImportCreate = req.body |
a84b8fa5 | 65 | const user = res.locals.oauth.token.User |
ce33919c | 66 | |
990b6a0b C |
67 | let videoName: string |
68 | let torrentName: string | |
69 | let magnetUri: string | |
70 | ||
71 | if (torrentfile) { | |
72 | torrentName = torrentfile.originalname | |
73 | ||
74 | // Rename the torrent to a secured name | |
75 | const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, getSecureTorrentName(torrentName)) | |
f481c4f9 | 76 | await move(torrentfile.path, newTorrentPath) |
990b6a0b C |
77 | torrentfile.path = newTorrentPath |
78 | ||
62689b94 | 79 | const buf = await readFile(torrentfile.path) |
990b6a0b C |
80 | const parsedTorrent = parseTorrent(buf) |
81 | ||
82 | videoName = isArray(parsedTorrent.name) ? parsedTorrent.name[ 0 ] : parsedTorrent.name as string | |
83 | } else { | |
84 | magnetUri = body.magnetUri | |
85 | ||
86 | const parsed = magnetUtil.decode(magnetUri) | |
87 | videoName = isArray(parsed.name) ? parsed.name[ 0 ] : parsed.name as string | |
88 | } | |
ce33919c | 89 | |
7ccddd7b | 90 | const video = buildVideo(res.locals.videoChannel.id, body, { name: videoName }, user) |
ce33919c C |
91 | |
92 | await processThumbnail(req, video) | |
93 | await processPreview(req, video) | |
94 | ||
3e17515e | 95 | const tags = body.tags || undefined |
ce33919c C |
96 | const videoImportAttributes = { |
97 | magnetUri, | |
990b6a0b | 98 | torrentName, |
a84b8fa5 C |
99 | state: VideoImportState.PENDING, |
100 | userId: user.id | |
ce33919c | 101 | } |
dae86118 | 102 | const videoImport = await insertIntoDB(video, res.locals.videoChannel, tags, videoImportAttributes) |
ce33919c C |
103 | |
104 | // Create job to import the video | |
105 | const payload = { | |
990b6a0b | 106 | type: torrentfile ? 'torrent-file' as 'torrent-file' : 'magnet-uri' as 'magnet-uri', |
ce33919c C |
107 | videoImportId: videoImport.id, |
108 | magnetUri | |
109 | } | |
110 | await JobQueue.Instance.createJob({ type: 'video-import', payload }) | |
111 | ||
993cef4b | 112 | auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON())) |
ce33919c C |
113 | |
114 | return res.json(videoImport.toFormattedJSON()).end() | |
115 | } | |
116 | ||
117 | async function addYoutubeDLImport (req: express.Request, res: express.Response) { | |
fbad87b0 C |
118 | const body: VideoImportCreate = req.body |
119 | const targetUrl = body.targetUrl | |
a84b8fa5 | 120 | const user = res.locals.oauth.token.User |
fbad87b0 C |
121 | |
122 | let youtubeDLInfo: YoutubeDLInfo | |
123 | try { | |
124 | youtubeDLInfo = await getYoutubeDLInfo(targetUrl) | |
125 | } catch (err) { | |
126 | logger.info('Cannot fetch information from import for URL %s.', targetUrl, { err }) | |
127 | ||
128 | return res.status(400).json({ | |
129 | error: 'Cannot fetch remote information of this URL.' | |
130 | }).end() | |
131 | } | |
132 | ||
7ccddd7b | 133 | const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo, user) |
ce33919c C |
134 | |
135 | const downloadThumbnail = !await processThumbnail(req, video) | |
136 | const downloadPreview = !await processPreview(req, video) | |
137 | ||
138 | const tags = body.tags || youtubeDLInfo.tags | |
139 | const videoImportAttributes = { | |
140 | targetUrl, | |
a84b8fa5 C |
141 | state: VideoImportState.PENDING, |
142 | userId: user.id | |
ce33919c | 143 | } |
dae86118 | 144 | const videoImport = await insertIntoDB(video, res.locals.videoChannel, tags, videoImportAttributes) |
ce33919c C |
145 | |
146 | // Create job to import the video | |
147 | const payload = { | |
148 | type: 'youtube-dl' as 'youtube-dl', | |
149 | videoImportId: videoImport.id, | |
150 | thumbnailUrl: youtubeDLInfo.thumbnailUrl, | |
151 | downloadThumbnail, | |
152 | downloadPreview | |
153 | } | |
154 | await JobQueue.Instance.createJob({ type: 'video-import', payload }) | |
155 | ||
993cef4b | 156 | auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON())) |
ce33919c C |
157 | |
158 | return res.json(videoImport.toFormattedJSON()).end() | |
159 | } | |
160 | ||
7ccddd7b | 161 | function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo, user: UserModel) { |
fbad87b0 | 162 | const videoData = { |
ce33919c | 163 | name: body.name || importData.name || 'Unknown name', |
fbad87b0 | 164 | remote: false, |
ce33919c C |
165 | category: body.category || importData.category, |
166 | licence: body.licence || importData.licence, | |
590fb506 | 167 | language: body.language || undefined, |
fbad87b0 | 168 | commentsEnabled: body.commentsEnabled || true, |
7f2cfe3a | 169 | downloadEnabled: body.downloadEnabled || true, |
fbad87b0 C |
170 | waitTranscoding: body.waitTranscoding || false, |
171 | state: VideoState.TO_IMPORT, | |
ce33919c C |
172 | nsfw: body.nsfw || importData.nsfw || false, |
173 | description: body.description || importData.description, | |
fbad87b0 C |
174 | support: body.support || null, |
175 | privacy: body.privacy || VideoPrivacy.PRIVATE, | |
176 | duration: 0, // duration will be set by the import job | |
4e553a41 AM |
177 | channelId: channelId, |
178 | originallyPublishedAt: importData.originallyPublishedAt | |
fbad87b0 C |
179 | } |
180 | const video = new VideoModel(videoData) | |
181 | video.url = getVideoActivityPubUrl(video) | |
182 | ||
ce33919c C |
183 | return video |
184 | } | |
185 | ||
186 | async function processThumbnail (req: express.Request, video: VideoModel) { | |
5d08a6a7 | 187 | const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined |
fbad87b0 C |
188 | if (thumbnailField) { |
189 | const thumbnailPhysicalFile = thumbnailField[ 0 ] | |
190 | await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE) | |
ce33919c C |
191 | |
192 | return true | |
fbad87b0 C |
193 | } |
194 | ||
ce33919c C |
195 | return false |
196 | } | |
197 | ||
198 | async function processPreview (req: express.Request, video: VideoModel) { | |
5d08a6a7 | 199 | const previewField = req.files ? req.files['previewfile'] : undefined |
fbad87b0 C |
200 | if (previewField) { |
201 | const previewPhysicalFile = previewField[0] | |
202 | await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE) | |
ce33919c C |
203 | |
204 | return true | |
fbad87b0 C |
205 | } |
206 | ||
ce33919c C |
207 | return false |
208 | } | |
209 | ||
210 | function insertIntoDB ( | |
211 | video: VideoModel, | |
212 | videoChannel: VideoChannelModel, | |
213 | tags: string[], | |
214 | videoImportAttributes: FilteredModelAttributes<VideoImportModel> | |
215 | ): Bluebird<VideoImportModel> { | |
216 | return sequelizeTypescript.transaction(async t => { | |
fbad87b0 C |
217 | const sequelizeOptions = { transaction: t } |
218 | ||
219 | // Save video object in database | |
220 | const videoCreated = await video.save(sequelizeOptions) | |
ce33919c | 221 | videoCreated.VideoChannel = videoChannel |
fbad87b0 | 222 | |
7ccddd7b JM |
223 | await autoBlacklistVideoIfNeeded(video, videoChannel.Account.User, t) |
224 | ||
fbad87b0 | 225 | // Set tags to the video |
3e17515e | 226 | if (tags) { |
590fb506 | 227 | const tagInstances = await TagModel.findOrCreateTags(tags, t) |
fbad87b0 C |
228 | |
229 | await videoCreated.$set('Tags', tagInstances, sequelizeOptions) | |
230 | videoCreated.Tags = tagInstances | |
3e17515e C |
231 | } else { |
232 | videoCreated.Tags = [] | |
fbad87b0 C |
233 | } |
234 | ||
235 | // Create video import object in database | |
ce33919c C |
236 | const videoImport = await VideoImportModel.create( |
237 | Object.assign({ videoId: videoCreated.id }, videoImportAttributes), | |
238 | sequelizeOptions | |
239 | ) | |
fbad87b0 C |
240 | videoImport.Video = videoCreated |
241 | ||
242 | return videoImport | |
243 | }) | |
fbad87b0 | 244 | } |