ClsiFormatChecker.coffee 1.6 KB

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