prune-storage.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. run()
  8. .then(() => process.exit(0))
  9. .catch(err => {
  10. console.error(err)
  11. process.exit(-1)
  12. })
  13. async function run () {
  14. await initDatabaseModels(true)
  15. const storageToPrune = [
  16. CONFIG.STORAGE.VIDEOS_DIR,
  17. CONFIG.STORAGE.PREVIEWS_DIR,
  18. CONFIG.STORAGE.THUMBNAILS_DIR,
  19. CONFIG.STORAGE.TORRENTS_DIR
  20. ]
  21. let toDelete: string[] = []
  22. for (const directory of storageToPrune) {
  23. toDelete = toDelete.concat(await pruneDirectory(directory))
  24. }
  25. if (toDelete.length === 0) {
  26. console.log('No files to delete.')
  27. return
  28. }
  29. console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
  30. const res = await askConfirmation()
  31. if (res === true) {
  32. console.log('Processing delete...\n')
  33. for (const path of toDelete) {
  34. await remove(path)
  35. }
  36. console.log('Done!')
  37. } else {
  38. console.log('Exiting without deleting files.')
  39. }
  40. }
  41. async function pruneDirectory (directory: string) {
  42. const files = await readdir(directory)
  43. const toDelete: string[] = []
  44. for (const file of files) {
  45. const uuid = getUUIDFromFilename(file)
  46. let video: VideoModel
  47. if (uuid) video = await VideoModel.loadByUUID(uuid)
  48. if (!uuid || !video) toDelete.push(join(directory, file))
  49. }
  50. return toDelete
  51. }
  52. function getUUIDFromFilename (filename: string) {
  53. const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
  54. const result = filename.match(regex)
  55. if (!result || Array.isArray(result) === false) return null
  56. return result[0]
  57. }
  58. async function askConfirmation () {
  59. return new Promise((res, rej) => {
  60. prompt.start()
  61. const schema = {
  62. properties: {
  63. confirm: {
  64. type: 'string',
  65. description: 'Are you sure you want to delete these files? Please check carefully',
  66. default: 'n',
  67. required: true
  68. }
  69. }
  70. }
  71. prompt.get(schema, function (err, result) {
  72. if (err) return rej(err)
  73. return res(result.confirm && result.confirm.match(/y/) !== null)
  74. })
  75. })
  76. }