2018-03-16 06:25:40 -04:00
|
|
|
ProjectGetter = require('../Project/ProjectGetter')
|
2018-11-01 13:34:31 -04:00
|
|
|
ProjectHistoryHandler = require('../Project/ProjectHistoryHandler')
|
2018-03-16 06:25:40 -04:00
|
|
|
ProjectLocator = require('../Project/ProjectLocator')
|
2018-10-29 10:48:23 -04:00
|
|
|
ProjectRootDocManager = require('../Project/ProjectRootDocManager')
|
2018-03-16 06:25:40 -04:00
|
|
|
UserGetter = require('../User/UserGetter')
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
settings = require 'settings-sharelatex'
|
|
|
|
async = require 'async'
|
|
|
|
request = require 'request'
|
|
|
|
request = request.defaults()
|
|
|
|
settings = require 'settings-sharelatex'
|
|
|
|
|
|
|
|
module.exports = ExportsHandler = self =
|
|
|
|
|
2018-06-04 06:05:47 -04:00
|
|
|
exportProject: (export_params, callback=(error, export_data) ->) ->
|
|
|
|
self._buildExport export_params, (err, export_data) ->
|
2018-03-16 06:25:40 -04:00
|
|
|
return callback(err) if err?
|
|
|
|
self._requestExport export_data, (err, export_v1_id) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
export_data.v1_id = export_v1_id
|
|
|
|
# TODO: possibly store the export data in Mongo
|
|
|
|
callback null, export_data
|
|
|
|
|
2018-06-04 06:05:47 -04:00
|
|
|
_buildExport: (export_params, callback=(err, export_data) ->) ->
|
2018-10-29 10:48:23 -04:00
|
|
|
{project_id, user_id, brand_variation_id, title, description, author,
|
|
|
|
license, show_source} = export_params
|
2018-03-16 06:25:40 -04:00
|
|
|
jobs =
|
|
|
|
project: (cb) ->
|
|
|
|
ProjectGetter.getProject project_id, cb
|
|
|
|
# TODO: when we update async, signature will change from (cb, results) to (results, cb)
|
|
|
|
rootDoc: [ 'project', (cb, results) ->
|
2018-11-09 03:09:13 -05:00
|
|
|
ProjectRootDocManager.ensureRootDocumentIsValid project_id, (error) ->
|
2018-10-29 10:48:23 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
ProjectLocator.findRootDoc {project: results.project, project_id: project_id}, cb
|
2018-03-16 06:25:40 -04:00
|
|
|
]
|
|
|
|
user: (cb) ->
|
2018-07-03 05:15:51 -04:00
|
|
|
UserGetter.getUser user_id, {first_name: 1, last_name: 1, email: 1, overleaf: 1}, cb
|
2018-03-16 06:25:40 -04:00
|
|
|
historyVersion: (cb) ->
|
2018-11-01 13:34:31 -04:00
|
|
|
ProjectHistoryHandler.ensureHistoryExistsForProject project_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
self._requestVersion project_id, cb
|
2018-03-16 06:25:40 -04:00
|
|
|
|
|
|
|
async.auto jobs, (err, results) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, user_id:user_id, brand_variation_id:brand_variation_id, "error building project export"
|
|
|
|
return callback(err)
|
|
|
|
|
|
|
|
{project, rootDoc, user, historyVersion} = results
|
|
|
|
if !rootDoc[1]?
|
|
|
|
err = new Error("cannot export project without root doc")
|
|
|
|
logger.err err:err, project_id: project_id
|
|
|
|
return callback(err)
|
|
|
|
|
2018-06-04 06:05:47 -04:00
|
|
|
if export_params.first_name && export_params.last_name
|
|
|
|
user.first_name = export_params.first_name
|
|
|
|
user.last_name = export_params.last_name
|
|
|
|
|
2018-03-16 06:25:40 -04:00
|
|
|
export_data =
|
|
|
|
project:
|
|
|
|
id: project_id
|
|
|
|
rootDocPath: rootDoc[1]?.fileSystem
|
|
|
|
historyId: project.overleaf?.history?.id
|
|
|
|
historyVersion: historyVersion
|
2018-07-02 06:12:42 -04:00
|
|
|
v1ProjectId: project.overleaf?.id
|
2018-07-27 10:42:37 -04:00
|
|
|
metadata:
|
|
|
|
compiler: project.compiler
|
|
|
|
imageName: project.imageName
|
2018-09-13 07:14:06 -04:00
|
|
|
title: title
|
|
|
|
description: description
|
|
|
|
author: author
|
|
|
|
license: license
|
2018-10-29 10:48:23 -04:00
|
|
|
showSource: show_source
|
2018-03-16 06:25:40 -04:00
|
|
|
user:
|
|
|
|
id: user_id
|
|
|
|
firstName: user.first_name
|
|
|
|
lastName: user.last_name
|
|
|
|
email: user.email
|
|
|
|
orcidId: null # until v2 gets ORCID
|
2018-07-03 05:15:51 -04:00
|
|
|
v1UserId: user.overleaf?.id
|
2018-03-16 06:25:40 -04:00
|
|
|
destination:
|
|
|
|
brandVariationId: brand_variation_id
|
|
|
|
options:
|
|
|
|
callbackUrl: null # for now, until we want v1 to call us back
|
|
|
|
callback null, export_data
|
|
|
|
|
|
|
|
_requestExport: (export_data, callback=(err, export_v1_id) ->) ->
|
|
|
|
request.post {
|
|
|
|
url: "#{settings.apis.v1.url}/api/v1/sharelatex/exports"
|
|
|
|
auth: {user: settings.apis.v1.user, pass: settings.apis.v1.pass }
|
|
|
|
json: export_data
|
|
|
|
}, (err, res, body) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, export:export_data, "error making request to v1 export"
|
|
|
|
callback err
|
|
|
|
else if 200 <= res.statusCode < 300
|
|
|
|
callback null, body.exportId
|
|
|
|
else
|
|
|
|
err = new Error("v1 export returned a failure status code: #{res.statusCode}")
|
|
|
|
logger.err err:err, export:export_data, "v1 export returned failure status code: #{res.statusCode}"
|
|
|
|
callback err
|
|
|
|
|
|
|
|
_requestVersion: (project_id, callback=(err, export_v1_id) ->) ->
|
|
|
|
request.get {
|
|
|
|
url: "#{settings.apis.project_history.url}/project/#{project_id}/version"
|
|
|
|
json: true
|
|
|
|
}, (err, res, body) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "error making request to project history"
|
|
|
|
callback err
|
|
|
|
else if res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
callback null, body.version
|
|
|
|
else
|
|
|
|
err = new Error("project history version returned a failure status code: #{res.statusCode}")
|
|
|
|
logger.err err:err, project_id:project_id, "project history version returned failure status code: #{res.statusCode}"
|
|
|
|
callback err
|
2018-06-09 07:02:23 -04:00
|
|
|
|
|
|
|
fetchExport: (export_id, callback=(err, export_json) ->) ->
|
|
|
|
request.get {
|
|
|
|
url: "#{settings.apis.v1.url}/api/v1/sharelatex/exports/#{export_id}"
|
|
|
|
auth: {user: settings.apis.v1.user, pass: settings.apis.v1.pass }
|
|
|
|
}, (err, res, body) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, export:export_id, "error making request to v1 export"
|
|
|
|
callback err
|
|
|
|
else if 200 <= res.statusCode < 300
|
|
|
|
callback null, body
|
|
|
|
else
|
|
|
|
err = new Error("v1 export returned a failure status code: #{res.statusCode}")
|
|
|
|
logger.err err:err, export:export_id, "v1 export returned failure status code: #{res.statusCode}"
|
|
|
|
callback err
|
2018-07-30 14:14:59 -04:00
|
|
|
|
2018-09-27 11:11:11 -04:00
|
|
|
fetchDownload: (export_id, type, callback=(err, file_url) ->) ->
|
2018-07-30 14:14:59 -04:00
|
|
|
request.get {
|
2018-09-27 11:11:11 -04:00
|
|
|
url: "#{settings.apis.v1.url}/api/v1/sharelatex/exports/#{export_id}/#{type}_url"
|
2018-07-30 14:14:59 -04:00
|
|
|
auth: {user: settings.apis.v1.user, pass: settings.apis.v1.pass }
|
|
|
|
}, (err, res, body) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, export:export_id, "error making request to v1 export"
|
|
|
|
callback err
|
|
|
|
else if 200 <= res.statusCode < 300
|
|
|
|
callback null, body
|
|
|
|
else
|
|
|
|
err = new Error("v1 export returned a failure status code: #{res.statusCode}")
|
|
|
|
logger.err err:err, export:export_id, "v1 export zip fetch returned failure status code: #{res.statusCode}"
|
|
|
|
callback err
|