peertube-5.0.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { ensureDir } from 'fs-extra/esm'
  2. import { Op } from 'sequelize'
  3. import { updateTorrentMetadata } from '@server/helpers/webtorrent.js'
  4. import { DIRECTORIES } from '@server/initializers/constants.js'
  5. import { moveFilesIfPrivacyChanged } from '@server/lib/video-privacy.js'
  6. import { VideoModel } from '@server/models/video/video.js'
  7. import { MVideoFullLight } from '@server/types/models/index.js'
  8. import { VideoPrivacy } from '@peertube/peertube-models'
  9. import { initDatabaseModels } from '@server/initializers/database.js'
  10. run()
  11. .then(() => process.exit(0))
  12. .catch(err => {
  13. console.error(err)
  14. process.exit(-1)
  15. })
  16. async function run () {
  17. console.log('Moving private video files in dedicated folders.')
  18. await ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE)
  19. await ensureDir(DIRECTORIES.VIDEOS.PRIVATE)
  20. await initDatabaseModels(true)
  21. const videos = await VideoModel.unscoped().findAll({
  22. attributes: [ 'uuid' ],
  23. where: {
  24. privacy: {
  25. [Op.in]: [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]
  26. }
  27. }
  28. })
  29. for (const { uuid } of videos) {
  30. try {
  31. console.log('Moving files of video %s.', uuid)
  32. const video = await VideoModel.loadFull(uuid)
  33. try {
  34. await moveFilesIfPrivacyChanged(video, VideoPrivacy.PUBLIC)
  35. } catch (err) {
  36. console.error('Cannot move files of video %s.', uuid, err)
  37. }
  38. try {
  39. await updateTorrents(video)
  40. } catch (err) {
  41. console.error('Cannot regenerate torrents of video %s.', uuid, err)
  42. }
  43. } catch (err) {
  44. console.error('Cannot process video %s.', uuid, err)
  45. }
  46. }
  47. }
  48. async function updateTorrents (video: MVideoFullLight) {
  49. for (const file of video.VideoFiles) {
  50. await updateTorrentMetadata(video, file)
  51. await file.save()
  52. }
  53. const playlist = video.getHLSPlaylist()
  54. for (const file of (playlist?.VideoFiles || [])) {
  55. await updateTorrentMetadata(playlist, file)
  56. await file.save()
  57. }
  58. }