project_import.js 4.0 KB

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