+function addVideoImport (req: express.Request, res: express.Response) {
+ if (req.body.targetUrl) return addYoutubeDLImport(req, res)
+
+ const file = req.files?.['torrentfile']?.[0]
+ if (req.body.magnetUri || file) return addTorrentImport(req, res, file)
+}
+
+async function addTorrentImport (req: express.Request, res: express.Response, torrentfile: Express.Multer.File) {
+ const body: VideoImportCreate = req.body
+ const user = res.locals.oauth.token.User
+
+ let videoName: string
+ let torrentName: string
+ let magnetUri: string
+
+ if (torrentfile) {
+ const result = await processTorrentOrAbortRequest(req, res, torrentfile)
+ if (!result) return
+
+ videoName = result.name
+ torrentName = result.torrentName
+ } else {
+ const result = processMagnetURI(body)
+ magnetUri = result.magnetUri
+ videoName = result.name
+ }
+
+ const video = buildVideo(res.locals.videoChannel.id, body, { name: videoName })
+
+ const thumbnailModel = await processThumbnail(req, video)
+ const previewModel = await processPreview(req, video)
+
+ const videoImport = await insertIntoDB({
+ video,
+ thumbnailModel,
+ previewModel,
+ videoChannel: res.locals.videoChannel,
+ tags: body.tags || undefined,
+ user,
+ videoImportAttributes: {
+ magnetUri,
+ torrentName,
+ state: VideoImportState.PENDING,
+ userId: user.id
+ }
+ })
+
+ // Create job to import the video
+ const payload = {
+ type: torrentfile
+ ? 'torrent-file' as 'torrent-file'
+ : 'magnet-uri' as 'magnet-uri',
+ videoImportId: videoImport.id,
+ magnetUri
+ }
+ await JobQueue.Instance.createJobWithPromise({ type: 'video-import', payload })
+
+ auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
+
+ return res.json(videoImport.toFormattedJSON()).end()
+}
+
+async function addYoutubeDLImport (req: express.Request, res: express.Response) {