Просмотр исходного кода

Merge pull request #22327 from overleaf/bg-fix-copy-blob

fix bug that prevents copying blobs between different backends in history-v1

GitOrigin-RevId: 41140ad42d0d7c1beda83e588649127c22603dec
Brian Gough 1 год назад
Родитель
Сommit
104ae341b1

+ 1 - 1
services/history-v1/api/swagger/projects.js

@@ -206,7 +206,7 @@ exports.paths = {
           name: 'copyFrom',
           in: 'query',
           description: 'source project id',
-          required: false,
+          required: true,
           type: 'string',
         },
       ],

+ 2 - 1
services/history-v1/storage/lib/blob_store/index.js

@@ -405,10 +405,11 @@ class BlobStore {
     const sourceProjectId = this.projectId
     const { bucket, key: sourceKey } = getBlobLocation(sourceProjectId, hash)
     const destKey = makeProjectKey(targetProjectId, hash)
+    const targetBackend = getBackend(targetProjectId)
     logger.debug({ sourceProjectId, targetProjectId, hash }, 'copyBlob started')
     try {
       await persistor.copyObject(bucket, sourceKey, destKey)
-      await this.backend.insertBlob(targetProjectId, sourceBlob)
+      await targetBackend.insertBlob(targetProjectId, sourceBlob)
     } finally {
       logger.debug(
         { sourceProjectId, targetProjectId, hash },

+ 37 - 2
services/history-v1/test/acceptance/js/storage/blob_store.test.js

@@ -480,7 +480,7 @@ describe('BlobStore', function () {
       })
 
       describe('copyBlob method', function () {
-        it('copies a binary blob to another project', async function () {
+        it('copies a binary blob to another project in the same backend', async function () {
           const testFile = 'graph.png'
           const originalHash = testFiles.GRAPH_PNG_HASH
           const insertedBlob = await blobStore.putFile(testFiles.path(testFile))
@@ -493,7 +493,7 @@ describe('BlobStore', function () {
           expect(copiedBlob.getStringLength()).to.be.null
         })
 
-        it('copies a text blob to another project', async function () {
+        it('copies a text blob to another project in the same backend', async function () {
           const insertedBlob = await blobStore.putString(helloWorldString)
           await blobStore.copyBlob(insertedBlob, scenario.projectId2)
           const copiedBlob = await blobStore2.getBlob(helloWorldHash)
@@ -502,6 +502,41 @@ describe('BlobStore', function () {
           expect(content).to.equal(helloWorldString)
         })
       })
+
+      describe('copyBlob method with different backends', function () {
+        const otherScenario = scenarios.find(
+          s => s.backend !== scenario.backend
+        )
+        const otherBlobStore = new BlobStore(otherScenario.projectId2)
+
+        beforeEach(async function () {
+          await otherBlobStore.initialize()
+        })
+
+        it('copies a binary blob to another project in a different backend', async function () {
+          const testFile = 'graph.png'
+          const originalHash = testFiles.GRAPH_PNG_HASH
+          const insertedBlob = await blobStore.putFile(testFiles.path(testFile))
+          await blobStore.copyBlob(insertedBlob, otherScenario.projectId2)
+          const copiedBlob = await otherBlobStore.getBlob(originalHash)
+          expect(copiedBlob).to.exist
+          expect(copiedBlob.getHash()).to.equal(originalHash)
+          expect(copiedBlob.getByteLength()).to.equal(
+            insertedBlob.getByteLength()
+          )
+          expect(copiedBlob.getStringLength()).to.be.null
+        })
+
+        it('copies a text blob to another project in a different backend', async function () {
+          const insertedBlob = await blobStore.putString(helloWorldString)
+          await blobStore.copyBlob(insertedBlob, otherScenario.projectId2)
+          const copiedBlob = await otherBlobStore.getBlob(helloWorldHash)
+          expect(copiedBlob).to.exist
+          expect(copiedBlob.getHash()).to.equal(helloWorldHash)
+          const content = await otherBlobStore.getString(helloWorldHash)
+          expect(content).to.equal(helloWorldString)
+        })
+      })
     })
   }