DocstoreClient.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. let DocstoreClient
  2. const request = require('request').defaults({ jar: false })
  3. const settings = require('@overleaf/settings')
  4. const Persistor = require('../../../../app/js/PersistorManager')
  5. async function streamToString(stream) {
  6. const chunks = []
  7. return await new Promise((resolve, reject) => {
  8. stream.on('data', chunk => chunks.push(chunk))
  9. stream.on('error', reject)
  10. stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
  11. })
  12. }
  13. async function getStringFromPersistor(persistor, bucket, key) {
  14. const stream = await persistor.getObjectStream(bucket, key, {})
  15. stream.resume()
  16. return await streamToString(stream)
  17. }
  18. module.exports = DocstoreClient = {
  19. createDoc(projectId, docId, lines, version, ranges, callback) {
  20. return DocstoreClient.updateDoc(
  21. projectId,
  22. docId,
  23. lines,
  24. version,
  25. ranges,
  26. callback
  27. )
  28. },
  29. getDoc(projectId, docId, qs, callback) {
  30. request.get(
  31. {
  32. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
  33. json: true,
  34. qs,
  35. },
  36. callback
  37. )
  38. },
  39. peekDoc(projectId, docId, qs, callback) {
  40. request.get(
  41. {
  42. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/peek`,
  43. json: true,
  44. qs,
  45. },
  46. callback
  47. )
  48. },
  49. isDocDeleted(projectId, docId, callback) {
  50. request.get(
  51. {
  52. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/deleted`,
  53. json: true,
  54. },
  55. callback
  56. )
  57. },
  58. getAllDocs(projectId, callback) {
  59. request.get(
  60. {
  61. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc`,
  62. json: true,
  63. },
  64. (req, res, body) => {
  65. callback(req, res, body)
  66. }
  67. )
  68. },
  69. getAllDeletedDocs(projectId, callback) {
  70. request.get(
  71. {
  72. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc-deleted`,
  73. json: true,
  74. },
  75. (error, res, body) => {
  76. if (error) return callback(error)
  77. if (res.statusCode !== 200) {
  78. return callback(new Error('unexpected statusCode'))
  79. }
  80. callback(null, body)
  81. }
  82. )
  83. },
  84. getAllRanges(projectId, callback) {
  85. request.get(
  86. {
  87. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/ranges`,
  88. json: true,
  89. },
  90. callback
  91. )
  92. },
  93. updateDoc(projectId, docId, lines, version, ranges, callback) {
  94. return request.post(
  95. {
  96. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
  97. json: {
  98. lines,
  99. version,
  100. ranges,
  101. },
  102. },
  103. callback
  104. )
  105. },
  106. deleteDoc(projectId, docId, callback) {
  107. DocstoreClient.deleteDocWithDateAndName(
  108. projectId,
  109. docId,
  110. new Date(),
  111. 'main.tex',
  112. callback
  113. )
  114. },
  115. deleteDocWithDate(projectId, docId, date, callback) {
  116. DocstoreClient.deleteDocWithDateAndName(
  117. projectId,
  118. docId,
  119. date,
  120. 'main.tex',
  121. callback
  122. )
  123. },
  124. deleteDocWithName(projectId, docId, name, callback) {
  125. DocstoreClient.deleteDocWithDateAndName(
  126. projectId,
  127. docId,
  128. new Date(),
  129. name,
  130. callback
  131. )
  132. },
  133. deleteDocWithDateAndName(projectId, docId, deletedAt, name, callback) {
  134. request.patch(
  135. {
  136. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
  137. json: { name, deleted: true, deletedAt },
  138. },
  139. callback
  140. )
  141. },
  142. archiveAllDoc(projectId, callback) {
  143. request.post(
  144. {
  145. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/archive`,
  146. },
  147. callback
  148. )
  149. },
  150. archiveDoc(projectId, docId, callback) {
  151. request.post(
  152. {
  153. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/archive`,
  154. },
  155. callback
  156. )
  157. },
  158. destroyAllDoc(projectId, callback) {
  159. request.post(
  160. {
  161. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/destroy`,
  162. },
  163. callback
  164. )
  165. },
  166. getS3Doc(projectId, docId, callback) {
  167. getStringFromPersistor(
  168. Persistor,
  169. settings.docstore.bucket,
  170. `${projectId}/${docId}`
  171. )
  172. .then(data => {
  173. callback(null, JSON.parse(data))
  174. })
  175. .catch(callback)
  176. },
  177. }