Add file moving tool (#6223)

This commit is contained in:
Alexander Onnikov
2024-08-02 21:40:15 +07:00
committed by GitHub
parent 74d76d34a4
commit 29b082fc7b
4 changed files with 104 additions and 6 deletions
+2 -2
View File
@@ -24,7 +24,7 @@ describe('aggregator tests', () => {
const ws1: WorkspaceId = { name: 'ws1', productId: '' }
return { mem1, mem2, aggr, ws1, testCtx }
}
it('reuse existing storage', async () => {
it('not reuse existing storage', async () => {
const { mem1, aggr, ws1, testCtx } = prepare1()
// Test default provider
@@ -37,7 +37,7 @@ describe('aggregator tests', () => {
// Test content typed provider
await aggr.put(testCtx, ws1, 'test', 'data2', 'text/plain')
const stat2 = await aggr.stat(testCtx, ws1, 'test')
expect(stat2?.provider).toEqual('mem1')
expect(stat2?.provider).toEqual('mem2')
const dta = Buffer.concat(await aggr.read(testCtx, ws1, 'test')).toString()
expect(dta).toEqual('data2')
+8 -2
View File
@@ -317,12 +317,11 @@ export class AggregatorStorageAdapter implements StorageAdapter, StorageAdapterE
contentType: string,
size?: number | undefined
): Promise<UploadedObjectInfo> {
// We need to reuse same provider for existing documents.
const stat = (
await this.dbAdapter.find<Blob>(ctx, workspaceId, DOMAIN_BLOB, { _id: objectName as Ref<Blob> }, { limit: 1 })
).shift()
const { provider, adapter } = this.selectProvider(stat?.provider, contentType)
const { provider, adapter } = this.selectProvider(undefined, contentType)
const result = await adapter.put(ctx, workspaceId, objectName, stream, contentType, size)
@@ -351,6 +350,13 @@ export class AggregatorStorageAdapter implements StorageAdapter, StorageAdapterE
}
await this.dbAdapter.upload<Blob>(ctx, workspaceId, DOMAIN_BLOB, [blobDoc])
// If the file is already stored in different provider, we need to remove it.
if (stat !== undefined && stat.provider !== provider) {
const adapter = this.adapters.get(stat.provider)
await adapter?.remove(ctx, workspaceId, [stat._id])
}
return result
}
}