CompileController.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let CompileController
  16. const RequestParser = require('./RequestParser')
  17. const CompileManager = require('./CompileManager')
  18. const Settings = require('@overleaf/settings')
  19. const Metrics = require('./Metrics')
  20. const ProjectPersistenceManager = require('./ProjectPersistenceManager')
  21. const logger = require('logger-sharelatex')
  22. const Errors = require('./Errors')
  23. function isImageNameAllowed(imageName) {
  24. const ALLOWED_IMAGES =
  25. Settings.clsi && Settings.clsi.docker && Settings.clsi.docker.allowedImages
  26. return !ALLOWED_IMAGES || ALLOWED_IMAGES.includes(imageName)
  27. }
  28. module.exports = CompileController = {
  29. compile(req, res, next) {
  30. if (next == null) {
  31. next = function (error) {}
  32. }
  33. const timer = new Metrics.Timer('compile-request')
  34. return RequestParser.parse(req.body, function (error, request) {
  35. if (error != null) {
  36. return next(error)
  37. }
  38. request.project_id = req.params.project_id
  39. if (req.params.user_id != null) {
  40. request.user_id = req.params.user_id
  41. }
  42. return ProjectPersistenceManager.markProjectAsJustAccessed(
  43. request.project_id,
  44. function (error) {
  45. if (error != null) {
  46. return next(error)
  47. }
  48. return CompileManager.doCompileWithLock(request, function (
  49. error,
  50. outputFiles,
  51. stats,
  52. timings
  53. ) {
  54. let code, status
  55. if (outputFiles == null) {
  56. outputFiles = []
  57. }
  58. if (error instanceof Errors.AlreadyCompilingError) {
  59. code = 423 // Http 423 Locked
  60. status = 'compile-in-progress'
  61. } else if (error instanceof Errors.FilesOutOfSyncError) {
  62. code = 409 // Http 409 Conflict
  63. status = 'retry'
  64. } else if (error && error.code === 'EPIPE') {
  65. // docker returns EPIPE when shutting down
  66. code = 503 // send 503 Unavailable response
  67. status = 'unavailable'
  68. } else if (error != null ? error.terminated : undefined) {
  69. status = 'terminated'
  70. } else if (error != null ? error.validate : undefined) {
  71. status = `validation-${error.validate}`
  72. } else if (error != null ? error.timedout : undefined) {
  73. status = 'timedout'
  74. logger.log(
  75. { err: error, project_id: request.project_id },
  76. 'timeout running compile'
  77. )
  78. } else if (error != null) {
  79. status = 'error'
  80. code = 500
  81. logger.warn(
  82. { err: error, project_id: request.project_id },
  83. 'error running compile'
  84. )
  85. } else {
  86. let file
  87. status = 'failure'
  88. for (file of Array.from(outputFiles)) {
  89. if (file.path === 'output.pdf' && file.size > 0) {
  90. status = 'success'
  91. }
  92. }
  93. if (status === 'failure') {
  94. logger.warn(
  95. { project_id: request.project_id, outputFiles },
  96. 'project failed to compile successfully, no output.pdf generated'
  97. )
  98. }
  99. // log an error if any core files are found
  100. for (file of Array.from(outputFiles)) {
  101. if (file.path === 'core') {
  102. logger.error(
  103. { project_id: request.project_id, req, outputFiles },
  104. 'core file found in output'
  105. )
  106. }
  107. }
  108. }
  109. if (error != null) {
  110. outputFiles = error.outputFiles || []
  111. }
  112. timer.done()
  113. return res.status(code || 200).send({
  114. compile: {
  115. status,
  116. error: (error != null ? error.message : undefined) || error,
  117. stats,
  118. timings,
  119. outputFiles: outputFiles.map((file) => {
  120. return {
  121. url:
  122. `${Settings.apis.clsi.url}/project/${request.project_id}` +
  123. (request.user_id != null
  124. ? `/user/${request.user_id}`
  125. : '') +
  126. (file.build != null ? `/build/${file.build}` : '') +
  127. `/output/${file.path}`,
  128. ...file
  129. }
  130. })
  131. }
  132. })
  133. })
  134. }
  135. )
  136. })
  137. },
  138. stopCompile(req, res, next) {
  139. const { project_id, user_id } = req.params
  140. return CompileManager.stopCompile(project_id, user_id, function (error) {
  141. if (error != null) {
  142. return next(error)
  143. }
  144. return res.sendStatus(204)
  145. })
  146. },
  147. clearCache(req, res, next) {
  148. if (next == null) {
  149. next = function (error) {}
  150. }
  151. return ProjectPersistenceManager.clearProject(
  152. req.params.project_id,
  153. req.params.user_id,
  154. function (error) {
  155. if (error != null) {
  156. return next(error)
  157. }
  158. return res.sendStatus(204)
  159. }
  160. )
  161. }, // No content
  162. syncFromCode(req, res, next) {
  163. if (next == null) {
  164. next = function (error) {}
  165. }
  166. const { file } = req.query
  167. const line = parseInt(req.query.line, 10)
  168. const column = parseInt(req.query.column, 10)
  169. const { imageName } = req.query
  170. const { project_id } = req.params
  171. const { user_id } = req.params
  172. if (imageName && !isImageNameAllowed(imageName)) {
  173. return res.status(400).send('invalid image')
  174. }
  175. return CompileManager.syncFromCode(
  176. project_id,
  177. user_id,
  178. file,
  179. line,
  180. column,
  181. imageName,
  182. function (error, pdfPositions) {
  183. if (error != null) {
  184. return next(error)
  185. }
  186. return res.json({
  187. pdf: pdfPositions
  188. })
  189. }
  190. )
  191. },
  192. syncFromPdf(req, res, next) {
  193. if (next == null) {
  194. next = function (error) {}
  195. }
  196. const page = parseInt(req.query.page, 10)
  197. const h = parseFloat(req.query.h)
  198. const v = parseFloat(req.query.v)
  199. const { imageName } = req.query
  200. const { project_id } = req.params
  201. const { user_id } = req.params
  202. if (imageName && !isImageNameAllowed(imageName)) {
  203. return res.status(400).send('invalid image')
  204. }
  205. return CompileManager.syncFromPdf(
  206. project_id,
  207. user_id,
  208. page,
  209. h,
  210. v,
  211. imageName,
  212. function (error, codePositions) {
  213. if (error != null) {
  214. return next(error)
  215. }
  216. return res.json({
  217. code: codePositions
  218. })
  219. }
  220. )
  221. },
  222. wordcount(req, res, next) {
  223. if (next == null) {
  224. next = function (error) {}
  225. }
  226. const file = req.query.file || 'main.tex'
  227. const { project_id } = req.params
  228. const { user_id } = req.params
  229. const { image } = req.query
  230. if (image && !isImageNameAllowed(image)) {
  231. return res.status(400).send('invalid image')
  232. }
  233. logger.log({ image, file, project_id }, 'word count request')
  234. return CompileManager.wordcount(project_id, user_id, file, image, function (
  235. error,
  236. result
  237. ) {
  238. if (error != null) {
  239. return next(error)
  240. }
  241. return res.json({
  242. texcount: result
  243. })
  244. })
  245. },
  246. status(req, res, next) {
  247. if (next == null) {
  248. next = function (error) {}
  249. }
  250. return res.send('OK')
  251. }
  252. }