import * as express from 'express'
import * as magnetUtil from 'magnet-uri'
import 'multer'
-import { auditLoggerFactory, VideoImportAuditView } from '../../../helpers/audit-logger'
+import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger'
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares'
import {
CONFIG,
import { VideoChannelModel } from '../../../models/video/video-channel'
import * as Bluebird from 'bluebird'
import * as parseTorrent from 'parse-torrent'
-import { readFileBufferPromise, renamePromise } from '../../../helpers/core-utils'
import { getSecureTorrentName } from '../../../helpers/utils'
+import { readFile, rename } from 'fs-extra'
const auditLogger = auditLoggerFactory('video-imports')
const videoImportsRouter = express.Router()
function addVideoImport (req: express.Request, res: express.Response) {
if (req.body.targetUrl) return addYoutubeDLImport(req, res)
- const file = req.files['torrentfile'][0]
+ 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, torrentfile: Express.Multer.File) {
const body: VideoImportCreate = req.body
+ const user = res.locals.oauth.token.User
let videoName: string
let torrentName: string
// Rename the torrent to a secured name
const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, getSecureTorrentName(torrentName))
- await renamePromise(torrentfile.path, newTorrentPath)
+ await rename(torrentfile.path, newTorrentPath)
torrentfile.path = newTorrentPath
- const buf = await readFileBufferPromise(torrentfile.path)
+ const buf = await readFile(torrentfile.path)
const parsedTorrent = parseTorrent(buf)
videoName = isArray(parsedTorrent.name) ? parsedTorrent.name[ 0 ] : parsedTorrent.name as string
await processThumbnail(req, video)
await processPreview(req, video)
- const tags = null
+ const tags = body.tags || undefined
const videoImportAttributes = {
magnetUri,
torrentName,
- state: VideoImportState.PENDING
+ state: VideoImportState.PENDING,
+ userId: user.id
}
const videoImport: VideoImportModel = await insertIntoDB(video, res.locals.videoChannel, tags, videoImportAttributes)
}
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()
}
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 {
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)
}
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()
}
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