+
+ return videoImport
+}
+
+async function processTorrentOrAbortRequest (req: express.Request, res: express.Response, torrentfile: Express.Multer.File) {
+ const torrentName = torrentfile.originalname
+
+ // Rename the torrent to a secured name
+ const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, getSecureTorrentName(torrentName))
+ await move(torrentfile.path, newTorrentPath, { overwrite: true })
+ torrentfile.path = newTorrentPath
+
+ const buf = await readFile(torrentfile.path)
+ const parsedTorrent = parseTorrent(buf) as Instance
+
+ if (parsedTorrent.files.length !== 1) {
+ cleanUpReqFiles(req)
+
+ res.fail({
+ type: ServerErrorCode.INCORRECT_FILES_IN_TORRENT,
+ message: 'Torrents with only 1 file are supported.'
+ })
+ return undefined
+ }
+
+ return {
+ name: extractNameFromArray(parsedTorrent.name),
+ torrentName
+ }
+}
+
+function processMagnetURI (body: VideoImportCreate) {
+ const magnetUri = body.magnetUri
+ const parsed = decode(magnetUri)
+
+ return {
+ name: extractNameFromArray(parsed.name),
+ magnetUri
+ }
+}
+
+function extractNameFromArray (name: string | string[]) {
+ return isArray(name) ? name[0] : name
+}
+
+async function processYoutubeSubtitles (youtubeDL: YoutubeDLWrapper, targetUrl: string, videoId: number) {
+ try {
+ const subtitles = await youtubeDL.getSubtitles()
+
+ logger.info('Will create %s subtitles from youtube import %s.', subtitles.length, targetUrl)
+
+ for (const subtitle of subtitles) {
+ if (!await isVTTFileValid(subtitle.path)) {
+ await remove(subtitle.path)
+ continue
+ }
+
+ const videoCaption = new VideoCaptionModel({
+ videoId,
+ language: subtitle.language,
+ filename: VideoCaptionModel.generateCaptionName(subtitle.language)
+ }) as MVideoCaption
+
+ // Move physical file
+ await moveAndProcessCaptionFile(subtitle, videoCaption)
+
+ await sequelizeTypescript.transaction(async t => {
+ await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
+ })
+ }
+ } catch (err) {
+ logger.warn('Cannot get video subtitles.', { err })
+ }
+}
+
+async function hasUnicastURLsOnly (youtubeDLInfo: YoutubeDLInfo) {
+ const hosts = youtubeDLInfo.urls.map(u => new URL(u).hostname)
+ const uniqHosts = new Set(hosts)
+
+ for (const h of uniqHosts) {
+ if (await isResolvingToUnicastOnly(h) !== true) {
+ return false
+ }
+ }
+
+ return true