ClsiFormatChecker.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* eslint-disable
  2. max-len,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. let ClsiFormatChecker
  13. const _ = require('lodash')
  14. const async = require('async')
  15. const settings = require('@overleaf/settings')
  16. module.exports = ClsiFormatChecker = {
  17. checkRecoursesForProblems(resources, callback) {
  18. const jobs = {
  19. conflictedPaths(cb) {
  20. return ClsiFormatChecker._checkForConflictingPaths(resources, cb)
  21. },
  22. sizeCheck(cb) {
  23. return ClsiFormatChecker._checkDocsAreUnderSizeLimit(resources, cb)
  24. },
  25. }
  26. return async.series(jobs, function (err, problems) {
  27. if (err != null) {
  28. return callback(err)
  29. }
  30. problems = _.omitBy(problems, _.isEmpty)
  31. if (_.isEmpty(problems)) {
  32. return callback()
  33. } else {
  34. return callback(null, problems)
  35. }
  36. })
  37. },
  38. _checkForConflictingPaths(resources, callback) {
  39. const paths = resources.map(resource => resource.path)
  40. const conflicts = _.filter(paths, function (path) {
  41. const matchingPaths = _.filter(
  42. paths,
  43. checkPath => checkPath.indexOf(path + '/') !== -1
  44. )
  45. return matchingPaths.length > 0
  46. })
  47. const conflictObjects = conflicts.map(conflict => ({ path: conflict }))
  48. return callback(null, conflictObjects)
  49. },
  50. _checkDocsAreUnderSizeLimit(resources, callback) {
  51. const sizeLimit = 1000 * 1000 * settings.compileBodySizeLimitMb
  52. let totalSize = 0
  53. let sizedResources = resources.map(function (resource) {
  54. const result = { path: resource.path }
  55. if (resource.content != null) {
  56. result.size = resource.content.replace(/\n/g, '').length
  57. result.kbSize = Math.ceil(result.size / 1000)
  58. } else {
  59. result.size = 0
  60. }
  61. totalSize += result.size
  62. return result
  63. })
  64. const tooLarge = totalSize > sizeLimit
  65. if (!tooLarge) {
  66. return callback()
  67. } else {
  68. sizedResources = _.sortBy(sizedResources, 'size').reverse().slice(0, 10)
  69. return callback(null, { resources: sizedResources, totalSize })
  70. }
  71. },
  72. }