]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/import.ts
Fix thumbnail processing
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / import.ts
index c16a254d2505a82659abda0f8710a555f5912fc5..398fd5a7f68e9379a0cfb1f6091ed38d2e3dabda 100644 (file)
@@ -1,8 +1,16 @@
-import * as magnetUtil from 'magnet-uri'
 import * as express from 'express'
-import { auditLoggerFactory, VideoImportAuditView } from '../../../helpers/audit-logger'
+import * as magnetUtil from 'magnet-uri'
+import 'multer'
+import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger'
 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares'
-import { CONFIG, IMAGE_MIMETYPE_EXT, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE } from '../../../initializers'
+import {
+  CONFIG,
+  IMAGE_MIMETYPE_EXT,
+  PREVIEWS_SIZE,
+  sequelizeTypescript,
+  THUMBNAILS_SIZE,
+  TORRENT_MIMETYPE_EXT
+} from '../../../initializers'
 import { getYoutubeDLInfo, YoutubeDLInfo } from '../../../helpers/youtube-dl'
 import { createReqFiles } from '../../../helpers/express-utils'
 import { logger } from '../../../helpers/logger'
@@ -18,16 +26,20 @@ import { isArray } from '../../../helpers/custom-validators/misc'
 import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
 import { VideoChannelModel } from '../../../models/video/video-channel'
 import * as Bluebird from 'bluebird'
+import * as parseTorrent from 'parse-torrent'
+import { getSecureTorrentName } from '../../../helpers/utils'
+import { readFile, rename } from 'fs-extra'
 
 const auditLogger = auditLoggerFactory('video-imports')
 const videoImportsRouter = express.Router()
 
 const reqVideoFileImport = createReqFiles(
-  [ 'thumbnailfile', 'previewfile' ],
-  IMAGE_MIMETYPE_EXT,
+  [ 'thumbnailfile', 'previewfile', 'torrentfile' ],
+  Object.assign({}, TORRENT_MIMETYPE_EXT, IMAGE_MIMETYPE_EXT),
   {
     thumbnailfile: CONFIG.STORAGE.THUMBNAILS_DIR,
-    previewfile: CONFIG.STORAGE.PREVIEWS_DIR
+    previewfile: CONFIG.STORAGE.PREVIEWS_DIR,
+    torrentfile: CONFIG.STORAGE.TORRENTS_DIR
   }
 )
 
@@ -49,37 +61,60 @@ export {
 function addVideoImport (req: express.Request, res: express.Response) {
   if (req.body.targetUrl) return addYoutubeDLImport(req, res)
 
-  if (req.body.magnetUri) return addTorrentImport(req, res)
+  const file = req.files && req.files['torrentfile'] ? req.files['torrentfile'][0] : undefined
+  if (req.body.magnetUri || file) return addTorrentImport(req, res, file)
 }
 
-async function addTorrentImport (req: express.Request, res: express.Response) {
+async function addTorrentImport (req: express.Request, res: express.Response, torrentfile: Express.Multer.File) {
   const body: VideoImportCreate = req.body
-  const magnetUri = body.magnetUri
+  const user = res.locals.oauth.token.User
+
+  let videoName: string
+  let torrentName: string
+  let magnetUri: string
 
-  const parsed = magnetUtil.decode(magnetUri)
-  const magnetName = isArray(parsed.name) ? parsed.name[0] : parsed.name as string
+  if (torrentfile) {
+    torrentName = torrentfile.originalname
+
+    // Rename the torrent to a secured name
+    const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, getSecureTorrentName(torrentName))
+    await rename(torrentfile.path, newTorrentPath)
+    torrentfile.path = newTorrentPath
+
+    const buf = await readFile(torrentfile.path)
+    const parsedTorrent = parseTorrent(buf)
+
+    videoName = isArray(parsedTorrent.name) ? parsedTorrent.name[ 0 ] : parsedTorrent.name as string
+  } else {
+    magnetUri = body.magnetUri
+
+    const parsed = magnetUtil.decode(magnetUri)
+    videoName = isArray(parsed.name) ? parsed.name[ 0 ] : parsed.name as string
+  }
 
-  const video = buildVideo(res.locals.videoChannel.id, body, { name: magnetName })
+  const video = buildVideo(res.locals.videoChannel.id, body, { name: videoName })
 
   await processThumbnail(req, video)
   await processPreview(req, video)
 
-  const tags = null
+  const tags = body.tags || undefined
   const videoImportAttributes = {
     magnetUri,
-    state: VideoImportState.PENDING
+    torrentName,
+    state: VideoImportState.PENDING,
+    userId: user.id
   }
   const videoImport: VideoImportModel = await insertIntoDB(video, res.locals.videoChannel, tags, videoImportAttributes)
 
   // Create job to import the video
   const payload = {
-    type: 'magnet-uri' as 'magnet-uri',
+    type: torrentfile ? 'torrent-file' as 'torrent-file' : 'magnet-uri' as 'magnet-uri',
     videoImportId: videoImport.id,
     magnetUri
   }
   await JobQueue.Instance.createJob({ type: 'video-import', payload })
 
-  auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoImportAuditView(videoImport.toFormattedJSON()))
+  auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
 
   return res.json(videoImport.toFormattedJSON()).end()
 }
@@ -87,6 +122,7 @@ async function addTorrentImport (req: express.Request, res: express.Response) {
 async function addYoutubeDLImport (req: express.Request, res: express.Response) {
   const body: VideoImportCreate = req.body
   const targetUrl = body.targetUrl
+  const user = res.locals.oauth.token.User
 
   let youtubeDLInfo: YoutubeDLInfo
   try {
@@ -107,7 +143,8 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
   const tags = body.tags || youtubeDLInfo.tags
   const videoImportAttributes = {
     targetUrl,
-    state: VideoImportState.PENDING
+    state: VideoImportState.PENDING,
+    userId: user.id
   }
   const videoImport: VideoImportModel = await insertIntoDB(video, res.locals.videoChannel, tags, videoImportAttributes)
 
@@ -121,7 +158,7 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
   }
   await JobQueue.Instance.createJob({ type: 'video-import', payload })
 
-  auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoImportAuditView(videoImport.toFormattedJSON()))
+  auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
 
   return res.json(videoImport.toFormattedJSON()).end()
 }
@@ -187,11 +224,13 @@ function insertIntoDB (
     videoCreated.VideoChannel = videoChannel
 
     // Set tags to the video
-    if (tags !== undefined) {
+    if (tags) {
       const tagInstances = await TagModel.findOrCreateTags(tags, t)
 
       await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
       videoCreated.Tags = tagInstances
+    } else {
+      videoCreated.Tags = []
     }
 
     // Create video import object in database