videos.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import * as validator from 'validator'
  2. import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
  3. import { peertubeTruncate } from '../../core-utils'
  4. import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
  5. import {
  6. isVideoDurationValid,
  7. isVideoNameValid,
  8. isVideoStateValid,
  9. isVideoTagValid,
  10. isVideoTruncatedDescriptionValid,
  11. isVideoViewsValid
  12. } from '../videos'
  13. import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
  14. import { VideoState } from '../../../../shared/models/videos'
  15. function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
  16. return isBaseActivityValid(activity, 'Update') &&
  17. sanitizeAndCheckVideoTorrentObject(activity.object)
  18. }
  19. function isActivityPubVideoDurationValid (value: string) {
  20. // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
  21. return exists(value) &&
  22. typeof value === 'string' &&
  23. value.startsWith('PT') &&
  24. value.endsWith('S') &&
  25. isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
  26. }
  27. function sanitizeAndCheckVideoTorrentObject (video: any) {
  28. if (!video || video.type !== 'Video') return false
  29. if (!setValidRemoteTags(video)) return false
  30. if (!setValidRemoteVideoUrls(video)) return false
  31. if (!setRemoteVideoTruncatedContent(video)) return false
  32. if (!setValidAttributedTo(video)) return false
  33. if (!setValidRemoteCaptions(video)) return false
  34. // Default attributes
  35. if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
  36. if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
  37. if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
  38. return isActivityPubUrlValid(video.id) &&
  39. isVideoNameValid(video.name) &&
  40. isActivityPubVideoDurationValid(video.duration) &&
  41. isUUIDValid(video.uuid) &&
  42. (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
  43. (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
  44. (!video.language || isRemoteStringIdentifierValid(video.language)) &&
  45. isVideoViewsValid(video.views) &&
  46. isBooleanValid(video.sensitive) &&
  47. isBooleanValid(video.commentsEnabled) &&
  48. isBooleanValid(video.downloadEnabled) &&
  49. isDateValid(video.published) &&
  50. isDateValid(video.updated) &&
  51. (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
  52. (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
  53. isRemoteVideoIconValid(video.icon) &&
  54. video.url.length !== 0 &&
  55. video.attributedTo.length !== 0
  56. }
  57. function isRemoteVideoUrlValid (url: any) {
  58. // FIXME: Old bug, we used the width to represent the resolution. Remove it in a few release (currently beta.11)
  59. if (url.width && !url.height) url.height = url.width
  60. return url.type === 'Link' &&
  61. (
  62. // TODO: remove mimeType (backward compatibility, introduced in v1.1.0)
  63. ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType || url.mimeType) !== -1 &&
  64. isActivityPubUrlValid(url.href) &&
  65. validator.isInt(url.height + '', { min: 0 }) &&
  66. validator.isInt(url.size + '', { min: 0 }) &&
  67. (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
  68. ) ||
  69. (
  70. ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType || url.mimeType) !== -1 &&
  71. isActivityPubUrlValid(url.href) &&
  72. validator.isInt(url.height + '', { min: 0 })
  73. ) ||
  74. (
  75. ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType || url.mimeType) !== -1 &&
  76. validator.isLength(url.href, { min: 5 }) &&
  77. validator.isInt(url.height + '', { min: 0 })
  78. ) ||
  79. (
  80. (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
  81. isActivityPubUrlValid(url.href) &&
  82. isArray(url.tag)
  83. )
  84. }
  85. // ---------------------------------------------------------------------------
  86. export {
  87. sanitizeAndCheckVideoTorrentUpdateActivity,
  88. isRemoteStringIdentifierValid,
  89. sanitizeAndCheckVideoTorrentObject,
  90. isRemoteVideoUrlValid
  91. }
  92. // ---------------------------------------------------------------------------
  93. function setValidRemoteTags (video: any) {
  94. if (Array.isArray(video.tag) === false) return false
  95. video.tag = video.tag.filter(t => {
  96. return t.type === 'Hashtag' &&
  97. isVideoTagValid(t.name)
  98. })
  99. return true
  100. }
  101. function setValidRemoteCaptions (video: any) {
  102. if (!video.subtitleLanguage) video.subtitleLanguage = []
  103. if (Array.isArray(video.subtitleLanguage) === false) return false
  104. video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
  105. return isRemoteStringIdentifierValid(caption)
  106. })
  107. return true
  108. }
  109. function isRemoteNumberIdentifierValid (data: any) {
  110. return validator.isInt(data.identifier, { min: 0 })
  111. }
  112. function isRemoteStringIdentifierValid (data: any) {
  113. return typeof data.identifier === 'string'
  114. }
  115. function isRemoteVideoContentValid (mediaType: string, content: string) {
  116. return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
  117. }
  118. function isRemoteVideoIconValid (icon: any) {
  119. return icon.type === 'Image' &&
  120. isActivityPubUrlValid(icon.url) &&
  121. icon.mediaType === 'image/jpeg' &&
  122. validator.isInt(icon.width + '', { min: 0 }) &&
  123. validator.isInt(icon.height + '', { min: 0 })
  124. }
  125. function setValidRemoteVideoUrls (video: any) {
  126. if (Array.isArray(video.url) === false) return false
  127. video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
  128. return true
  129. }
  130. function setRemoteVideoTruncatedContent (video: any) {
  131. if (video.content) {
  132. video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max)
  133. }
  134. return true
  135. }