]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/import.ts
Cleanup model types
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / import.ts
index f9a24a0c2c7c628af9f70f708593df6735dac956..e7adcc35a2597ebaa1adafb875202135e8ee9827 100644 (file)
@@ -15,8 +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'
 import { getSecureTorrentName } from '../../../helpers/utils'
@@ -24,9 +22,17 @@ 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 {
+  MChannelAccountDefault,
+  MThumbnail,
+  MUser,
+  MVideoTag,
+  MVideoThumbnailAccountDefault,
+  MVideoWithBlacklistLight
+} from '@server/typings/models'
+import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import'
 
 const auditLogger = auditLoggerFactory('video-imports')
 const videoImportsRouter = express.Router()
@@ -108,7 +114,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 +159,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 +213,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, false)
   }
 
   return undefined
@@ -216,39 +224,41 @@ 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, false)
   }
 
   return undefined
 }
 
 function insertIntoDB (parameters: {
-  video: VideoModel,
-  thumbnailModel: ThumbnailModel,
-  previewModel: ThumbnailModel,
-  videoChannel: VideoChannelModel,
+  video: MVideoThumbnailAccountDefault,
+  thumbnailModel: MThumbnail,
+  previewModel: MThumbnail,
+  videoChannel: MChannelAccountDefault,
   tags: string[],
-  videoImportAttributes: FilteredModelAttributes<VideoImportModel>
-}): Bluebird<VideoImportModel> {
-  let { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes } = parameters
+  videoImportAttributes: Partial<MVideoImport>,
+  user: MUser
+}): Bluebird<MVideoImportVideo> {
+  const { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes, user } = parameters
 
   return sequelizeTypescript.transaction(async t => {
     const sequelizeOptions = { transaction: t }
 
     // Save video object in database
-    const videoCreated = await video.save(sequelizeOptions)
+    const videoCreated = await video.save(sequelizeOptions) as (MVideoThumbnailAccountDefault & MVideoWithBlacklistLight & MVideoTag)
     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: videoCreated,
+      user,
+      notify: false,
+      isRemote: false,
+      isNew: true,
+      transaction: t
+    })
 
     // Set tags to the video
     if (tags) {
@@ -264,7 +274,7 @@ function insertIntoDB (parameters: {
     const videoImport = await VideoImportModel.create(
       Object.assign({ videoId: videoCreated.id }, videoImportAttributes),
       sequelizeOptions
-    )
+    ) as MVideoImportVideo
     videoImport.Video = videoCreated
 
     return videoImport