prune-storage.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import * as prompt from 'prompt'
  2. import { join } from 'path'
  3. import { CONFIG } from '../server/initializers/constants'
  4. import { VideoModel } from '../server/models/video/video'
  5. import { initDatabaseModels } from '../server/initializers'
  6. import { remove, readdir } from 'fs-extra'
  7. import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
  8. import { getUUIDFromFilename } from '../server/helpers/utils'
  9. run()
  10. .then(() => process.exit(0))
  11. .catch(err => {
  12. console.error(err)
  13. process.exit(-1)
  14. })
  15. async function run () {
  16. await initDatabaseModels(true)
  17. const storageOnlyOwnedToPrune = [
  18. CONFIG.STORAGE.VIDEOS_DIR,
  19. CONFIG.STORAGE.TORRENTS_DIR,
  20. CONFIG.STORAGE.REDUNDANCY_DIR
  21. ]
  22. const storageForAllToPrune = [
  23. CONFIG.STORAGE.PREVIEWS_DIR,
  24. CONFIG.STORAGE.THUMBNAILS_DIR
  25. ]
  26. let toDelete: string[] = []
  27. for (const directory of storageOnlyOwnedToPrune) {
  28. toDelete = toDelete.concat(await pruneDirectory(directory, true))
  29. }
  30. for (const directory of storageForAllToPrune) {
  31. toDelete = toDelete.concat(await pruneDirectory(directory, false))
  32. }
  33. const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
  34. toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
  35. if (toDelete.length === 0) {
  36. console.log('No files to delete.')
  37. return
  38. }
  39. console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
  40. const res = await askConfirmation()
  41. if (res === true) {
  42. console.log('Processing delete...\n')
  43. for (const path of toDelete) {
  44. await remove(path)
  45. }
  46. console.log('Done!')
  47. } else {
  48. console.log('Exiting without deleting files.')
  49. }
  50. }
  51. async function pruneDirectory (directory: string, onlyOwned = false) {
  52. const files = await readdir(directory)
  53. const toDelete: string[] = []
  54. for (const file of files) {
  55. const uuid = getUUIDFromFilename(file)
  56. let video: VideoModel
  57. let localRedundancy: boolean
  58. if (uuid) {
  59. video = await VideoModel.loadByUUIDWithFile(uuid)
  60. localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
  61. }
  62. if (
  63. !uuid ||
  64. !video ||
  65. (onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
  66. ) {
  67. toDelete.push(join(directory, file))
  68. }
  69. }
  70. return toDelete
  71. }
  72. async function askConfirmation () {
  73. return new Promise((res, rej) => {
  74. prompt.start()
  75. const schema = {
  76. properties: {
  77. confirm: {
  78. type: 'string',
  79. description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
  80. ' Notice PeerTube must have been stopped when your ran this script.' +
  81. ' Can we delete these files?',
  82. default: 'n',
  83. required: true
  84. }
  85. }
  86. }
  87. prompt.get(schema, function (err, result) {
  88. if (err) return rej(err)
  89. return res(result.confirm && result.confirm.match(/y/) !== null)
  90. })
  91. })
  92. }