DocArchiveManager.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-useless-escape,
  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. let DocArchive;
  15. const MongoManager = require("./MongoManager");
  16. const Errors = require("./Errors");
  17. const logger = require("logger-sharelatex");
  18. const _ = require("underscore");
  19. const async = require("async");
  20. const settings = require("settings-sharelatex");
  21. const request = require("request");
  22. const crypto = require("crypto");
  23. const RangeManager = require("./RangeManager");
  24. const thirtySeconds = 30 * 1000;
  25. module.exports = (DocArchive = {
  26. archiveAllDocs(project_id, callback) {
  27. if (callback == null) { callback = function(err, docs) {}; }
  28. return MongoManager.getProjectsDocs(project_id, {include_deleted: true}, {lines: true, ranges: true, rev: true, inS3: true}, function(err, docs) {
  29. if (err != null) {
  30. return callback(err);
  31. } else if ((docs == null)) {
  32. return callback(new Errors.NotFoundError(`No docs for project ${project_id}`));
  33. }
  34. docs = _.filter(docs, doc => doc.inS3 !== true);
  35. const jobs = _.map(docs, doc => cb => DocArchive.archiveDoc(project_id, doc, cb));
  36. return async.parallelLimit(jobs, 5, callback);
  37. });
  38. },
  39. archiveDoc(project_id, doc, callback){
  40. let options;
  41. logger.log({project_id, doc_id: doc._id}, "sending doc to s3");
  42. try {
  43. options = DocArchive.buildS3Options(project_id+"/"+doc._id);
  44. } catch (e) {
  45. return callback(e);
  46. }
  47. return DocArchive._mongoDocToS3Doc(doc, function(error, json_doc) {
  48. if (error != null) { return callback(error); }
  49. options.body = json_doc;
  50. options.headers =
  51. {'Content-Type': "application/json"};
  52. return request.put(options, function(err, res) {
  53. if ((err != null) || (res.statusCode !== 200)) {
  54. logger.err({err, res, project_id, doc_id: doc._id, statusCode: (res != null ? res.statusCode : undefined)}, "something went wrong archiving doc in aws");
  55. return callback(new Error("Error in S3 request"));
  56. }
  57. const md5lines = crypto.createHash("md5").update(json_doc, "utf8").digest("hex");
  58. const md5response = res.headers.etag.toString().replace(/\"/g, '');
  59. if (md5lines !== md5response) {
  60. logger.err({responseMD5:md5response, linesMD5:md5lines, project_id, doc_id: (doc != null ? doc._id : undefined)}, "err in response md5 from s3");
  61. return callback(new Error("Error in S3 md5 response"));
  62. }
  63. return MongoManager.markDocAsArchived(doc._id, doc.rev, function(err) {
  64. if (err != null) { return callback(err); }
  65. return callback();
  66. });
  67. });
  68. });
  69. },
  70. unArchiveAllDocs(project_id, callback) {
  71. if (callback == null) { callback = function(err) {}; }
  72. return MongoManager.getArchivedProjectDocs(project_id, function(err, docs) {
  73. if (err != null) {
  74. logger.err({err, project_id}, "error unarchiving all docs");
  75. return callback(err);
  76. } else if ((docs == null)) {
  77. return callback(new Errors.NotFoundError(`No docs for project ${project_id}`));
  78. }
  79. const jobs = _.map(docs, doc => (function(cb) {
  80. if ((doc.inS3 == null)) {
  81. return cb();
  82. } else {
  83. return DocArchive.unarchiveDoc(project_id, doc._id, cb);
  84. }
  85. }));
  86. return async.parallelLimit(jobs, 5, callback);
  87. });
  88. },
  89. unarchiveDoc(project_id, doc_id, callback){
  90. let options;
  91. logger.log({project_id, doc_id}, "getting doc from s3");
  92. try {
  93. options = DocArchive.buildS3Options(project_id+"/"+doc_id);
  94. } catch (e) {
  95. return callback(e);
  96. }
  97. options.json = true;
  98. return request.get(options, function(err, res, doc){
  99. if ((err != null) || (res.statusCode !== 200)) {
  100. logger.err({err, res, project_id, doc_id}, "something went wrong unarchiving doc from aws");
  101. return callback(new Errors.NotFoundError("Error in S3 request"));
  102. }
  103. return DocArchive._s3DocToMongoDoc(doc, function(error, mongo_doc) {
  104. if (error != null) { return callback(error); }
  105. return MongoManager.upsertIntoDocCollection(project_id, doc_id.toString(), mongo_doc, function(err) {
  106. if (err != null) { return callback(err); }
  107. logger.log({project_id, doc_id}, "deleting doc from s3");
  108. return DocArchive._deleteDocFromS3(project_id, doc_id, callback);
  109. });
  110. });
  111. });
  112. },
  113. destroyAllDocs(project_id, callback) {
  114. if (callback == null) { callback = function(err) {}; }
  115. return MongoManager.getProjectsDocs(project_id, {include_deleted: true}, {_id: 1}, function(err, docs) {
  116. if (err != null) {
  117. logger.err({err, project_id}, "error getting project's docs");
  118. return callback(err);
  119. } else if ((docs == null)) {
  120. return callback();
  121. }
  122. const jobs = _.map(docs, doc => cb => DocArchive.destroyDoc(project_id, doc._id, cb));
  123. return async.parallelLimit(jobs, 5, callback);
  124. });
  125. },
  126. destroyDoc(project_id, doc_id, callback){
  127. logger.log({project_id, doc_id}, "removing doc from mongo and s3");
  128. return MongoManager.findDoc(project_id, doc_id, {inS3: 1}, function(error, doc) {
  129. if (error != null) { return callback(error); }
  130. if (doc == null) { return callback(new Errors.NotFoundError("Doc not found in Mongo")); }
  131. if (doc.inS3 === true) {
  132. return DocArchive._deleteDocFromS3(project_id, doc_id, function(err) {
  133. if (err != null) { return err; }
  134. return MongoManager.destroyDoc(doc_id, callback);
  135. });
  136. } else {
  137. return MongoManager.destroyDoc(doc_id, callback);
  138. }
  139. });
  140. },
  141. _deleteDocFromS3(project_id, doc_id, callback) {
  142. let options;
  143. try {
  144. options = DocArchive.buildS3Options(project_id+"/"+doc_id);
  145. } catch (e) {
  146. return callback(e);
  147. }
  148. options.json = true;
  149. return request.del(options, function(err, res, body){
  150. if ((err != null) || (res.statusCode !== 204)) {
  151. logger.err({err, res, project_id, doc_id}, "something went wrong deleting doc from aws");
  152. return callback(new Error("Error in S3 request"));
  153. }
  154. return callback();
  155. });
  156. },
  157. _s3DocToMongoDoc(doc, callback) {
  158. if (callback == null) { callback = function(error, mongo_doc) {}; }
  159. const mongo_doc = {};
  160. if ((doc.schema_v === 1) && (doc.lines != null)) {
  161. mongo_doc.lines = doc.lines;
  162. if (doc.ranges != null) {
  163. mongo_doc.ranges = RangeManager.jsonRangesToMongo(doc.ranges);
  164. }
  165. } else if (doc instanceof Array) {
  166. mongo_doc.lines = doc;
  167. } else {
  168. return callback(new Error("I don't understand the doc format in s3"));
  169. }
  170. return callback(null, mongo_doc);
  171. },
  172. _mongoDocToS3Doc(doc, callback) {
  173. if (callback == null) { callback = function(error, s3_doc) {}; }
  174. if ((doc.lines == null)) {
  175. return callback(new Error("doc has no lines"));
  176. }
  177. const json = JSON.stringify({
  178. lines: doc.lines,
  179. ranges: doc.ranges,
  180. schema_v: 1
  181. });
  182. if (json.indexOf("\u0000") !== -1) {
  183. const error = new Error("null bytes detected");
  184. logger.err({err: error, doc, json}, error.message);
  185. return callback(error);
  186. }
  187. return callback(null, json);
  188. },
  189. buildS3Options(key){
  190. if ((settings.docstore.s3 == null)) {
  191. throw new Error("S3 settings are not configured");
  192. }
  193. return {
  194. aws: {
  195. key: settings.docstore.s3.key,
  196. secret: settings.docstore.s3.secret,
  197. bucket: settings.docstore.s3.bucket
  198. },
  199. timeout: thirtySeconds,
  200. uri:`https://${settings.docstore.s3.bucket}.s3.amazonaws.com/${key}`
  201. };
  202. }
  203. });