DocumentController.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* eslint-disable
  2. camelcase,
  3. node/handle-callback-err,
  4. max-len,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const ProjectGetter = require('../Project/ProjectGetter')
  16. const OError = require('@overleaf/o-error')
  17. const ProjectLocator = require('../Project/ProjectLocator')
  18. const ProjectEntityHandler = require('../Project/ProjectEntityHandler')
  19. const ProjectEntityUpdateHandler = require('../Project/ProjectEntityUpdateHandler')
  20. const logger = require('logger-sharelatex')
  21. const _ = require('lodash')
  22. module.exports = {
  23. getDocument(req, res, next) {
  24. if (next == null) {
  25. next = function (error) {}
  26. }
  27. const project_id = req.params.Project_id
  28. const { doc_id } = req.params
  29. const plain =
  30. __guard__(req != null ? req.query : undefined, x => x.plain) === 'true'
  31. return ProjectGetter.getProject(
  32. project_id,
  33. { rootFolder: true, overleaf: true },
  34. function (error, project) {
  35. if (error != null) {
  36. return next(error)
  37. }
  38. if (project == null) {
  39. return res.sendStatus(404)
  40. }
  41. return ProjectLocator.findElement(
  42. { project, element_id: doc_id, type: 'doc' },
  43. function (error, doc, path) {
  44. if (error != null) {
  45. OError.tag(error, 'error finding element for getDocument', {
  46. doc_id,
  47. project_id
  48. })
  49. return next(error)
  50. }
  51. return ProjectEntityHandler.getDoc(
  52. project_id,
  53. doc_id,
  54. function (error, lines, rev, version, ranges) {
  55. if (error != null) {
  56. OError.tag(
  57. error,
  58. 'error finding doc contents for getDocument',
  59. {
  60. doc_id,
  61. project_id
  62. }
  63. )
  64. return next(error)
  65. }
  66. if (plain) {
  67. res.type('text/plain')
  68. return res.send(lines.join('\n'))
  69. } else {
  70. const projectHistoryId = _.get(project, 'overleaf.history.id')
  71. const projectHistoryType = _.get(
  72. project,
  73. 'overleaf.history.display'
  74. )
  75. ? 'project-history'
  76. : undefined // for backwards compatibility, don't send anything if the project is still on track-changes
  77. return res.json({
  78. lines,
  79. version,
  80. ranges,
  81. pathname: path.fileSystem,
  82. projectHistoryId,
  83. projectHistoryType
  84. })
  85. }
  86. }
  87. )
  88. }
  89. )
  90. }
  91. )
  92. },
  93. setDocument(req, res, next) {
  94. if (next == null) {
  95. next = function (error) {}
  96. }
  97. const project_id = req.params.Project_id
  98. const { doc_id } = req.params
  99. const { lines, version, ranges, lastUpdatedAt, lastUpdatedBy } = req.body
  100. return ProjectEntityUpdateHandler.updateDocLines(
  101. project_id,
  102. doc_id,
  103. lines,
  104. version,
  105. ranges,
  106. lastUpdatedAt,
  107. lastUpdatedBy,
  108. function (error) {
  109. if (error != null) {
  110. OError.tag(error, 'error finding element for getDocument', {
  111. doc_id,
  112. project_id
  113. })
  114. return next(error)
  115. }
  116. logger.log(
  117. { doc_id, project_id },
  118. 'finished receiving set document request from api (docupdater)'
  119. )
  120. return res.sendStatus(200)
  121. }
  122. )
  123. }
  124. }
  125. function __guard__(value, transform) {
  126. return typeof value !== 'undefined' && value !== null
  127. ? transform(value)
  128. : undefined
  129. }