diff options
Diffstat (limited to 'server/lib/job-queue/handlers')
-rw-r--r-- | server/lib/job-queue/handlers/video-import.ts | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/server/lib/job-queue/handlers/video-import.ts b/server/lib/job-queue/handlers/video-import.ts index 4f2faab7d..3b9d08d3b 100644 --- a/server/lib/job-queue/handlers/video-import.ts +++ b/server/lib/job-queue/handlers/video-import.ts | |||
@@ -12,6 +12,7 @@ import { doRequestAndSaveToFile } from '../../../helpers/requests' | |||
12 | import { VideoState } from '../../../../shared' | 12 | import { VideoState } from '../../../../shared' |
13 | import { JobQueue } from '../index' | 13 | import { JobQueue } from '../index' |
14 | import { federateVideoIfNeeded } from '../../activitypub' | 14 | import { federateVideoIfNeeded } from '../../activitypub' |
15 | import { VideoModel } from '../../../models/video/video' | ||
15 | 16 | ||
16 | export type VideoImportPayload = { | 17 | export type VideoImportPayload = { |
17 | type: 'youtube-dl' | 18 | type: 'youtube-dl' |
@@ -26,9 +27,13 @@ async function processVideoImport (job: Bull.Job) { | |||
26 | logger.info('Processing video import in job %d.', job.id) | 27 | logger.info('Processing video import in job %d.', job.id) |
27 | 28 | ||
28 | const videoImport = await VideoImportModel.loadAndPopulateVideo(payload.videoImportId) | 29 | const videoImport = await VideoImportModel.loadAndPopulateVideo(payload.videoImportId) |
29 | if (!videoImport) throw new Error('Cannot import video %s: the video import entry does not exist anymore.') | 30 | if (!videoImport || !videoImport.Video) { |
31 | throw new Error('Cannot import video %s: the video import or video linked to this import does not exist anymore.') | ||
32 | } | ||
30 | 33 | ||
31 | let tempVideoPath: string | 34 | let tempVideoPath: string |
35 | let videoDestFile: string | ||
36 | let videoFile: VideoFileModel | ||
32 | try { | 37 | try { |
33 | // Download video from youtubeDL | 38 | // Download video from youtubeDL |
34 | tempVideoPath = await downloadYoutubeDLVideo(videoImport.targetUrl) | 39 | tempVideoPath = await downloadYoutubeDLVideo(videoImport.targetUrl) |
@@ -47,11 +52,14 @@ async function processVideoImport (job: Bull.Job) { | |||
47 | fps, | 52 | fps, |
48 | videoId: videoImport.videoId | 53 | videoId: videoImport.videoId |
49 | } | 54 | } |
50 | const videoFile = new VideoFileModel(videoFileData) | 55 | videoFile = new VideoFileModel(videoFileData) |
56 | // Import if the import fails, to clean files | ||
57 | videoImport.Video.VideoFiles = [ videoFile ] | ||
51 | 58 | ||
52 | // Move file | 59 | // Move file |
53 | const destination = join(CONFIG.STORAGE.VIDEOS_DIR, videoImport.Video.getVideoFilename(videoFile)) | 60 | videoDestFile = join(CONFIG.STORAGE.VIDEOS_DIR, videoImport.Video.getVideoFilename(videoFile)) |
54 | await renamePromise(tempVideoPath, destination) | 61 | await renamePromise(tempVideoPath, videoDestFile) |
62 | tempVideoPath = null // This path is not used anymore | ||
55 | 63 | ||
56 | // Process thumbnail | 64 | // Process thumbnail |
57 | if (payload.downloadThumbnail) { | 65 | if (payload.downloadThumbnail) { |
@@ -77,15 +85,21 @@ async function processVideoImport (job: Bull.Job) { | |||
77 | await videoImport.Video.createTorrentAndSetInfoHash(videoFile) | 85 | await videoImport.Video.createTorrentAndSetInfoHash(videoFile) |
78 | 86 | ||
79 | const videoImportUpdated: VideoImportModel = await sequelizeTypescript.transaction(async t => { | 87 | const videoImportUpdated: VideoImportModel = await sequelizeTypescript.transaction(async t => { |
80 | await videoFile.save({ transaction: t }) | 88 | // Refresh video |
89 | const video = await VideoModel.load(videoImport.videoId, t) | ||
90 | if (!video) throw new Error('Video linked to import ' + videoImport.videoId + ' does not exist anymore.') | ||
91 | videoImport.Video = video | ||
92 | |||
93 | const videoFileCreated = await videoFile.save({ transaction: t }) | ||
94 | video.VideoFiles = [ videoFileCreated ] | ||
81 | 95 | ||
82 | // Update video DB object | 96 | // Update video DB object |
83 | videoImport.Video.duration = duration | 97 | video.duration = duration |
84 | videoImport.Video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED | 98 | video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED |
85 | const videoUpdated = await videoImport.Video.save({ transaction: t }) | 99 | const videoUpdated = await video.save({ transaction: t }) |
86 | 100 | ||
87 | // Now we can federate the video | 101 | // Now we can federate the video |
88 | await federateVideoIfNeeded(videoImport.Video, true, t) | 102 | await federateVideoIfNeeded(video, true, t) |
89 | 103 | ||
90 | // Update video import object | 104 | // Update video import object |
91 | videoImport.state = VideoImportState.SUCCESS | 105 | videoImport.state = VideoImportState.SUCCESS |
@@ -112,7 +126,7 @@ async function processVideoImport (job: Bull.Job) { | |||
112 | try { | 126 | try { |
113 | if (tempVideoPath) await unlinkPromise(tempVideoPath) | 127 | if (tempVideoPath) await unlinkPromise(tempVideoPath) |
114 | } catch (errUnlink) { | 128 | } catch (errUnlink) { |
115 | logger.error('Cannot cleanup files after a video import error.', { err: errUnlink }) | 129 | logger.warn('Cannot cleanup files after a video import error.', { err: errUnlink }) |
116 | } | 130 | } |
117 | 131 | ||
118 | videoImport.error = err.message | 132 | videoImport.error = err.message |