TemplatesManager.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const { Project } = require('../../models/Project')
  14. const OError = require('@overleaf/o-error')
  15. const ProjectDetailsHandler = require('../Project/ProjectDetailsHandler')
  16. const ProjectOptionsHandler = require('../Project/ProjectOptionsHandler')
  17. const ProjectRootDocManager = require('../Project/ProjectRootDocManager')
  18. const ProjectUploadManager = require('../Uploads/ProjectUploadManager')
  19. const FileWriter = require('../../infrastructure/FileWriter')
  20. const async = require('async')
  21. const fs = require('fs')
  22. const util = require('util')
  23. const logger = require('logger-sharelatex')
  24. const request = require('request')
  25. const requestPromise = require('request-promise-native')
  26. const settings = require('settings-sharelatex')
  27. const uuid = require('uuid')
  28. const Errors = require('../Errors/Errors')
  29. const _ = require('underscore')
  30. const TemplatesManager = {
  31. createProjectFromV1Template(
  32. brandVariationId,
  33. compiler,
  34. mainFile,
  35. templateId,
  36. templateName,
  37. templateVersionId,
  38. user_id,
  39. imageName,
  40. _callback
  41. ) {
  42. const callback = _.once(_callback)
  43. const zipUrl = `${settings.apis.v1.url}/api/v1/sharelatex/templates/${templateVersionId}`
  44. const zipReq = request(zipUrl, {
  45. auth: {
  46. user: settings.apis.v1.user,
  47. pass: settings.apis.v1.pass,
  48. },
  49. timeout: 60 * 1000,
  50. })
  51. zipReq.on('error', function (err) {
  52. logger.warn({ err }, 'error getting zip from template API')
  53. return callback(err)
  54. })
  55. return FileWriter.ensureDumpFolderExists(function (err) {
  56. if (err != null) {
  57. return callback(err)
  58. }
  59. const projectName = ProjectDetailsHandler.fixProjectName(templateName)
  60. const dumpPath = `${settings.path.dumpFolder}/${uuid.v4()}`
  61. const writeStream = fs.createWriteStream(dumpPath)
  62. const attributes = {
  63. fromV1TemplateId: templateId,
  64. fromV1TemplateVersionId: templateVersionId,
  65. }
  66. writeStream.on('close', function () {
  67. if (zipReq.response.statusCode !== 200) {
  68. logger.warn(
  69. { uri: zipUrl, statusCode: zipReq.response.statusCode },
  70. 'non-success code getting zip from template API'
  71. )
  72. return callback(new Error('get zip failed'))
  73. }
  74. return ProjectUploadManager.createProjectFromZipArchiveWithName(
  75. user_id,
  76. projectName,
  77. dumpPath,
  78. attributes,
  79. function (err, project) {
  80. if (err != null) {
  81. OError.tag(err, 'problem building project from zip', {
  82. zipReq,
  83. })
  84. return callback(err)
  85. }
  86. return async.series(
  87. [
  88. cb => TemplatesManager._setCompiler(project._id, compiler, cb),
  89. cb => TemplatesManager._setImage(project._id, imageName, cb),
  90. cb => TemplatesManager._setMainFile(project._id, mainFile, cb),
  91. cb =>
  92. TemplatesManager._setBrandVariationId(
  93. project._id,
  94. brandVariationId,
  95. cb
  96. ),
  97. ],
  98. function (err) {
  99. if (err != null) {
  100. return callback(err)
  101. }
  102. fs.unlink(dumpPath, function (err) {
  103. if (err != null) {
  104. return logger.err({ err }, 'error unlinking template zip')
  105. }
  106. })
  107. const update = {
  108. fromV1TemplateId: templateId,
  109. fromV1TemplateVersionId: templateVersionId,
  110. }
  111. return Project.updateOne(
  112. { _id: project._id },
  113. update,
  114. {},
  115. function (err) {
  116. if (err != null) {
  117. return callback(err)
  118. }
  119. return callback(null, project)
  120. }
  121. )
  122. }
  123. )
  124. }
  125. )
  126. })
  127. return zipReq.pipe(writeStream)
  128. })
  129. },
  130. _setCompiler(project_id, compiler, callback) {
  131. if (compiler == null) {
  132. return callback()
  133. }
  134. return ProjectOptionsHandler.setCompiler(project_id, compiler, callback)
  135. },
  136. _setImage(project_id, imageName, callback) {
  137. if (!imageName) {
  138. imageName = 'wl_texlive:2018.1'
  139. }
  140. return ProjectOptionsHandler.setImageName(project_id, imageName, callback)
  141. },
  142. _setMainFile(project_id, mainFile, callback) {
  143. if (mainFile == null) {
  144. return callback()
  145. }
  146. return ProjectRootDocManager.setRootDocFromName(
  147. project_id,
  148. mainFile,
  149. callback
  150. )
  151. },
  152. _setBrandVariationId(project_id, brandVariationId, callback) {
  153. if (brandVariationId == null) {
  154. return callback()
  155. }
  156. return ProjectOptionsHandler.setBrandVariationId(
  157. project_id,
  158. brandVariationId,
  159. callback
  160. )
  161. },
  162. promises: {
  163. async fetchFromV1(templateId) {
  164. const { body, statusCode } = await requestPromise({
  165. baseUrl: settings.apis.v1.url,
  166. url: `/api/v2/templates/${templateId}`,
  167. method: 'GET',
  168. auth: {
  169. user: settings.apis.v1.user,
  170. pass: settings.apis.v1.pass,
  171. sendImmediately: true,
  172. },
  173. resolveWithFullResponse: true,
  174. simple: false,
  175. json: true,
  176. timeout: 60 * 1000,
  177. })
  178. if (statusCode === 404) {
  179. throw new Errors.NotFoundError()
  180. }
  181. if (statusCode !== 200) {
  182. logger.warn(
  183. { templateId },
  184. "[TemplateMetrics] Couldn't fetch template data from v1"
  185. )
  186. throw new Error("Couldn't fetch template data from v1")
  187. }
  188. return body
  189. },
  190. },
  191. }
  192. TemplatesManager.fetchFromV1 = util.callbackify(
  193. TemplatesManager.promises.fetchFromV1
  194. )
  195. module.exports = TemplatesManager