2015-06-02 14:55:22 -04:00
|
|
|
MongoManager = require "./MongoManager"
|
|
|
|
Errors = require "./Errors"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
_ = require "underscore"
|
|
|
|
async = require "async"
|
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
request = require("request")
|
|
|
|
crypto = require("crypto")
|
|
|
|
thirtySeconds = 30 * 1000
|
|
|
|
|
|
|
|
module.exports = DocArchive =
|
|
|
|
|
2015-08-13 08:00:09 -04:00
|
|
|
archiveAllDocs: (project_id, callback = (err, docs) ->) ->
|
|
|
|
MongoManager.getProjectsDocs project_id, (err, docs) ->
|
|
|
|
if err?
|
|
|
|
return callback(err)
|
2015-06-02 14:55:22 -04:00
|
|
|
else if !docs?
|
|
|
|
return callback new Errors.NotFoundError("No docs for project #{project_id}")
|
|
|
|
jobs = _.map docs, (doc) ->
|
2015-08-13 08:00:09 -04:00
|
|
|
(cb)->
|
|
|
|
if doc.inS3
|
|
|
|
return cb()
|
|
|
|
else
|
|
|
|
DocArchive.archiveDoc project_id, doc, cb
|
2015-06-02 14:55:22 -04:00
|
|
|
async.series jobs, callback
|
|
|
|
|
|
|
|
|
|
|
|
archiveDoc: (project_id, doc, callback)->
|
|
|
|
logger.log project_id: project_id, doc_id: doc._id, "sending doc to s3"
|
2015-06-02 18:24:45 -04:00
|
|
|
options = DocArchive.buildS3Options(doc.lines, project_id+"/"+doc._id)
|
2015-06-02 14:55:22 -04:00
|
|
|
request.put options, (err, res)->
|
2015-08-13 08:00:09 -04:00
|
|
|
if err? || res.statusCode != 200
|
|
|
|
logger.err err:err, res:res, project_id:project_id, doc_id: doc._id, statusCode: res?.statusCode, "something went wrong archiving doc in aws"
|
|
|
|
return callback new Error("Error in S3 request")
|
2015-09-01 11:52:02 -04:00
|
|
|
md5lines = crypto.createHash("md5").update(JSON.stringify(doc.lines), "utf8").digest("hex")
|
2015-06-03 10:45:47 -04:00
|
|
|
md5response = res.headers.etag.toString().replace(/\"/g, '')
|
|
|
|
if md5lines != md5response
|
2015-08-13 08:59:12 -04:00
|
|
|
logger.err responseMD5:md5response, linesMD5:md5lines, project_id:project_id, doc_id: doc?._id, "err in response md5 from s3"
|
2015-08-13 08:00:09 -04:00
|
|
|
return callback new Error("Error in S3 md5 response")
|
|
|
|
MongoManager.markDocAsArchived doc._id, doc.rev, (err) ->
|
|
|
|
return callback(err) if err?
|
2015-06-02 14:55:22 -04:00
|
|
|
callback()
|
|
|
|
|
2015-08-13 08:00:09 -04:00
|
|
|
unArchiveAllDocs: (project_id, callback = (err) ->) ->
|
|
|
|
MongoManager.getArchivedProjectDocs project_id, (err, docs) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "error unarchiving all docs"
|
|
|
|
return callback(err)
|
2015-06-02 14:55:22 -04:00
|
|
|
else if !docs?
|
|
|
|
return callback new Errors.NotFoundError("No docs for project #{project_id}")
|
|
|
|
jobs = _.map docs, (doc) ->
|
|
|
|
(cb)->
|
|
|
|
if !doc.inS3?
|
|
|
|
return cb()
|
|
|
|
else
|
|
|
|
DocArchive.unarchiveDoc project_id, doc._id, cb
|
|
|
|
async.series jobs, callback
|
|
|
|
|
|
|
|
unarchiveDoc: (project_id, doc_id, callback)->
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "getting doc from s3"
|
2015-06-02 18:24:45 -04:00
|
|
|
options = DocArchive.buildS3Options(true, project_id+"/"+doc_id)
|
2015-06-02 14:55:22 -04:00
|
|
|
request.get options, (err, res, lines)->
|
|
|
|
if err? || res.statusCode != 200
|
2015-08-13 08:59:12 -04:00
|
|
|
logger.err err:err, res:res, project_id:project_id, doc_id:doc_id, "something went wrong unarchiving doc from aws"
|
2015-06-02 19:08:50 -04:00
|
|
|
return callback new Errors.NotFoundError("Error in S3 request")
|
2015-08-13 08:00:09 -04:00
|
|
|
MongoManager.upsertIntoDocCollection project_id, doc_id.toString(), lines, (err) ->
|
|
|
|
return callback(err) if err?
|
2015-06-03 19:05:15 -04:00
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "deleting doc from s3"
|
|
|
|
request.del options, (err, res, body)->
|
|
|
|
if err? || res.statusCode != 204
|
2015-08-13 08:59:12 -04:00
|
|
|
logger.err err:err, res:res, project_id:project_id, doc_id:doc_id, "something went wrong deleting doc from aws"
|
2015-06-03 19:05:15 -04:00
|
|
|
return callback new Errors.NotFoundError("Error in S3 request")
|
|
|
|
callback()
|
2015-06-02 14:55:22 -04:00
|
|
|
|
2015-06-02 18:24:45 -04:00
|
|
|
buildS3Options: (content, key)->
|
|
|
|
return {
|
|
|
|
aws:
|
2015-08-13 08:00:09 -04:00
|
|
|
key: settings.docstore.s3.key
|
|
|
|
secret: settings.docstore.s3.secret
|
|
|
|
bucket: settings.docstore.s3.bucket
|
2015-06-02 18:24:45 -04:00
|
|
|
timeout: thirtySeconds
|
|
|
|
json: content
|
2015-08-13 08:00:09 -04:00
|
|
|
uri:"https://#{settings.docstore.s3.bucket}.s3.amazonaws.com/#{key}"
|
2015-06-02 18:24:45 -04:00
|
|
|
}
|