ExportsHandler.mjs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  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. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. import OError from '@overleaf/o-error'
  15. import ProjectGetter from '../Project/ProjectGetter.mjs'
  16. import ProjectHistoryHandler from '../Project/ProjectHistoryHandler.mjs'
  17. import ProjectLocator from '../Project/ProjectLocator.mjs'
  18. import ProjectRootDocManager from '../Project/ProjectRootDocManager.mjs'
  19. import UserGetter from '../User/UserGetter.js'
  20. import logger from '@overleaf/logger'
  21. import settings from '@overleaf/settings'
  22. import async from 'async'
  23. import Request from 'request'
  24. let ExportsHandler
  25. const request = Request.defaults()
  26. export default ExportsHandler = {
  27. exportProject(exportParams, callback) {
  28. if (callback == null) {
  29. callback = function () {}
  30. }
  31. return ExportsHandler._buildExport(
  32. exportParams,
  33. function (err, exportData) {
  34. if (err != null) {
  35. return callback(err)
  36. }
  37. return ExportsHandler._requestExport(exportData, function (err, body) {
  38. if (err != null) {
  39. return callback(err)
  40. }
  41. exportData.v1_id = body.exportId
  42. exportData.message = body.message
  43. // TODO: possibly store the export data in Mongo
  44. return callback(null, exportData)
  45. })
  46. }
  47. )
  48. },
  49. _buildExport(exportParams, callback) {
  50. if (callback == null) {
  51. callback = function () {}
  52. }
  53. const {
  54. project_id: projectId,
  55. user_id: userId,
  56. brand_variation_id: brandVariationId,
  57. title,
  58. description,
  59. author,
  60. license,
  61. show_source: showSource,
  62. } = exportParams
  63. const jobs = {
  64. project(cb) {
  65. return ProjectGetter.getProject(projectId, cb)
  66. },
  67. rootDoc: [
  68. 'project',
  69. (results, cb) =>
  70. ProjectRootDocManager.ensureRootDocumentIsValid(
  71. projectId,
  72. function (error) {
  73. if (error != null) {
  74. return callback(error)
  75. }
  76. return ProjectLocator.findRootDoc(
  77. { project: results.project, project_id: projectId },
  78. cb
  79. )
  80. }
  81. ),
  82. ],
  83. user(cb) {
  84. return UserGetter.getUser(
  85. userId,
  86. { first_name: 1, last_name: 1, email: 1, overleaf: 1 },
  87. cb
  88. )
  89. },
  90. historyVersion(cb) {
  91. return ProjectHistoryHandler.ensureHistoryExistsForProject(
  92. projectId,
  93. function (error) {
  94. if (error != null) {
  95. return callback(error)
  96. }
  97. return ExportsHandler._requestVersion(projectId, cb)
  98. }
  99. )
  100. },
  101. }
  102. return async.auto(jobs, function (err, results) {
  103. if (err != null) {
  104. OError.tag(err, 'error building project export', {
  105. project_id: projectId,
  106. user_id: userId,
  107. brand_variation_id: brandVariationId,
  108. })
  109. return callback(err)
  110. }
  111. const { project, rootDoc, user, historyVersion } = results
  112. if (!rootDoc || rootDoc[1] == null) {
  113. err = new OError('cannot export project without root doc', {
  114. project_id: projectId,
  115. })
  116. return callback(err)
  117. }
  118. if (exportParams.first_name && exportParams.last_name) {
  119. user.first_name = exportParams.first_name
  120. user.last_name = exportParams.last_name
  121. }
  122. const exportData = {
  123. project: {
  124. id: projectId,
  125. rootDocPath: rootDoc[1] != null ? rootDoc[1].fileSystem : undefined,
  126. historyId: project.overleaf?.history?.id,
  127. historyVersion,
  128. v1ProjectId:
  129. project.overleaf != null ? project.overleaf.id : undefined,
  130. metadata: {
  131. compiler: project.compiler,
  132. imageName: project.imageName,
  133. title,
  134. description,
  135. author,
  136. license,
  137. showSource,
  138. },
  139. },
  140. user: {
  141. id: userId,
  142. firstName: user.first_name,
  143. lastName: user.last_name,
  144. email: user.email,
  145. orcidId: null, // until v2 gets ORCID
  146. v1UserId: user.overleaf != null ? user.overleaf.id : undefined,
  147. },
  148. destination: {
  149. brandVariationId,
  150. },
  151. options: {
  152. callbackUrl: null,
  153. }, // for now, until we want v1 to call us back
  154. }
  155. return callback(null, exportData)
  156. })
  157. },
  158. _requestExport(exportData, callback) {
  159. if (callback == null) {
  160. callback = function () {}
  161. }
  162. return request.post(
  163. {
  164. url: `${settings.apis.v1.url}/api/v1/overleaf/exports`,
  165. auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
  166. json: exportData,
  167. timeout: settings.apis.v1.timeout,
  168. },
  169. function (err, res, body) {
  170. if (err != null) {
  171. OError.tag(err, 'error making request to v1 export', {
  172. export: exportData,
  173. })
  174. return callback(err)
  175. } else if (res.statusCode >= 200 && res.statusCode < 300) {
  176. return callback(null, body)
  177. } else {
  178. logger.warn(
  179. { export: exportData },
  180. `v1 export returned failure; forwarding: ${body}`
  181. )
  182. // pass the v1 error along for the publish modal to handle
  183. const err = { forwardResponse: body }
  184. return callback(err)
  185. }
  186. }
  187. )
  188. },
  189. _requestVersion(projectId, callback) {
  190. if (callback == null) {
  191. callback = function () {}
  192. }
  193. return request.get(
  194. {
  195. url: `${settings.apis.project_history.url}/project/${projectId}/version`,
  196. json: true,
  197. },
  198. function (err, res, body) {
  199. if (err != null) {
  200. OError.tag(err, 'error making request to project history', {
  201. project_id: projectId,
  202. })
  203. return callback(err)
  204. } else if (res.statusCode >= 200 && res.statusCode < 300) {
  205. return callback(null, body.version)
  206. } else {
  207. err = new OError(
  208. `project history version returned a failure status code: ${res.statusCode}`,
  209. { project_id: projectId }
  210. )
  211. return callback(err)
  212. }
  213. }
  214. )
  215. },
  216. fetchExport(exportId, callback) {
  217. if (callback == null) {
  218. callback = function () {}
  219. }
  220. return request.get(
  221. {
  222. url: `${settings.apis.v1.url}/api/v1/overleaf/exports/${exportId}`,
  223. auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
  224. timeout: settings.apis.v1.timeout,
  225. },
  226. function (err, res, body) {
  227. if (err != null) {
  228. OError.tag(err, 'error making request to v1 export', {
  229. export: exportId,
  230. })
  231. return callback(err)
  232. } else if (res.statusCode >= 200 && res.statusCode < 300) {
  233. return callback(null, body)
  234. } else {
  235. err = new OError(
  236. `v1 export returned a failure status code: ${res.statusCode}`,
  237. { export: exportId }
  238. )
  239. return callback(err)
  240. }
  241. }
  242. )
  243. },
  244. fetchDownload(exportId, type, callback) {
  245. if (callback == null) {
  246. callback = function () {}
  247. }
  248. return request.get(
  249. {
  250. url: `${settings.apis.v1.url}/api/v1/overleaf/exports/${exportId}/${type}_url`,
  251. auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
  252. timeout: settings.apis.v1.timeout,
  253. },
  254. function (err, res, body) {
  255. if (err != null) {
  256. OError.tag(err, 'error making request to v1 export', {
  257. export: exportId,
  258. })
  259. return callback(err)
  260. } else if (res.statusCode >= 200 && res.statusCode < 300) {
  261. return callback(null, body)
  262. } else {
  263. err = new OError(
  264. `v1 export returned a failure status code: ${res.statusCode}`,
  265. { export: exportId }
  266. )
  267. return callback(err)
  268. }
  269. }
  270. )
  271. },
  272. }