video-file-import.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import * as Bull from 'bull'
  2. import { copy, stat } from 'fs-extra'
  3. import { extname } from 'path'
  4. import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
  5. import { getVideoFilePath } from '@server/lib/video-paths'
  6. import { MVideoFile, MVideoWithFile } from '@server/types/models'
  7. import { VideoFileImportPayload } from '@shared/models'
  8. import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
  9. import { logger } from '../../../helpers/logger'
  10. import { VideoModel } from '../../../models/video/video'
  11. import { VideoFileModel } from '../../../models/video/video-file'
  12. import { onNewWebTorrentFileResolution } from './video-transcoding'
  13. async function processVideoFileImport (job: Bull.Job) {
  14. const payload = job.data as VideoFileImportPayload
  15. logger.info('Processing video file import in job %d.', job.id)
  16. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
  17. // No video, maybe deleted?
  18. if (!video) {
  19. logger.info('Do not process job %d, video does not exist.', job.id)
  20. return undefined
  21. }
  22. await updateVideoFile(video, payload.filePath)
  23. await onNewWebTorrentFileResolution(video)
  24. return video
  25. }
  26. // ---------------------------------------------------------------------------
  27. export {
  28. processVideoFileImport
  29. }
  30. // ---------------------------------------------------------------------------
  31. async function updateVideoFile (video: MVideoWithFile, inputFilePath: string) {
  32. const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
  33. const { size } = await stat(inputFilePath)
  34. const fps = await getVideoFileFPS(inputFilePath)
  35. let updatedVideoFile = new VideoFileModel({
  36. resolution: videoFileResolution,
  37. extname: extname(inputFilePath),
  38. size,
  39. fps,
  40. videoId: video.id
  41. }) as MVideoFile
  42. const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
  43. if (currentVideoFile) {
  44. // Remove old file and old torrent
  45. await video.removeFile(currentVideoFile)
  46. await video.removeTorrent(currentVideoFile)
  47. // Remove the old video file from the array
  48. video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
  49. // Update the database
  50. currentVideoFile.extname = updatedVideoFile.extname
  51. currentVideoFile.size = updatedVideoFile.size
  52. currentVideoFile.fps = updatedVideoFile.fps
  53. updatedVideoFile = currentVideoFile
  54. }
  55. const outputPath = getVideoFilePath(video, updatedVideoFile)
  56. await copy(inputFilePath, outputPath)
  57. await createTorrentAndSetInfoHash(video, updatedVideoFile)
  58. await updatedVideoFile.save()
  59. video.VideoFiles.push(updatedVideoFile)
  60. }