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