From 453e83ea5d81d203ba34bc43cd5c2c750ba40568 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 15 Aug 2019 11:53:26 +0200 Subject: Stronger model typings --- server/lib/job-queue/handlers/video-import.ts | 38 +++++++++++++++------------ 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'server/lib/job-queue/handlers/video-import.ts') diff --git a/server/lib/job-queue/handlers/video-import.ts b/server/lib/job-queue/handlers/video-import.ts index 13b741180..f9dda79f8 100644 --- a/server/lib/job-queue/handlers/video-import.ts +++ b/server/lib/job-queue/handlers/video-import.ts @@ -17,9 +17,10 @@ import { move, remove, stat } from 'fs-extra' import { Notifier } from '../../notifier' import { CONFIG } from '../../../initializers/config' import { sequelizeTypescript } from '../../../initializers/database' -import { ThumbnailModel } from '../../../models/video/thumbnail' import { createVideoMiniatureFromUrl, generateVideoMiniature } from '../../thumbnail' import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' +import { MThumbnail } from '../../../typings/models/video/thumbnail' +import { MVideoImportDefault, MVideoImportDefaultFiles, MVideoImportVideo } from '@server/typings/models/video/video-import' type VideoImportYoutubeDLPayload = { type: 'youtube-dl' @@ -110,11 +111,13 @@ type ProcessFileOptions = { generateThumbnail: boolean generatePreview: boolean } -async function processFile (downloader: () => Promise, videoImport: VideoImportModel, options: ProcessFileOptions) { +async function processFile (downloader: () => Promise, videoImportArg: MVideoImportDefault, options: ProcessFileOptions) { let tempVideoPath: string let videoDestFile: string let videoFile: VideoFileModel + const videoImport = videoImportArg as MVideoImportDefaultFiles + try { // Download video from youtubeDL tempVideoPath = await downloader() @@ -148,7 +151,7 @@ async function processFile (downloader: () => Promise, videoImport: Vide tempVideoPath = null // This path is not used anymore // Process thumbnail - let thumbnailModel: ThumbnailModel + let thumbnailModel: MThumbnail if (options.downloadThumbnail && options.thumbnailUrl) { thumbnailModel = await createVideoMiniatureFromUrl(options.thumbnailUrl, videoImport.Video, ThumbnailType.MINIATURE) } else if (options.generateThumbnail || options.downloadThumbnail) { @@ -156,7 +159,7 @@ async function processFile (downloader: () => Promise, videoImport: Vide } // Process preview - let previewModel: ThumbnailModel + let previewModel: MThumbnail if (options.downloadPreview && options.thumbnailUrl) { previewModel = await createVideoMiniatureFromUrl(options.thumbnailUrl, videoImport.Video, ThumbnailType.PREVIEW) } else if (options.generatePreview || options.downloadPreview) { @@ -166,14 +169,15 @@ async function processFile (downloader: () => Promise, videoImport: Vide // Create torrent await videoImport.Video.createTorrentAndSetInfoHash(videoFile) - const videoImportUpdated: VideoImportModel = await sequelizeTypescript.transaction(async t => { + const { videoImportUpdated, video } = await sequelizeTypescript.transaction(async t => { + const videoImportToUpdate = videoImport as MVideoImportVideo + // Refresh video - const video = await VideoModel.load(videoImport.videoId, t) - if (!video) throw new Error('Video linked to import ' + videoImport.videoId + ' does not exist anymore.') - videoImport.Video = video + const video = await VideoModel.load(videoImportToUpdate.videoId, t) + if (!video) throw new Error('Video linked to import ' + videoImportToUpdate.videoId + ' does not exist anymore.') const videoFileCreated = await videoFile.save({ transaction: t }) - video.VideoFiles = [ videoFileCreated ] + videoImportToUpdate.Video = Object.assign(video, { VideoFiles: [ videoFileCreated ] }) // Update video DB object video.duration = duration @@ -188,25 +192,25 @@ async function processFile (downloader: () => Promise, videoImport: Vide await federateVideoIfNeeded(videoForFederation, true, t) // Update video import object - videoImport.state = VideoImportState.SUCCESS - const videoImportUpdated = await videoImport.save({ transaction: t }) + videoImportToUpdate.state = VideoImportState.SUCCESS + const videoImportUpdated = await videoImportToUpdate.save({ transaction: t }) as MVideoImportVideo + videoImportUpdated.Video = video logger.info('Video %s imported.', video.uuid) - videoImportUpdated.Video = videoForFederation - return videoImportUpdated + return { videoImportUpdated, video: videoForFederation } }) Notifier.Instance.notifyOnFinishedVideoImport(videoImportUpdated, true) - if (videoImportUpdated.Video.isBlacklisted()) { - Notifier.Instance.notifyOnVideoAutoBlacklist(videoImportUpdated.Video) + if (video.isBlacklisted()) { + Notifier.Instance.notifyOnVideoAutoBlacklist(video) } else { - Notifier.Instance.notifyOnNewVideoIfNeeded(videoImportUpdated.Video) + Notifier.Instance.notifyOnNewVideoIfNeeded(video) } // Create transcoding jobs? - if (videoImportUpdated.Video.state === VideoState.TO_TRANSCODE) { + if (video.state === VideoState.TO_TRANSCODE) { // Put uuid because we don't have id auto incremented for now const dataInput = { type: 'optimize' as 'optimize', -- cgit v1.2.3