video.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Transaction } from 'sequelize/types'
  2. import { sequelizeTypescript } from '@server/initializers/database'
  3. import { TagModel } from '@server/models/video/tag'
  4. import { VideoModel } from '@server/models/video/video'
  5. import { FilteredModelAttributes } from '@server/types'
  6. import { MTag, MThumbnail, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
  7. import { ThumbnailType, VideoCreate, VideoPrivacy } from '@shared/models'
  8. import { federateVideoIfNeeded } from './activitypub/videos'
  9. import { Notifier } from './notifier'
  10. import { createVideoMiniatureFromExisting } from './thumbnail'
  11. function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
  12. return {
  13. name: videoInfo.name,
  14. remote: false,
  15. category: videoInfo.category,
  16. licence: videoInfo.licence,
  17. language: videoInfo.language,
  18. commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true"
  19. downloadEnabled: videoInfo.downloadEnabled !== false,
  20. waitTranscoding: videoInfo.waitTranscoding || false,
  21. nsfw: videoInfo.nsfw || false,
  22. description: videoInfo.description,
  23. support: videoInfo.support,
  24. privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
  25. channelId: channelId,
  26. originallyPublishedAt: videoInfo.originallyPublishedAt
  27. }
  28. }
  29. async function buildVideoThumbnailsFromReq (options: {
  30. video: MVideoThumbnail
  31. files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[]
  32. fallback: (type: ThumbnailType) => Promise<MThumbnail>
  33. automaticallyGenerated?: boolean
  34. }) {
  35. const { video, files, fallback, automaticallyGenerated } = options
  36. const promises = [
  37. {
  38. type: ThumbnailType.MINIATURE,
  39. fieldName: 'thumbnailfile'
  40. },
  41. {
  42. type: ThumbnailType.PREVIEW,
  43. fieldName: 'previewfile'
  44. }
  45. ].map(p => {
  46. const fields = files?.[p.fieldName]
  47. if (fields) {
  48. return createVideoMiniatureFromExisting({
  49. inputPath: fields[0].path,
  50. video,
  51. type: p.type,
  52. automaticallyGenerated: automaticallyGenerated || false
  53. })
  54. }
  55. return fallback(p.type)
  56. })
  57. return Promise.all(promises)
  58. }
  59. async function setVideoTags (options: {
  60. video: MVideoTag
  61. tags: string[]
  62. transaction?: Transaction
  63. defaultValue?: MTag[]
  64. }) {
  65. const { video, tags, transaction, defaultValue } = options
  66. // Set tags to the video
  67. if (tags) {
  68. const tagInstances = await TagModel.findOrCreateTags(tags, transaction)
  69. await video.$set('Tags', tagInstances, { transaction })
  70. video.Tags = tagInstances
  71. } else {
  72. video.Tags = defaultValue || []
  73. }
  74. }
  75. async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) {
  76. const result = await sequelizeTypescript.transaction(async t => {
  77. // Maybe the video changed in database, refresh it
  78. const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
  79. // Video does not exist anymore
  80. if (!videoDatabase) return undefined
  81. // We transcoded the video file in another format, now we can publish it
  82. const videoPublished = await videoDatabase.publishIfNeededAndSave(t)
  83. // If the video was not published, we consider it is a new one for other instances
  84. // Live videos are always federated, so it's not a new video
  85. await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t)
  86. return { videoDatabase, videoPublished }
  87. })
  88. if (result?.videoPublished) {
  89. Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase)
  90. Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase)
  91. }
  92. }
  93. // ---------------------------------------------------------------------------
  94. export {
  95. buildLocalVideoFromReq,
  96. publishAndFederateIfNeeded,
  97. buildVideoThumbnailsFromReq,
  98. setVideoTags
  99. }