count_image_files.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {
  2. db,
  3. READ_PREFERENCE_SECONDARY,
  4. } from '../app/src/infrastructure/mongodb.js'
  5. import { extname } from 'node:path'
  6. import { scriptRunner } from './lib/ScriptRunner.mjs'
  7. const FILE_TYPES = [
  8. '.jpg',
  9. '.jpeg',
  10. '.png',
  11. '.bmp',
  12. '.webp',
  13. '.svg',
  14. '.pdf',
  15. '.eps',
  16. '.gif',
  17. '.ico',
  18. '.tiff',
  19. ]
  20. const longestFileType = Math.max(...FILE_TYPES.map(fileType => fileType.length))
  21. async function main() {
  22. const projects = db.projects.find(
  23. {},
  24. {
  25. projection: { rootFolder: 1 },
  26. readPreference: READ_PREFERENCE_SECONDARY,
  27. }
  28. )
  29. let projectsProcessed = 0
  30. const result = new Map(FILE_TYPES.map(fileType => [fileType, 0]))
  31. for await (const project of projects) {
  32. projectsProcessed += 1
  33. if (projectsProcessed % 100000 === 0) {
  34. console.log(projectsProcessed, 'projects processed')
  35. }
  36. countFiles(project.rootFolder[0], result)
  37. }
  38. const sortedResults = [...result.entries()].sort(
  39. ([, countA], [, countB]) => countB - countA
  40. )
  41. sortedResults.forEach(([fileType, count]) => {
  42. console.log(
  43. `${fileType.padStart(longestFileType, ' ')}: ${count
  44. .toString()
  45. .padStart(7, ' ')}`
  46. )
  47. })
  48. }
  49. function countFiles(folder, result) {
  50. if (folder.folders) {
  51. for (const subfolder of folder.folders) {
  52. countFiles(subfolder, result)
  53. }
  54. }
  55. if (folder.fileRefs) {
  56. for (const file of folder.fileRefs) {
  57. const fileType = extname(file.name).toLowerCase()
  58. const current = result.get(fileType)
  59. if (current !== undefined) {
  60. result.set(fileType, current + 1)
  61. }
  62. }
  63. }
  64. return result
  65. }
  66. try {
  67. await scriptRunner(main)
  68. process.exit(0)
  69. } catch (error) {
  70. console.error(error)
  71. process.exit(1)
  72. }