import 'multer'
import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger'
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares'
-import { MIMETYPES, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../../../initializers/constants'
+import { MIMETYPES } from '../../../initializers/constants'
import { getYoutubeDLInfo, YoutubeDLInfo } from '../../../helpers/youtube-dl'
import { createReqFiles } from '../../../helpers/express-utils'
import { logger } from '../../../helpers/logger'
import { TagModel } from '../../../models/video/tag'
import { VideoImportModel } from '../../../models/video/video-import'
import { JobQueue } from '../../../lib/job-queue/job-queue'
-import { processImage } from '../../../helpers/image-utils'
import { join } from 'path'
import { isArray } from '../../../helpers/custom-validators/misc'
-import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
import { VideoChannelModel } from '../../../models/video/video-channel'
-import { UserModel } from '../../../models/account/user'
import * as Bluebird from 'bluebird'
import * as parseTorrent from 'parse-torrent'
import { getSecureTorrentName } from '../../../helpers/utils'
import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
import { CONFIG } from '../../../initializers/config'
import { sequelizeTypescript } from '../../../initializers/database'
+import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail'
+import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type'
+import { ThumbnailModel } from '../../../models/video/thumbnail'
+import { UserModel } from '../../../models/account/user'
const auditLogger = auditLoggerFactory('video-imports')
const videoImportsRouter = express.Router()
videoName = isArray(parsed.name) ? parsed.name[ 0 ] : parsed.name as string
}
- const video = buildVideo(res.locals.videoChannel.id, body, { name: videoName }, user)
+ const video = buildVideo(res.locals.videoChannel.id, body, { name: videoName })
- await processThumbnail(req, video)
- await processPreview(req, video)
+ const thumbnailModel = await processThumbnail(req, video)
+ const previewModel = await processPreview(req, video)
const tags = body.tags || undefined
const videoImportAttributes = {
state: VideoImportState.PENDING,
userId: user.id
}
- const videoImport = await insertIntoDB(video, res.locals.videoChannel, tags, videoImportAttributes)
+ const videoImport = await insertIntoDB({
+ video,
+ thumbnailModel,
+ previewModel,
+ videoChannel: res.locals.videoChannel,
+ tags,
+ videoImportAttributes,
+ user
+ })
// Create job to import the video
const payload = {
}).end()
}
- const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo, user)
+ const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo)
- const downloadThumbnail = !await processThumbnail(req, video)
- const downloadPreview = !await processPreview(req, video)
+ const thumbnailModel = await processThumbnail(req, video)
+ const previewModel = await processPreview(req, video)
const tags = body.tags || youtubeDLInfo.tags
const videoImportAttributes = {
state: VideoImportState.PENDING,
userId: user.id
}
- const videoImport = await insertIntoDB(video, res.locals.videoChannel, tags, videoImportAttributes)
+ const videoImport = await insertIntoDB({
+ video,
+ thumbnailModel,
+ previewModel,
+ videoChannel: res.locals.videoChannel,
+ tags,
+ videoImportAttributes,
+ user
+ })
// Create job to import the video
const payload = {
type: 'youtube-dl' as 'youtube-dl',
videoImportId: videoImport.id,
thumbnailUrl: youtubeDLInfo.thumbnailUrl,
- downloadThumbnail,
- downloadPreview
+ downloadThumbnail: !thumbnailModel,
+ downloadPreview: !previewModel
}
await JobQueue.Instance.createJob({ type: 'video-import', payload })
return res.json(videoImport.toFormattedJSON()).end()
}
-function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo, user: UserModel) {
+function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo) {
const videoData = {
name: body.name || importData.name || 'Unknown name',
remote: false,
const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined
if (thumbnailField) {
const thumbnailPhysicalFile = thumbnailField[ 0 ]
- await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE)
- return true
+ return createVideoMiniatureFromExisting(thumbnailPhysicalFile.path, video, ThumbnailType.MINIATURE)
}
- return false
+ return undefined
}
async function processPreview (req: express.Request, video: VideoModel) {
const previewField = req.files ? req.files['previewfile'] : undefined
if (previewField) {
const previewPhysicalFile = previewField[0]
- await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE)
- return true
+ return createVideoMiniatureFromExisting(previewPhysicalFile.path, video, ThumbnailType.PREVIEW)
}
- return false
+ return undefined
}
-function insertIntoDB (
+function insertIntoDB (parameters: {
video: VideoModel,
+ thumbnailModel: ThumbnailModel,
+ previewModel: ThumbnailModel,
videoChannel: VideoChannelModel,
tags: string[],
- videoImportAttributes: FilteredModelAttributes<VideoImportModel>
-): Bluebird<VideoImportModel> {
+ videoImportAttributes: Partial<VideoImportModel>,
+ user: UserModel
+}): Bluebird<VideoImportModel> {
+ const { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes, user } = parameters
+
return sequelizeTypescript.transaction(async t => {
const sequelizeOptions = { transaction: t }
const videoCreated = await video.save(sequelizeOptions)
videoCreated.VideoChannel = videoChannel
- await autoBlacklistVideoIfNeeded(video, videoChannel.Account.User, t)
+ if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
+ if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
+
+ await autoBlacklistVideoIfNeeded(video, user, t)
// Set tags to the video
if (tags) {