find_malformed_filetrees.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const { ReadPreference } = require('mongodb')
  2. const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
  3. async function main() {
  4. await waitForDb()
  5. const projects = db.projects.find(
  6. {},
  7. { projection: { rootFolder: 1 }, readPreference: ReadPreference.SECONDARY }
  8. )
  9. let projectsProcessed = 0
  10. for await (const project of projects) {
  11. projectsProcessed += 1
  12. if (projectsProcessed % 100000 === 0) {
  13. console.log(projectsProcessed, 'projects processed')
  14. }
  15. processProject(project)
  16. }
  17. }
  18. function processProject(project) {
  19. if (!project.rootFolder || !Array.isArray(project.rootFolder)) {
  20. console.log('BAD PATH:', project._id, 'rootFolder')
  21. return
  22. }
  23. if (!project.rootFolder[0]) {
  24. console.log('BAD PATH:', project._id, 'rootFolder.0')
  25. return
  26. }
  27. const badPaths = findBadPaths(project.rootFolder[0])
  28. for (const path of badPaths) {
  29. console.log('BAD PATH:', project._id, `rootFolder.0.${path}`)
  30. }
  31. }
  32. function findBadPaths(folder) {
  33. const result = []
  34. if (typeof folder.name !== 'string' || !folder.name) {
  35. result.push('name')
  36. }
  37. if (folder.folders) {
  38. if (Array.isArray(folder.folders)) {
  39. for (const [i, subfolder] of folder.folders.entries()) {
  40. if (!subfolder || typeof subfolder !== 'object') {
  41. result.push(`folders.${i}`)
  42. continue
  43. }
  44. for (const badPath of findBadPaths(subfolder)) {
  45. result.push(`folders.${i}.${badPath}`)
  46. }
  47. }
  48. } else {
  49. result.push('folders')
  50. }
  51. }
  52. if (folder.docs) {
  53. if (Array.isArray(folder.docs)) {
  54. for (const [i, doc] of folder.docs.entries()) {
  55. if (!doc || typeof doc !== 'object') {
  56. result.push(`docs.${i}`)
  57. continue
  58. }
  59. if (typeof doc.name !== 'string' || !doc.name) {
  60. result.push(`docs.${i}.name`)
  61. continue
  62. }
  63. }
  64. } else {
  65. result.push('docs')
  66. }
  67. }
  68. if (folder.fileRefs) {
  69. if (Array.isArray(folder.fileRefs)) {
  70. for (const [i, file] of folder.fileRefs.entries()) {
  71. if (!file || typeof file !== 'object') {
  72. result.push(`fileRefs.${i}`)
  73. continue
  74. }
  75. if (typeof file.name !== 'string' || !file.name) {
  76. result.push(`fileRefs.${i}.name`)
  77. continue
  78. }
  79. }
  80. } else {
  81. result.push('fileRefs')
  82. }
  83. }
  84. return result
  85. }
  86. main()
  87. .then(() => {
  88. process.exit(0)
  89. })
  90. .catch(err => {
  91. console.error(err)
  92. process.exit(1)
  93. })