ClsiFormatChecker.mjs 1.6 KB

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