ClsiFormatChecker.coffee 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. _ = require("lodash")
  2. async = require("async")
  3. module.exports = ClsiFormatChecker =
  4. checkRecoursesForProblems: (resources, callback)->
  5. jobs =
  6. conflictedPaths: (cb)->
  7. ClsiFormatChecker._checkForConflictingPaths resources, cb
  8. sizeCheck: (cb)->
  9. ClsiFormatChecker._checkDocsAreUnderSizeLimit resources, cb
  10. async.series jobs, (err, problems)->
  11. if err?
  12. return callback(err)
  13. problems = _.omitBy(problems, _.isEmpty)
  14. if _.isEmpty(problems)
  15. return callback()
  16. else
  17. callback(null, problems)
  18. _checkForConflictingPaths: (resources, callback)->
  19. paths = _.map(resources, 'path')
  20. conflicts = _.filter paths, (path)->
  21. matchingPaths = _.filter paths, (checkPath)->
  22. return checkPath.indexOf(path+"/") != -1
  23. return matchingPaths.length > 0
  24. conflictObjects = _.map conflicts, (conflict)->
  25. path:conflict
  26. callback null, conflictObjects
  27. _checkDocsAreUnderSizeLimit: (resources, callback)->
  28. FIVEMB = 1000 * 1000 * 5
  29. totalSize = 0
  30. sizedResources = _.map resources, (resource)->
  31. result = {path:resource.path}
  32. if resource.content?
  33. result.size = resource.content.replace(/\n/g).length
  34. result.kbSize = Math.ceil(result.size / 1000)
  35. else
  36. result.size = 0
  37. totalSize += result.size
  38. return result
  39. tooLarge = totalSize > FIVEMB
  40. if !tooLarge
  41. return callback()
  42. else
  43. sizedResources = _.sortBy(sizedResources, "size").reverse().slice(0, 10)
  44. return callback(null, {resources:sizedResources, totalSize:totalSize})