Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

3.0 KB
Raw Blame
import { getVideoStreamDimensionsInfo } from '@peertube/peertube-ffmpeg'
import { VideoFileImportPayload } from '@peertube/peertube-models'
import { CONFIG } from '@server/initializers/config.js'
import { scheduleVideoFederation } from '@server/lib/activitypub/videos/index.js'
import { buildNewFile } from '@server/lib/video-file.js'
import { buildMoveVideoJob } from '@server/lib/video-jobs.js'
import { VideoPathManager } from '@server/lib/video-path-manager.js'
import { createTorrentForFile } from '@server/lib/webtorrent.js'
import { VideoInfohashModel } from '@server/models/video/video-infohash.js'
import { VideoModel } from '@server/models/video/video.js'
import { MVideoFull } from '@server/types/models/index.js'
import { Job } from 'bullmq'
import { copy } from 'fs-extra/esm'
import { createLogger } from '../../../helpers/logger.js'
import { JobQueue } from '../job-queue.js'

const logger = createLogger()

export async function processVideoFileImport (job: Job) {
  const payload = job.data as VideoFileImportPayload
  logger.info('Processing video file import in job %s.', job.id)

  const video = await VideoModel.loadFull(payload.videoUUID)
  // No video, maybe deleted?
  if (!video) {
    logger.info(`Do not process job ${job.id}, video does not exist.`)
    return undefined
  }

  return logger.withContext([ video.uuid ], async () => {
    await updateVideoFile(video, payload.filePath)

    if (CONFIG.OBJECT_STORAGE.ENABLED) {
      await JobQueue.Instance.createJob(
        await buildMoveVideoJob({
          type: 'move-to-object-storage',
          video,
          moveVideoState: {
            previousVideoState: video.state
          }
        })
      )
    } else {
      scheduleVideoFederation({ video })
    }

    return video
  })
}

// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------

async function updateVideoFile (video: MVideoFull, inputFilePath: string) {
  const { resolution } = await getVideoStreamDimensionsInfo(inputFilePath)
  const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)

  if (currentVideoFile) {
    // Remove old file and old torrent
    await video.removeWebVideoFile(currentVideoFile)
    // Remove the old video file from the array
    video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)

    await currentVideoFile.destroy()
  }

  const newVideoFile = await buildNewFile({ mode: 'web-video', path: inputFilePath })
  newVideoFile.videoId = video.id

  const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
  await copy(inputFilePath, outputPath)

  const { infoHash, torrentFilename } = await createTorrentForFile(video, newVideoFile)
  newVideoFile.torrentFilename = torrentFilename
  await newVideoFile.save()

  const infohashModel = await VideoInfohashModel.replaceFileInfohash(newVideoFile.id, infoHash)

  video.VideoFiles.push(Object.assign(newVideoFile, { InfoHash: infohashModel }))
}