X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fjob-queue%2Fhandlers%2Fvideo-file-import.ts;h=d950f64072e20db266a2989641322358db7e9bef;hb=e15bc9dcd59ebb6102d1455450d2e8bbf9f49c27;hp=5c5b7dccb77c5bac463080d492427d1b4ee5e2b3;hpb=d5c8932a601c1854db0a2e399ccaf26e17385f1a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/job-queue/handlers/video-file-import.ts b/server/lib/job-queue/handlers/video-file-import.ts index 5c5b7dccb..d950f6407 100644 --- a/server/lib/job-queue/handlers/video-file-import.ts +++ b/server/lib/job-queue/handlers/video-file-import.ts @@ -1,23 +1,25 @@ -import * as Bull from 'bull' -import { logger } from '../../../helpers/logger' -import { VideoModel } from '../../../models/video/video' -import { publishNewResolutionIfNeeded } from './video-transcoding' -import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' +import { Job } from 'bullmq' import { copy, stat } from 'fs-extra' -import { VideoFileModel } from '../../../models/video/video-file' -import { extname } from 'path' -import { MVideoFile, MVideoWithFile } from '@server/typings/models' - -export type VideoFileImportPayload = { - videoUUID: string, - filePath: string -} +import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' +import { CONFIG } from '@server/initializers/config' +import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' +import { generateWebTorrentVideoFilename } from '@server/lib/paths' +import { buildMoveToObjectStorageJob } from '@server/lib/video' +import { VideoPathManager } from '@server/lib/video-path-manager' +import { VideoModel } from '@server/models/video/video' +import { VideoFileModel } from '@server/models/video/video-file' +import { MVideoFullLight } from '@server/types/models' +import { getLowercaseExtension } from '@shared/core-utils' +import { VideoFileImportPayload, VideoStorage } from '@shared/models' +import { getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg' +import { logger } from '../../../helpers/logger' +import { JobQueue } from '../job-queue' -async function processVideoFileImport (job: Bull.Job) { +async function processVideoFileImport (job: Job) { const payload = job.data as VideoFileImportPayload - logger.info('Processing video file import in job %d.', job.id) + logger.info('Processing video file import in job %s.', job.id) - const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) + const video = await VideoModel.loadFull(payload.videoUUID) // No video, maybe deleted? if (!video) { logger.info('Do not process job %d, video does not exist.', job.id) @@ -26,7 +28,12 @@ async function processVideoFileImport (job: Bull.Job) { await updateVideoFile(video, payload.filePath) - await publishNewResolutionIfNeeded(video) + if (CONFIG.OBJECT_STORAGE.ENABLED) { + await JobQueue.Instance.createJob(await buildMoveToObjectStorageJob({ video, previousVideoState: video.state })) + } else { + await federateVideoIfNeeded(video, false) + } + return video } @@ -38,42 +45,39 @@ export { // --------------------------------------------------------------------------- -async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) { - const { videoFileResolution } = await getVideoFileResolution(inputFilePath) +async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) { + const { resolution } = await getVideoStreamDimensionsInfo(inputFilePath) const { size } = await stat(inputFilePath) - const fps = await getVideoFileFPS(inputFilePath) + const fps = await getVideoStreamFPS(inputFilePath) - let updatedVideoFile = new VideoFileModel({ - resolution: videoFileResolution, - extname: extname(inputFilePath), - size, - fps, - videoId: video.id - }) as MVideoFile + const fileExt = getLowercaseExtension(inputFilePath) - const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution) + const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution) if (currentVideoFile) { // Remove old file and old torrent - await video.removeFile(currentVideoFile) - await video.removeTorrent(currentVideoFile) + await video.removeWebTorrentFile(currentVideoFile) // Remove the old video file from the array video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) - // Update the database - currentVideoFile.extname = updatedVideoFile.extname - currentVideoFile.size = updatedVideoFile.size - currentVideoFile.fps = updatedVideoFile.fps - - updatedVideoFile = currentVideoFile + await currentVideoFile.destroy() } - const outputPath = video.getVideoFilePath(updatedVideoFile) - await copy(inputFilePath, outputPath) + const newVideoFile = new VideoFileModel({ + resolution, + extname: fileExt, + filename: generateWebTorrentVideoFilename(resolution, fileExt), + storage: VideoStorage.FILE_SYSTEM, + size, + fps, + videoId: video.id + }) - await video.createTorrentAndSetInfoHash(updatedVideoFile) + const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile) + await copy(inputFilePath, outputPath) - await updatedVideoFile.save() + video.VideoFiles.push(newVideoFile) + await createTorrentAndSetInfoHash(video, newVideoFile) - video.VideoFiles.push(updatedVideoFile) + await newVideoFile.save() }