X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fjob-queue%2Fhandlers%2Fvideo-file-import.ts;h=8297a15716819c5fba66efeb3ef7d2be5f0f8f17;hb=819b656439e5f0ec2ae5de9357502cdfe3196197;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..8297a1571 100644 --- a/server/lib/job-queue/handlers/video-file-import.ts +++ b/server/lib/job-queue/handlers/video-file-import.ts @@ -1,17 +1,16 @@ import * as Bull from 'bull' +import { copy, stat } from 'fs-extra' +import { extname } from 'path' +import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' +import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths' +import { UserModel } from '@server/models/user/user' +import { MVideoFullLight } from '@server/types/models' +import { VideoFileImportPayload } from '@shared/models' +import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' import { logger } from '../../../helpers/logger' import { VideoModel } from '../../../models/video/video' -import { publishNewResolutionIfNeeded } from './video-transcoding' -import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' -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 { onNewWebTorrentFileResolution } from './video-transcoding' async function processVideoFileImport (job: Bull.Job) { const payload = job.data as VideoFileImportPayload @@ -24,9 +23,22 @@ async function processVideoFileImport (job: Bull.Job) { return undefined } + const data = await getVideoFileResolution(payload.filePath) + await updateVideoFile(video, payload.filePath) - await publishNewResolutionIfNeeded(video) + const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId) + + const newResolutionPayload = { + type: 'new-resolution-to-webtorrent' as 'new-resolution-to-webtorrent', + videoUUID: video.uuid, + resolution: data.videoFileResolution, + isPortraitMode: data.isPortraitMode, + copyCodecs: false, + isNewVideo: false + } + await onNewWebTorrentFileResolution(video, user, newResolutionPayload) + return video } @@ -38,42 +50,39 @@ export { // --------------------------------------------------------------------------- -async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) { +async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) { const { videoFileResolution } = await getVideoFileResolution(inputFilePath) const { size } = await stat(inputFilePath) const fps = await getVideoFileFPS(inputFilePath) - let updatedVideoFile = new VideoFileModel({ - resolution: videoFileResolution, - extname: extname(inputFilePath), - size, - fps, - videoId: video.id - }) as MVideoFile + const fileExt = extname(inputFilePath) - const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution) + const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution) if (currentVideoFile) { // Remove old file and old torrent await video.removeFile(currentVideoFile) - await video.removeTorrent(currentVideoFile) + await currentVideoFile.removeTorrent() // 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: videoFileResolution, + extname: fileExt, + filename: generateVideoFilename(video, false, videoFileResolution, fileExt), + size, + fps, + videoId: video.id + }) - await video.createTorrentAndSetInfoHash(updatedVideoFile) + const outputPath = getVideoFilePath(video, newVideoFile) + await copy(inputFilePath, outputPath) - await updatedVideoFile.save() + video.VideoFiles.push(newVideoFile) + await createTorrentAndSetInfoHash(video, newVideoFile) - video.VideoFiles.push(updatedVideoFile) + await newVideoFile.save() }