prune-storage.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. ]
  21. const storageForAllToPrune = [
  22. CONFIG.STORAGE.PREVIEWS_DIR,
  23. CONFIG.STORAGE.THUMBNAILS_DIR
  24. ]
  25. let toDelete: string[] = []
  26. for (const directory of storageOnlyOwnedToPrune) {
  27. toDelete = toDelete.concat(await pruneDirectory(directory, true))
  28. }
  29. for (const directory of storageForAllToPrune) {
  30. toDelete = toDelete.concat(await pruneDirectory(directory, false))
  31. }
  32. if (toDelete.length === 0) {
  33. console.log('No files to delete.')
  34. return
  35. }
  36. console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
  37. const res = await askConfirmation()
  38. if (res === true) {
  39. console.log('Processing delete...\n')
  40. for (const path of toDelete) {
  41. await remove(path)
  42. }
  43. console.log('Done!')
  44. } else {
  45. console.log('Exiting without deleting files.')
  46. }
  47. }
  48. async function pruneDirectory (directory: string, onlyOwned = false) {
  49. const files = await readdir(directory)
  50. const toDelete: string[] = []
  51. for (const file of files) {
  52. const uuid = getUUIDFromFilename(file)
  53. let video: VideoModel
  54. let localRedundancy: boolean
  55. if (uuid) {
  56. video = await VideoModel.loadByUUIDWithFile(uuid)
  57. localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
  58. }
  59. if (
  60. !uuid ||
  61. !video ||
  62. (onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
  63. ) {
  64. toDelete.push(join(directory, file))
  65. }
  66. }
  67. return toDelete
  68. }
  69. async function askConfirmation () {
  70. return new Promise((res, rej) => {
  71. prompt.start()
  72. const schema = {
  73. properties: {
  74. confirm: {
  75. type: 'string',
  76. description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
  77. ' Can we delete these files?',
  78. default: 'n',
  79. required: true
  80. }
  81. }
  82. }
  83. prompt.get(schema, function (err, result) {
  84. if (err) return rej(err)
  85. return res(result.confirm && result.confirm.match(/y/) !== null)
  86. })
  87. })
  88. }