X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fimport.ts;h=dcba0e08f4a2372505de2d71615cd4eb408c2735;hb=b91bc1d1f3591c35ab4426f6ab594b4bd9f1ef62;hp=5a8cd703ea021143db6679244c14ad28f4f67d28;hpb=6dd9de95dfa39bd5c1faed00d1dbd52cd112bae0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index 5a8cd703e..dcba0e08f 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts @@ -3,7 +3,7 @@ import * as magnetUtil from 'magnet-uri' import 'multer' import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger' import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares' -import { MIMETYPES, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE } from '../../../initializers' +import { MIMETYPES } from '../../../initializers/constants' import { getYoutubeDLInfo, YoutubeDLInfo } from '../../../helpers/youtube-dl' import { createReqFiles } from '../../../helpers/express-utils' import { logger } from '../../../helpers/logger' @@ -13,18 +13,20 @@ import { getVideoActivityPubUrl } from '../../../lib/activitypub' 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 { move, readFile } from 'fs-extra' 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() @@ -88,10 +90,10 @@ async function addTorrentImport (req: express.Request, res: express.Response, to 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 = { @@ -100,7 +102,15 @@ async function addTorrentImport (req: express.Request, res: express.Response, to 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 = { @@ -131,10 +141,10 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response) }).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 = { @@ -142,15 +152,23 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response) 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 }) @@ -159,7 +177,7 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response) 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, @@ -188,32 +206,35 @@ async function processThumbnail (req: express.Request, video: VideoModel) { 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 -): Bluebird { + videoImportAttributes: Partial, + user: UserModel +}): Bluebird { + const { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes, user } = parameters + return sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } @@ -221,7 +242,10 @@ function insertIntoDB ( 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) {