ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

video-privacy.ts 4.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { move } from 'fs-extra/esm'
  2. import { join } from 'path'
  3. import { VideoPrivacy, VideoPrivacyType, FileStorage } from '@peertube/peertube-models'
  4. import { logger } from '@server/helpers/logger.js'
  5. import { DIRECTORIES } from '@server/initializers/constants.js'
  6. import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models/index.js'
  7. import { updateHLSFilesACL, updateWebVideoFileACL } from './object-storage/index.js'
  8. const validPrivacySet = new Set<VideoPrivacyType>([
  9. VideoPrivacy.PRIVATE,
  10. VideoPrivacy.INTERNAL,
  11. VideoPrivacy.PASSWORD_PROTECTED
  12. ])
  13. function setVideoPrivacy (video: MVideo, newPrivacy: VideoPrivacyType) {
  14. if (video.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) {
  15. video.publishedAt = new Date()
  16. }
  17. video.privacy = newPrivacy
  18. }
  19. function isVideoInPrivateDirectory (privacy: VideoPrivacyType) {
  20. return validPrivacySet.has(privacy)
  21. }
  22. function isVideoInPublicDirectory (privacy: VideoPrivacyType) {
  23. return !isVideoInPrivateDirectory(privacy)
  24. }
  25. async function moveFilesIfPrivacyChanged (video: MVideoFullLight, oldPrivacy: VideoPrivacyType) {
  26. // Now public, previously private
  27. if (isVideoInPublicDirectory(video.privacy) && isVideoInPrivateDirectory(oldPrivacy)) {
  28. await moveFiles({ type: 'private-to-public', video })
  29. return true
  30. }
  31. // Now private, previously public
  32. if (isVideoInPrivateDirectory(video.privacy) && isVideoInPublicDirectory(oldPrivacy)) {
  33. await moveFiles({ type: 'public-to-private', video })
  34. return true
  35. }
  36. return false
  37. }
  38. export {
  39. setVideoPrivacy,
  40. isVideoInPrivateDirectory,
  41. isVideoInPublicDirectory,
  42. moveFilesIfPrivacyChanged
  43. }
  44. // ---------------------------------------------------------------------------
  45. type MoveType = 'private-to-public' | 'public-to-private'
  46. async function moveFiles (options: {
  47. type: MoveType
  48. video: MVideoFullLight
  49. }) {
  50. const { type, video } = options
  51. for (const file of video.VideoFiles) {
  52. if (file.storage === FileStorage.FILE_SYSTEM) {
  53. await moveWebVideoFileOnFS(type, video, file)
  54. } else {
  55. await updateWebVideoFileACL(video, file)
  56. }
  57. }
  58. const hls = video.getHLSPlaylist()
  59. if (hls) {
  60. if (hls.storage === FileStorage.FILE_SYSTEM) {
  61. await moveHLSFilesOnFS(type, video)
  62. } else {
  63. await updateHLSFilesACL(hls)
  64. }
  65. }
  66. }
  67. async function moveWebVideoFileOnFS (type: MoveType, video: MVideo, file: MVideoFile) {
  68. const directories = getWebVideoDirectories(type)
  69. const source = join(directories.old, file.filename)
  70. const destination = join(directories.new, file.filename)
  71. try {
  72. logger.info('Moving web video files of %s after privacy change (%s -> %s).', video.uuid, source, destination)
  73. await move(source, destination)
  74. } catch (err) {
  75. logger.error('Cannot move web video file %s to %s after privacy change', source, destination, { err })
  76. }
  77. }
  78. function getWebVideoDirectories (moveType: MoveType) {
  79. if (moveType === 'private-to-public') {
  80. return { old: DIRECTORIES.WEB_VIDEOS.PRIVATE, new: DIRECTORIES.WEB_VIDEOS.PUBLIC }
  81. }
  82. return { old: DIRECTORIES.WEB_VIDEOS.PUBLIC, new: DIRECTORIES.WEB_VIDEOS.PRIVATE }
  83. }
  84. // ---------------------------------------------------------------------------
  85. async function moveHLSFilesOnFS (type: MoveType, video: MVideo) {
  86. const directories = getHLSDirectories(type)
  87. const source = join(directories.old, video.uuid)
  88. const destination = join(directories.new, video.uuid)
  89. try {
  90. logger.info('Moving HLS files of %s after privacy change (%s -> %s).', video.uuid, source, destination)
  91. await move(source, destination)
  92. } catch (err) {
  93. logger.error('Cannot move HLS file %s to %s after privacy change', source, destination, { err })
  94. }
  95. }
  96. function getHLSDirectories (moveType: MoveType) {
  97. if (moveType === 'private-to-public') {
  98. return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC }
  99. }
  100. return { old: DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE }
  101. }