]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/import.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / import.ts
index f9a24a0c2c7c628af9f70f708593df6735dac956..dcba0e08f4a2372505de2d71615cd4eb408c2735 100644 (file)
@@ -15,7 +15,6 @@ import { VideoImportModel } from '../../../models/video/video-import'
 import { JobQueue } from '../../../lib/job-queue/job-queue'
 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 * as Bluebird from 'bluebird'
 import * as parseTorrent from 'parse-torrent'
@@ -24,9 +23,10 @@ import { move, readFile } from 'fs-extra'
 import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
 import { CONFIG } from '../../../initializers/config'
 import { sequelizeTypescript } from '../../../initializers/database'
-import { createVideoThumbnailFromExisting } from '../../../lib/thumbnail'
+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()
@@ -108,7 +108,8 @@ async function addTorrentImport (req: express.Request, res: express.Response, to
     previewModel,
     videoChannel: res.locals.videoChannel,
     tags,
-    videoImportAttributes
+    videoImportAttributes,
+    user
   })
 
   // Create job to import the video
@@ -152,12 +153,13 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
     userId: user.id
   }
   const videoImport = await insertIntoDB({
-    video: video,
+    video,
     thumbnailModel,
     previewModel,
     videoChannel: res.locals.videoChannel,
     tags,
-    videoImportAttributes
+    videoImportAttributes,
+    user
   })
 
   // Create job to import the video
@@ -205,7 +207,7 @@ async function processThumbnail (req: express.Request, video: VideoModel) {
   if (thumbnailField) {
     const thumbnailPhysicalFile = thumbnailField[ 0 ]
 
-    return createVideoThumbnailFromExisting(thumbnailPhysicalFile.path, video, ThumbnailType.THUMBNAIL)
+    return createVideoMiniatureFromExisting(thumbnailPhysicalFile.path, video, ThumbnailType.MINIATURE)
   }
 
   return undefined
@@ -216,7 +218,7 @@ async function processPreview (req: express.Request, video: VideoModel) {
   if (previewField) {
     const previewPhysicalFile = previewField[0]
 
-    return createVideoThumbnailFromExisting(previewPhysicalFile.path, video, ThumbnailType.PREVIEW)
+    return createVideoMiniatureFromExisting(previewPhysicalFile.path, video, ThumbnailType.PREVIEW)
   }
 
   return undefined
@@ -228,9 +230,10 @@ function insertIntoDB (parameters: {
   previewModel: ThumbnailModel,
   videoChannel: VideoChannelModel,
   tags: string[],
-  videoImportAttributes: FilteredModelAttributes<VideoImportModel>
+  videoImportAttributes: Partial<VideoImportModel>,
+  user: UserModel
 }): Bluebird<VideoImportModel> {
-  let { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes } = parameters
+  const { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes, user } = parameters
 
   return sequelizeTypescript.transaction(async t => {
     const sequelizeOptions = { transaction: t }
@@ -239,16 +242,10 @@ function insertIntoDB (parameters: {
     const videoCreated = await video.save(sequelizeOptions)
     videoCreated.VideoChannel = videoChannel
 
-    if (thumbnailModel) {
-      thumbnailModel.videoId = videoCreated.id
-      videoCreated.addThumbnail(await thumbnailModel.save({ transaction: t }))
-    }
-    if (previewModel) {
-      previewModel.videoId = videoCreated.id
-      videoCreated.addThumbnail(await previewModel.save({ transaction: t }))
-    }
+    if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
+    if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
 
-    await autoBlacklistVideoIfNeeded(video, videoChannel.Account.User, t)
+    await autoBlacklistVideoIfNeeded(video, user, t)
 
     // Set tags to the video
     if (tags) {