project_import.js 4.1 KB

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