project_import.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // @ts-check
  2. 'use strict'
  3. const { expressify } = require('@overleaf/promise-utils')
  4. const HTTPStatus = require('http-status')
  5. const core = require('overleaf-editor-core')
  6. const Change = core.Change
  7. const Chunk = core.Chunk
  8. const File = core.File
  9. const FileMap = core.FileMap
  10. const Snapshot = core.Snapshot
  11. const TextOperation = core.TextOperation
  12. const logger = require('@overleaf/logger')
  13. const storage = require('../../storage')
  14. const BatchBlobStore = storage.BatchBlobStore
  15. const BlobStore = storage.BlobStore
  16. const chunkStore = storage.chunkStore
  17. const HashCheckBlobStore = storage.HashCheckBlobStore
  18. const persistChanges = storage.persistChanges
  19. const InvalidChangeError = storage.InvalidChangeError
  20. const render = require('./render')
  21. async function importSnapshot(req, res) {
  22. const projectId = req.swagger.params.project_id.value
  23. const rawSnapshot = req.swagger.params.snapshot.value
  24. let snapshot
  25. try {
  26. snapshot = Snapshot.fromRaw(rawSnapshot)
  27. } catch (err) {
  28. logger.warn({ err, projectId }, 'failed to import snapshot')
  29. return render.unprocessableEntity(res)
  30. }
  31. let historyId
  32. try {
  33. historyId = await chunkStore.initializeProject(projectId, snapshot)
  34. } catch (err) {
  35. if (err instanceof chunkStore.AlreadyInitialized) {
  36. logger.warn({ err, projectId }, 'already initialized')
  37. return render.conflict(res)
  38. } else {
  39. throw err
  40. }
  41. }
  42. res.status(HTTPStatus.OK).json({ projectId: historyId })
  43. }
  44. async function importChanges(req, res, next) {
  45. const projectId = req.swagger.params.project_id.value
  46. const rawChanges = req.swagger.params.changes.value
  47. const endVersion = req.swagger.params.end_version.value
  48. const returnSnapshot = req.swagger.params.return_snapshot.value || 'none'
  49. let changes
  50. try {
  51. changes = rawChanges.map(Change.fromRaw)
  52. } catch (err) {
  53. logger.warn({ err, projectId }, 'failed to parse changes')
  54. return render.unprocessableEntity(res)
  55. }
  56. // Set limits to force us to persist all of the changes.
  57. const farFuture = new Date()
  58. farFuture.setTime(farFuture.getTime() + 7 * 24 * 3600 * 1000)
  59. const limits = {
  60. maxChanges: 0,
  61. minChangeTimestamp: farFuture,
  62. maxChangeTimestamp: farFuture,
  63. }
  64. const blobStore = new BlobStore(projectId)
  65. const batchBlobStore = new BatchBlobStore(blobStore)
  66. const hashCheckBlobStore = new HashCheckBlobStore(blobStore)
  67. async function loadFiles() {
  68. const blobHashes = new Set()
  69. for (const change of changes) {
  70. // This populates the set blobHashes with blobs referred to in the change
  71. change.findBlobHashes(blobHashes)
  72. }
  73. await batchBlobStore.preload(Array.from(blobHashes))
  74. for (const change of changes) {
  75. await change.loadFiles('lazy', batchBlobStore)
  76. }
  77. }
  78. async function buildResultSnapshot(resultChunk) {
  79. const chunk =
  80. resultChunk ||
  81. (await chunkStore.loadLatest(projectId, { persistedOnly: true }))
  82. const snapshot = chunk.getSnapshot()
  83. snapshot.applyAll(chunk.getChanges())
  84. const rawSnapshot = await snapshot.store(hashCheckBlobStore)
  85. return rawSnapshot
  86. }
  87. await loadFiles()
  88. let result
  89. try {
  90. result = await persistChanges(projectId, changes, limits, endVersion, {
  91. queueChangesInRedis: true,
  92. })
  93. } catch (err) {
  94. if (
  95. err instanceof Chunk.ConflictingEndVersion ||
  96. err instanceof TextOperation.UnprocessableError ||
  97. err instanceof File.NotEditableError ||
  98. err instanceof FileMap.PathnameError ||
  99. err instanceof Snapshot.EditMissingFileError ||
  100. err instanceof chunkStore.ChunkVersionConflictError ||
  101. err instanceof InvalidChangeError
  102. ) {
  103. // If we failed to apply operations, that's probably because they were
  104. // invalid.
  105. logger.warn({ err, projectId, endVersion }, 'changes rejected by history')
  106. return render.unprocessableEntity(res)
  107. } else if (err instanceof Chunk.NotFoundError) {
  108. logger.warn({ err, projectId }, 'chunk not found')
  109. return render.notFound(res)
  110. } else {
  111. throw err
  112. }
  113. }
  114. if (returnSnapshot === 'none') {
  115. res.status(HTTPStatus.CREATED).json({
  116. resyncNeeded: result.resyncNeeded,
  117. })
  118. } else {
  119. const rawSnapshot = await buildResultSnapshot(result && result.currentChunk)
  120. res.status(HTTPStatus.CREATED).json(rawSnapshot)
  121. }
  122. }
  123. exports.importSnapshot = expressify(importSnapshot)
  124. exports.importChanges = expressify(importChanges)