ClsiFormatChecker.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. const { promisifyAll } = require('@overleaf/promise-utils')
  17. module.exports = ClsiFormatChecker = {
  18. checkRecoursesForProblems(resources, callback) {
  19. const jobs = {
  20. conflictedPaths(cb) {
  21. return ClsiFormatChecker._checkForConflictingPaths(resources, cb)
  22. },
  23. sizeCheck(cb) {
  24. return ClsiFormatChecker._checkDocsAreUnderSizeLimit(resources, cb)
  25. },
  26. }
  27. return async.series(jobs, function (err, problems) {
  28. if (err != null) {
  29. return callback(err)
  30. }
  31. problems = _.omitBy(problems, _.isEmpty)
  32. if (_.isEmpty(problems)) {
  33. return callback()
  34. } else {
  35. return callback(null, problems)
  36. }
  37. })
  38. },
  39. _checkForConflictingPaths(resources, callback) {
  40. const paths = resources.map(resource => resource.path)
  41. const conflicts = _.filter(paths, function (path) {
  42. const matchingPaths = _.filter(
  43. paths,
  44. checkPath => checkPath.indexOf(path + '/') !== -1
  45. )
  46. return matchingPaths.length > 0
  47. })
  48. const conflictObjects = conflicts.map(conflict => ({ path: conflict }))
  49. return callback(null, conflictObjects)
  50. },
  51. _checkDocsAreUnderSizeLimit(resources, callback) {
  52. const sizeLimit = 1000 * 1000 * settings.compileBodySizeLimitMb
  53. let totalSize = 0
  54. let sizedResources = resources.map(function (resource) {
  55. const result = { path: resource.path }
  56. if (resource.content != null) {
  57. result.size = resource.content.replace(/\n/g, '').length
  58. result.kbSize = Math.ceil(result.size / 1000)
  59. } else {
  60. result.size = 0
  61. }
  62. totalSize += result.size
  63. return result
  64. })
  65. const tooLarge = totalSize > sizeLimit
  66. if (!tooLarge) {
  67. return callback()
  68. } else {
  69. sizedResources = _.sortBy(sizedResources, 'size').reverse().slice(0, 10)
  70. return callback(null, { resources: sizedResources, totalSize })
  71. }
  72. },
  73. }
  74. module.exports.promises = promisifyAll(module.exports)