ニジカ投稿局 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.
 
 
 
 
 

93 lines
2.8 KiB

  1. import { createCommand } from '@commander-js/extra-typings'
  2. import { initDatabaseModels } from '@server/initializers/database.js'
  3. import { ActorImageModel } from '@server/models/actor/actor-image.js'
  4. import { ThumbnailModel } from '@server/models/video/thumbnail.js'
  5. import Bluebird from 'bluebird'
  6. import { askConfirmation, displayPeerTubeMustBeStoppedWarning } from './shared/common.js'
  7. const program = createCommand()
  8. .description('Remove unused objects from database or remote files')
  9. .option('--delete-remote-files', 'Remove remote files (avatars, banners, thumbnails...)')
  10. .parse(process.argv)
  11. const options = program.opts()
  12. if (!options.deleteRemoteFiles) {
  13. console.log('At least one option must be set (for example --delete-remote-files).')
  14. process.exit(0)
  15. }
  16. run()
  17. .then(() => process.exit(0))
  18. .catch(err => {
  19. console.error(err)
  20. process.exit(-1)
  21. })
  22. async function run () {
  23. await initDatabaseModels(true)
  24. displayPeerTubeMustBeStoppedWarning()
  25. if (options.deleteRemoteFiles) {
  26. return deleteRemoteFiles()
  27. }
  28. }
  29. async function deleteRemoteFiles () {
  30. console.log('Detecting remote files that can be deleted...')
  31. const thumbnails = await ThumbnailModel.listRemoteOnDisk()
  32. const actorImages = await ActorImageModel.listRemoteOnDisk()
  33. if (thumbnails.length === 0 && actorImages.length === 0) {
  34. console.log('No remote files to delete detected.')
  35. process.exit(0)
  36. }
  37. const res = await askConfirmation(
  38. `${thumbnails.length.toLocaleString()} thumbnails and ${actorImages.length.toLocaleString()} avatars/banners can be locally deleted. ` +
  39. `PeerTube will download them again on-demand. ` +
  40. `Do you want to delete these remote files?`
  41. )
  42. if (res !== true) {
  43. console.log('Exiting without delete remote files.')
  44. process.exit(0)
  45. }
  46. // ---------------------------------------------------------------------------
  47. console.log('Deleting remote thumbnails...')
  48. await Bluebird.map(thumbnails, async thumbnail => {
  49. if (!thumbnail.fileUrl) {
  50. console.log(`Skipping thumbnail removal of ${thumbnail.getPath()} as we don't have its remote file URL in the database.`)
  51. return
  52. }
  53. await thumbnail.removeThumbnail()
  54. thumbnail.onDisk = false
  55. await thumbnail.save()
  56. }, { concurrency: 20 })
  57. // ---------------------------------------------------------------------------
  58. console.log('Deleting remote avatars/banners...')
  59. await Bluebird.map(actorImages, async actorImage => {
  60. if (!actorImage.fileUrl) {
  61. console.log(`Skipping avatar/banner removal of ${actorImage.getPath()} as we don't have its remote file URL in the database.`)
  62. return
  63. }
  64. await actorImage.removeImage()
  65. actorImage.onDisk = false
  66. await actorImage.save()
  67. }, { concurrency: 20 })
  68. console.log('Remote files deleted!')
  69. }