| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- /* eslint-disable
- n/handle-callback-err,
- max-len,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- import OError from '@overleaf/o-error'
- import ProjectGetter from '../Project/ProjectGetter.mjs'
- import ProjectHistoryHandler from '../Project/ProjectHistoryHandler.mjs'
- import ProjectLocator from '../Project/ProjectLocator.mjs'
- import ProjectRootDocManager from '../Project/ProjectRootDocManager.mjs'
- import UserGetter from '../User/UserGetter.js'
- import logger from '@overleaf/logger'
- import settings from '@overleaf/settings'
- import async from 'async'
- import Request from 'request'
- let ExportsHandler
- const request = Request.defaults()
- export default ExportsHandler = {
- exportProject(exportParams, callback) {
- if (callback == null) {
- callback = function () {}
- }
- return ExportsHandler._buildExport(
- exportParams,
- function (err, exportData) {
- if (err != null) {
- return callback(err)
- }
- return ExportsHandler._requestExport(exportData, function (err, body) {
- if (err != null) {
- return callback(err)
- }
- exportData.v1_id = body.exportId
- exportData.message = body.message
- // TODO: possibly store the export data in Mongo
- return callback(null, exportData)
- })
- }
- )
- },
- _buildExport(exportParams, callback) {
- if (callback == null) {
- callback = function () {}
- }
- const {
- project_id: projectId,
- user_id: userId,
- brand_variation_id: brandVariationId,
- title,
- description,
- author,
- license,
- show_source: showSource,
- } = exportParams
- const jobs = {
- project(cb) {
- return ProjectGetter.getProject(projectId, cb)
- },
- rootDoc: [
- 'project',
- (results, cb) =>
- ProjectRootDocManager.ensureRootDocumentIsValid(
- projectId,
- function (error) {
- if (error != null) {
- return callback(error)
- }
- return ProjectLocator.findRootDoc(
- { project: results.project, project_id: projectId },
- cb
- )
- }
- ),
- ],
- user(cb) {
- return UserGetter.getUser(
- userId,
- { first_name: 1, last_name: 1, email: 1, overleaf: 1 },
- cb
- )
- },
- historyVersion(cb) {
- return ProjectHistoryHandler.ensureHistoryExistsForProject(
- projectId,
- function (error) {
- if (error != null) {
- return callback(error)
- }
- return ExportsHandler._requestVersion(projectId, cb)
- }
- )
- },
- }
- return async.auto(jobs, function (err, results) {
- if (err != null) {
- OError.tag(err, 'error building project export', {
- project_id: projectId,
- user_id: userId,
- brand_variation_id: brandVariationId,
- })
- return callback(err)
- }
- const { project, rootDoc, user, historyVersion } = results
- if (!rootDoc || rootDoc[1] == null) {
- err = new OError('cannot export project without root doc', {
- project_id: projectId,
- })
- return callback(err)
- }
- if (exportParams.first_name && exportParams.last_name) {
- user.first_name = exportParams.first_name
- user.last_name = exportParams.last_name
- }
- const exportData = {
- project: {
- id: projectId,
- rootDocPath: rootDoc[1] != null ? rootDoc[1].fileSystem : undefined,
- historyId: project.overleaf?.history?.id,
- historyVersion,
- v1ProjectId:
- project.overleaf != null ? project.overleaf.id : undefined,
- metadata: {
- compiler: project.compiler,
- imageName: project.imageName,
- title,
- description,
- author,
- license,
- showSource,
- },
- },
- user: {
- id: userId,
- firstName: user.first_name,
- lastName: user.last_name,
- email: user.email,
- orcidId: null, // until v2 gets ORCID
- v1UserId: user.overleaf != null ? user.overleaf.id : undefined,
- },
- destination: {
- brandVariationId,
- },
- options: {
- callbackUrl: null,
- }, // for now, until we want v1 to call us back
- }
- return callback(null, exportData)
- })
- },
- _requestExport(exportData, callback) {
- if (callback == null) {
- callback = function () {}
- }
- return request.post(
- {
- url: `${settings.apis.v1.url}/api/v1/overleaf/exports`,
- auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
- json: exportData,
- timeout: settings.apis.v1.timeout,
- },
- function (err, res, body) {
- if (err != null) {
- OError.tag(err, 'error making request to v1 export', {
- export: exportData,
- })
- return callback(err)
- } else if (res.statusCode >= 200 && res.statusCode < 300) {
- return callback(null, body)
- } else {
- logger.warn(
- { export: exportData },
- `v1 export returned failure; forwarding: ${body}`
- )
- // pass the v1 error along for the publish modal to handle
- const err = { forwardResponse: body }
- return callback(err)
- }
- }
- )
- },
- _requestVersion(projectId, callback) {
- if (callback == null) {
- callback = function () {}
- }
- return request.get(
- {
- url: `${settings.apis.project_history.url}/project/${projectId}/version`,
- json: true,
- },
- function (err, res, body) {
- if (err != null) {
- OError.tag(err, 'error making request to project history', {
- project_id: projectId,
- })
- return callback(err)
- } else if (res.statusCode >= 200 && res.statusCode < 300) {
- return callback(null, body.version)
- } else {
- err = new OError(
- `project history version returned a failure status code: ${res.statusCode}`,
- { project_id: projectId }
- )
- return callback(err)
- }
- }
- )
- },
- fetchExport(exportId, callback) {
- if (callback == null) {
- callback = function () {}
- }
- return request.get(
- {
- url: `${settings.apis.v1.url}/api/v1/overleaf/exports/${exportId}`,
- auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
- timeout: settings.apis.v1.timeout,
- },
- function (err, res, body) {
- if (err != null) {
- OError.tag(err, 'error making request to v1 export', {
- export: exportId,
- })
- return callback(err)
- } else if (res.statusCode >= 200 && res.statusCode < 300) {
- return callback(null, body)
- } else {
- err = new OError(
- `v1 export returned a failure status code: ${res.statusCode}`,
- { export: exportId }
- )
- return callback(err)
- }
- }
- )
- },
- fetchDownload(exportId, type, callback) {
- if (callback == null) {
- callback = function () {}
- }
- return request.get(
- {
- url: `${settings.apis.v1.url}/api/v1/overleaf/exports/${exportId}/${type}_url`,
- auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
- timeout: settings.apis.v1.timeout,
- },
- function (err, res, body) {
- if (err != null) {
- OError.tag(err, 'error making request to v1 export', {
- export: exportId,
- })
- return callback(err)
- } else if (res.statusCode >= 200 && res.statusCode < 300) {
- return callback(null, body)
- } else {
- err = new OError(
- `v1 export returned a failure status code: ${res.statusCode}`,
- { export: exportId }
- )
- return callback(err)
- }
- }
- )
- },
- }
|