2021-08-27 14:32:44 +02:00
|
|
|
import express from 'express'
|
2022-08-10 09:53:39 +02:00
|
|
|
import { move, readFile } from 'fs-extra'
|
2021-08-27 14:32:44 +02:00
|
|
|
import { decode } from 'magnet-uri'
|
|
|
|
import parseTorrent, { Instance } from 'parse-torrent'
|
2020-09-17 10:00:46 +02:00
|
|
|
import { join } from 'path'
|
2022-10-31 11:45:08 +01:00
|
|
|
import { buildYoutubeDLImport, buildVideoFromImport, insertFromImportIntoDB, YoutubeDlImportError } from '@server/lib/video-pre-import'
|
2022-08-10 09:53:39 +02:00
|
|
|
import { MThumbnail, MVideoThumbnail } from '@server/types/models'
|
|
|
|
import { HttpStatusCode, ServerErrorCode, ThumbnailType, VideoImportCreate, VideoImportPayload, VideoImportState } from '@shared/models'
|
2020-09-17 10:00:46 +02:00
|
|
|
import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger'
|
|
|
|
import { isArray } from '../../../helpers/custom-validators/misc'
|
2021-05-11 14:56:00 +02:00
|
|
|
import { cleanUpReqFiles, createReqFiles } from '../../../helpers/express-utils'
|
2020-09-17 10:00:46 +02:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { getSecureTorrentName } from '../../../helpers/utils'
|
|
|
|
import { CONFIG } from '../../../initializers/config'
|
|
|
|
import { MIMETYPES } from '../../../initializers/constants'
|
|
|
|
import { JobQueue } from '../../../lib/job-queue/job-queue'
|
2023-06-06 15:59:51 +02:00
|
|
|
import { updateLocalVideoMiniatureFromExisting } from '../../../lib/thumbnail'
|
2022-01-19 14:23:00 +01:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
|
|
|
authenticate,
|
|
|
|
videoImportAddValidator,
|
|
|
|
videoImportCancelValidator,
|
|
|
|
videoImportDeleteValidator
|
|
|
|
} from '../../../middlewares'
|
2018-08-02 15:34:09 +02:00
|
|
|
|
|
|
|
const auditLogger = auditLoggerFactory('video-imports')
|
|
|
|
const videoImportsRouter = express.Router()
|
|
|
|
|
|
|
|
const reqVideoFileImport = createReqFiles(
|
2018-08-07 09:54:36 +02:00
|
|
|
[ 'thumbnailfile', 'previewfile', 'torrentfile' ],
|
2022-03-04 10:57:36 +01:00
|
|
|
{ ...MIMETYPES.TORRENT.MIMETYPE_EXT, ...MIMETYPES.IMAGE.MIMETYPE_EXT }
|
2018-08-02 15:34:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
videoImportsRouter.post('/imports',
|
|
|
|
authenticate,
|
|
|
|
reqVideoFileImport,
|
|
|
|
asyncMiddleware(videoImportAddValidator),
|
2022-08-10 09:53:39 +02:00
|
|
|
asyncRetryTransactionMiddleware(handleVideoImport)
|
2018-08-02 15:34:09 +02:00
|
|
|
)
|
|
|
|
|
2022-01-19 14:23:00 +01:00
|
|
|
videoImportsRouter.post('/imports/:id/cancel',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoImportCancelValidator),
|
|
|
|
asyncRetryTransactionMiddleware(cancelVideoImport)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoImportsRouter.delete('/imports/:id',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoImportDeleteValidator),
|
|
|
|
asyncRetryTransactionMiddleware(deleteVideoImport)
|
|
|
|
)
|
|
|
|
|
2018-08-02 15:34:09 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoImportsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-01-19 14:23:00 +01:00
|
|
|
async function deleteVideoImport (req: express.Request, res: express.Response) {
|
|
|
|
const videoImport = res.locals.videoImport
|
|
|
|
|
|
|
|
await videoImport.destroy()
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cancelVideoImport (req: express.Request, res: express.Response) {
|
|
|
|
const videoImport = res.locals.videoImport
|
|
|
|
|
|
|
|
videoImport.state = VideoImportState.CANCELLED
|
|
|
|
await videoImport.save()
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
|
|
|
|
2022-08-10 09:53:39 +02:00
|
|
|
function handleVideoImport (req: express.Request, res: express.Response) {
|
|
|
|
if (req.body.targetUrl) return handleYoutubeDlImport(req, res)
|
2018-08-06 17:13:39 +02:00
|
|
|
|
2020-06-17 10:55:40 +02:00
|
|
|
const file = req.files?.['torrentfile']?.[0]
|
2022-08-10 09:53:39 +02:00
|
|
|
if (req.body.magnetUri || file) return handleTorrentImport(req, res, file)
|
2018-08-06 17:13:39 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 09:53:39 +02:00
|
|
|
async function handleTorrentImport (req: express.Request, res: express.Response, torrentfile: Express.Multer.File) {
|
2018-08-06 17:13:39 +02:00
|
|
|
const body: VideoImportCreate = req.body
|
2018-08-07 10:07:53 +02:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-08-06 17:13:39 +02:00
|
|
|
|
2018-08-07 09:54:36 +02:00
|
|
|
let videoName: string
|
|
|
|
let torrentName: string
|
|
|
|
let magnetUri: string
|
|
|
|
|
|
|
|
if (torrentfile) {
|
2021-05-12 14:51:17 +02:00
|
|
|
const result = await processTorrentOrAbortRequest(req, res, torrentfile)
|
|
|
|
if (!result) return
|
2018-08-07 09:54:36 +02:00
|
|
|
|
2021-05-12 14:51:17 +02:00
|
|
|
videoName = result.name
|
|
|
|
torrentName = result.torrentName
|
2018-08-07 09:54:36 +02:00
|
|
|
} else {
|
2021-05-12 14:51:17 +02:00
|
|
|
const result = processMagnetURI(body)
|
|
|
|
magnetUri = result.magnetUri
|
|
|
|
videoName = result.name
|
2018-08-07 09:54:36 +02:00
|
|
|
}
|
2018-08-06 17:13:39 +02:00
|
|
|
|
2022-08-10 09:53:39 +02:00
|
|
|
const video = await buildVideoFromImport({
|
|
|
|
channelId: res.locals.videoChannel.id,
|
|
|
|
importData: { name: videoName },
|
|
|
|
importDataOverride: body,
|
|
|
|
importType: 'torrent'
|
|
|
|
})
|
2018-08-06 17:13:39 +02:00
|
|
|
|
2019-04-17 10:07:00 +02:00
|
|
|
const thumbnailModel = await processThumbnail(req, video)
|
|
|
|
const previewModel = await processPreview(req, video)
|
2018-08-06 17:13:39 +02:00
|
|
|
|
2022-08-10 09:53:39 +02:00
|
|
|
const videoImport = await insertFromImportIntoDB({
|
2019-04-17 10:07:00 +02:00
|
|
|
video,
|
|
|
|
thumbnailModel,
|
|
|
|
previewModel,
|
|
|
|
videoChannel: res.locals.videoChannel,
|
2021-05-12 14:51:17 +02:00
|
|
|
tags: body.tags || undefined,
|
|
|
|
user,
|
2023-06-29 09:48:55 +02:00
|
|
|
videoPasswords: body.videoPasswords,
|
2021-05-12 14:51:17 +02:00
|
|
|
videoImportAttributes: {
|
|
|
|
magnetUri,
|
|
|
|
torrentName,
|
|
|
|
state: VideoImportState.PENDING,
|
|
|
|
userId: user.id
|
|
|
|
}
|
2019-04-17 10:07:00 +02:00
|
|
|
})
|
2018-08-06 17:13:39 +02:00
|
|
|
|
2022-08-10 09:53:39 +02:00
|
|
|
const payload: VideoImportPayload = {
|
2021-05-12 14:51:17 +02:00
|
|
|
type: torrentfile
|
2022-08-10 09:53:39 +02:00
|
|
|
? 'torrent-file'
|
|
|
|
: 'magnet-uri',
|
2018-08-06 17:13:39 +02:00
|
|
|
videoImportId: videoImport.id,
|
2022-08-10 09:53:39 +02:00
|
|
|
preventException: false
|
2018-08-06 17:13:39 +02:00
|
|
|
}
|
2022-08-08 15:48:17 +02:00
|
|
|
await JobQueue.Instance.createJob({ type: 'video-import', payload })
|
2018-08-06 17:13:39 +02:00
|
|
|
|
2018-09-19 17:02:16 +02:00
|
|
|
auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
|
2018-08-06 17:13:39 +02:00
|
|
|
|
|
|
|
return res.json(videoImport.toFormattedJSON()).end()
|
|
|
|
}
|
|
|
|
|
2022-08-10 09:53:39 +02:00
|
|
|
function statusFromYtDlImportError (err: YoutubeDlImportError): number {
|
|
|
|
switch (err.code) {
|
|
|
|
case YoutubeDlImportError.CODE.NOT_ONLY_UNICAST_URL:
|
|
|
|
return HttpStatusCode.FORBIDDEN_403
|
|
|
|
|
|
|
|
case YoutubeDlImportError.CODE.FETCH_ERROR:
|
|
|
|
return HttpStatusCode.BAD_REQUEST_400
|
|
|
|
|
|
|
|
default:
|
|
|
|
return HttpStatusCode.INTERNAL_SERVER_ERROR_500
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleYoutubeDlImport (req: express.Request, res: express.Response) {
|
2018-08-02 15:34:09 +02:00
|
|
|
const body: VideoImportCreate = req.body
|
|
|
|
const targetUrl = body.targetUrl
|
2018-08-07 10:07:53 +02:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-08-02 15:34:09 +02:00
|
|
|
|
|
|
|
try {
|
2022-08-10 09:53:39 +02:00
|
|
|
const { job, videoImport } = await buildYoutubeDLImport({
|
|
|
|
targetUrl,
|
|
|
|
channel: res.locals.videoChannel,
|
|
|
|
importDataOverride: body,
|
|
|
|
thumbnailFilePath: req.files?.['thumbnailfile']?.[0].path,
|
|
|
|
previewFilePath: req.files?.['previewfile']?.[0].path,
|
|
|
|
user
|
|
|
|
})
|
|
|
|
await JobQueue.Instance.createJob(job)
|
|
|
|
|
|
|
|
auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
|
|
|
|
|
|
|
|
return res.json(videoImport.toFormattedJSON()).end()
|
2018-08-02 15:34:09 +02:00
|
|
|
} catch (err) {
|
2022-08-10 09:53:39 +02:00
|
|
|
logger.error('An error occurred while importing the video %s. ', targetUrl, { err })
|
2018-08-02 15:34:09 +02:00
|
|
|
|
2021-06-01 01:36:53 +02:00
|
|
|
return res.fail({
|
2022-08-10 09:53:39 +02:00
|
|
|
message: err.message,
|
|
|
|
status: statusFromYtDlImportError(err),
|
2021-06-01 01:36:53 +02:00
|
|
|
data: {
|
|
|
|
targetUrl
|
|
|
|
}
|
|
|
|
})
|
2018-08-02 15:34:09 +02:00
|
|
|
}
|
2018-08-06 17:13:39 +02:00
|
|
|
}
|
|
|
|
|
2021-02-15 14:08:16 +01:00
|
|
|
async function processThumbnail (req: express.Request, video: MVideoThumbnail) {
|
2018-08-03 11:10:31 +02:00
|
|
|
const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined
|
2018-08-02 15:34:09 +02:00
|
|
|
if (thumbnailField) {
|
2020-01-31 16:56:52 +01:00
|
|
|
const thumbnailPhysicalFile = thumbnailField[0]
|
2018-08-06 17:13:39 +02:00
|
|
|
|
2023-06-06 15:59:51 +02:00
|
|
|
return updateLocalVideoMiniatureFromExisting({
|
2020-09-17 10:00:46 +02:00
|
|
|
inputPath: thumbnailPhysicalFile.path,
|
|
|
|
video,
|
|
|
|
type: ThumbnailType.MINIATURE,
|
|
|
|
automaticallyGenerated: false
|
|
|
|
})
|
2018-08-02 15:34:09 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 10:07:00 +02:00
|
|
|
return undefined
|
2018-08-06 17:13:39 +02:00
|
|
|
}
|
|
|
|
|
2021-02-15 14:08:16 +01:00
|
|
|
async function processPreview (req: express.Request, video: MVideoThumbnail): Promise<MThumbnail> {
|
2018-08-03 11:10:31 +02:00
|
|
|
const previewField = req.files ? req.files['previewfile'] : undefined
|
2018-08-02 15:34:09 +02:00
|
|
|
if (previewField) {
|
|
|
|
const previewPhysicalFile = previewField[0]
|
2018-08-06 17:13:39 +02:00
|
|
|
|
2023-06-06 15:59:51 +02:00
|
|
|
return updateLocalVideoMiniatureFromExisting({
|
2020-09-17 10:00:46 +02:00
|
|
|
inputPath: previewPhysicalFile.path,
|
|
|
|
video,
|
|
|
|
type: ThumbnailType.PREVIEW,
|
|
|
|
automaticallyGenerated: false
|
|
|
|
})
|
2018-08-02 15:34:09 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 10:07:00 +02:00
|
|
|
return undefined
|
2018-08-06 17:13:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-12 14:51:17 +02:00
|
|
|
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)
|
2021-08-27 14:32:44 +02:00
|
|
|
const parsedTorrent = parseTorrent(buf) as Instance
|
2021-05-12 14:51:17 +02:00
|
|
|
|
|
|
|
if (parsedTorrent.files.length !== 1) {
|
|
|
|
cleanUpReqFiles(req)
|
|
|
|
|
2021-06-01 01:36:53 +02:00
|
|
|
res.fail({
|
2021-06-01 16:07:58 +02:00
|
|
|
type: ServerErrorCode.INCORRECT_FILES_IN_TORRENT,
|
2021-06-01 01:36:53 +02:00
|
|
|
message: 'Torrents with only 1 file are supported.'
|
|
|
|
})
|
2021-05-12 14:51:17 +02:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: extractNameFromArray(parsedTorrent.name),
|
|
|
|
torrentName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processMagnetURI (body: VideoImportCreate) {
|
|
|
|
const magnetUri = body.magnetUri
|
2021-08-27 14:32:44 +02:00
|
|
|
const parsed = decode(magnetUri)
|
2021-05-12 14:51:17 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
name: extractNameFromArray(parsed.name),
|
|
|
|
magnetUri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractNameFromArray (name: string | string[]) {
|
|
|
|
return isArray(name) ? name[0] : name
|
|
|
|
}
|