project_import.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict'
  2. const BPromise = require('bluebird')
  3. const HTTPStatus = require('http-status')
  4. const core = require('overleaf-editor-core')
  5. const Change = core.Change
  6. const Chunk = core.Chunk
  7. const File = core.File
  8. const FileMap = core.FileMap
  9. const Snapshot = core.Snapshot
  10. const TextOperation = core.TextOperation
  11. const logger = require('@overleaf/logger')
  12. const storage = require('../../storage')
  13. const BatchBlobStore = storage.BatchBlobStore
  14. const BlobStore = storage.BlobStore
  15. const chunkStore = storage.chunkStore
  16. const HashCheckBlobStore = storage.HashCheckBlobStore
  17. const persistChanges = storage.persistChanges
  18. const render = require('./render')
  19. exports.importSnapshot = function importSnapshot(req, res, next) {
  20. const projectId = req.swagger.params.project_id.value
  21. const rawSnapshot = req.swagger.params.snapshot.value
  22. let snapshot
  23. try {
  24. snapshot = Snapshot.fromRaw(rawSnapshot)
  25. } catch (err) {
  26. return render.unprocessableEntity(res)
  27. }
  28. return chunkStore
  29. .initializeProject(projectId, snapshot)
  30. .then(function (projectId) {
  31. res.status(HTTPStatus.OK).json({ projectId })
  32. })
  33. .catch(err => {
  34. if (err instanceof chunkStore.AlreadyInitialized) {
  35. render.conflict(res)
  36. } else {
  37. next(err)
  38. }
  39. })
  40. }
  41. exports.importChanges = 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.error(err)
  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. function loadFiles() {
  65. const blobHashes = new Set()
  66. changes.forEach(function findBlobHashesToPreload(change) {
  67. change.findBlobHashes(blobHashes)
  68. })
  69. function lazyLoadChangeFiles(change) {
  70. return change.loadFiles('lazy', batchBlobStore)
  71. }
  72. return batchBlobStore
  73. .preload(Array.from(blobHashes))
  74. .then(function lazyLoadChangeFilesWithBatching() {
  75. return BPromise.each(changes, lazyLoadChangeFiles)
  76. })
  77. }
  78. function buildResultSnapshot(resultChunk) {
  79. return BPromise.resolve(
  80. resultChunk || chunkStore.loadLatest(projectId)
  81. ).then(function (chunk) {
  82. const snapshot = chunk.getSnapshot()
  83. snapshot.applyAll(chunk.getChanges())
  84. return snapshot.store(hashCheckBlobStore)
  85. })
  86. }
  87. return loadFiles()
  88. .then(function () {
  89. return persistChanges(projectId, changes, limits, endVersion)
  90. })
  91. .then(function (result) {
  92. if (returnSnapshot === 'none') {
  93. res.status(HTTPStatus.CREATED).json({})
  94. } else {
  95. return buildResultSnapshot(result && result.currentChunk).then(
  96. function (rawSnapshot) {
  97. res.status(HTTPStatus.CREATED).json(rawSnapshot)
  98. }
  99. )
  100. }
  101. })
  102. .catch(err => {
  103. if (
  104. err instanceof Chunk.ConflictingEndVersion ||
  105. err instanceof TextOperation.UnprocessableError ||
  106. err instanceof File.NotEditableError ||
  107. err instanceof FileMap.PathnameError ||
  108. err instanceof Snapshot.EditMissingFileError ||
  109. err instanceof chunkStore.ChunkVersionConflictError
  110. ) {
  111. // If we failed to apply operations, that's probably because they were
  112. // invalid.
  113. logger.error(err)
  114. render.unprocessableEntity(res)
  115. } else if (err instanceof Chunk.NotFoundError) {
  116. render.notFound(res)
  117. } else {
  118. next(err)
  119. }
  120. })
  121. }