Skip to content

Commit 29b082f

Browse files
authored
Add file moving tool (#6223)
1 parent 74d76d3 commit 29b082f

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

Diff for: dev/tool/src/index.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Copyright © 2020, 2021 Anticrm Platform Contributors.
3-
// Copyright © 2021 Hardcore Engineering Inc.
3+
// Copyright © 2021, 2024 Hardcore Engineering Inc.
44
//
55
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
66
// you may not use this file except in compliance with the License. You may
@@ -74,7 +74,7 @@ import { consoleModelLogger, type MigrateOperation } from '@hcengineering/model'
7474
import contact from '@hcengineering/model-contact'
7575
import { getMongoClient, getWorkspaceDB } from '@hcengineering/mongo'
7676
import { openAIConfigDefaults } from '@hcengineering/openai'
77-
import type { StorageAdapter } from '@hcengineering/server-core'
77+
import type { StorageAdapter, StorageAdapterEx } from '@hcengineering/server-core'
7878
import { deepEqual } from 'fast-equals'
7979
import { createWriteStream, readFileSync } from 'fs'
8080
import { benchmark, benchmarkWorker } from './benchmark'
@@ -95,6 +95,7 @@ import { fixJsonMarkup } from './markup'
9595
import { fixMixinForeignAttributes, showMixinForeignAttributes } from './mixin'
9696
import { openAIConfig } from './openai'
9797
import { fixAccountEmails, renameAccount } from './renameAccount'
98+
import { moveFiles } from './storage'
9899

99100
const colorConstants = {
100101
colorRed: '\u001b[31m',
@@ -1040,6 +1041,37 @@ export function devTool (
10401041
})
10411042
})
10421043

1044+
program
1045+
.command('move-files')
1046+
.option('-w, --workspace <workspace>', 'Selected workspace only', '')
1047+
.action(async (cmd: { workspace: string }) => {
1048+
const { mongodbUri } = prepareTools()
1049+
await withDatabase(mongodbUri, async (db, client) => {
1050+
await withStorage(mongodbUri, async (adapter) => {
1051+
try {
1052+
const exAdapter = adapter as StorageAdapterEx
1053+
if (exAdapter.adapters === undefined || exAdapter.adapters.size < 2) {
1054+
throw new Error('bad storage config, at least two storage providers are required')
1055+
}
1056+
1057+
console.log('moving files to storage provider', exAdapter.defaultAdapter)
1058+
1059+
const workspaces = await listWorkspacesPure(db, productId)
1060+
for (const workspace of workspaces) {
1061+
if (cmd.workspace !== '' && workspace.workspace !== cmd.workspace) {
1062+
continue
1063+
}
1064+
1065+
const wsId = getWorkspaceId(workspace.workspace, productId)
1066+
await moveFiles(toolCtx, wsId, exAdapter)
1067+
}
1068+
} catch (err: any) {
1069+
console.error(err)
1070+
}
1071+
})
1072+
})
1073+
})
1074+
10431075
program.command('fix-bw-workspace <workspace>').action(async (workspace: string) => {
10441076
const { mongodbUri } = prepareTools()
10451077
await withStorage(mongodbUri, async (adapter) => {

Diff for: dev/tool/src/storage.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// Copyright © 2024 Hardcore Engineering Inc.
3+
//
4+
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License. You may
6+
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
//
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
import { type MeasureContext, type WorkspaceId } from '@hcengineering/core'
17+
import { type StorageAdapterEx } from '@hcengineering/server-core'
18+
import { PassThrough } from 'stream'
19+
20+
export async function moveFiles (
21+
ctx: MeasureContext,
22+
workspaceId: WorkspaceId,
23+
exAdapter: StorageAdapterEx
24+
): Promise<void> {
25+
if (exAdapter.adapters === undefined) return
26+
27+
let count = 0
28+
29+
console.log('start', workspaceId.name)
30+
31+
// We assume that the adapter moves all new files to the default adapter
32+
const target = exAdapter.defaultAdapter
33+
await exAdapter.adapters.get(target)?.make(ctx, workspaceId)
34+
35+
for (const [name, adapter] of exAdapter.adapters.entries()) {
36+
if (name === target) continue
37+
38+
const iterator = await adapter.listStream(ctx, workspaceId)
39+
while (true) {
40+
const data = await iterator.next()
41+
if (data === undefined) break
42+
43+
const blob = await exAdapter.stat(ctx, workspaceId, data._id)
44+
if (blob === undefined) continue
45+
if (blob.provider === target) continue
46+
47+
const readable = await exAdapter.get(ctx, workspaceId, data._id)
48+
const stream = readable.pipe(new PassThrough())
49+
await exAdapter.put(ctx, workspaceId, data._id, stream, blob.contentType, blob.size)
50+
51+
count += 1
52+
if (count % 100 === 0) {
53+
console.log('...moved: ', count)
54+
}
55+
}
56+
await iterator.close()
57+
}
58+
59+
console.log('...done', workspaceId.name, count)
60+
}

Diff for: server/core/src/__tests__/aggregator.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('aggregator tests', () => {
2424
const ws1: WorkspaceId = { name: 'ws1', productId: '' }
2525
return { mem1, mem2, aggr, ws1, testCtx }
2626
}
27-
it('reuse existing storage', async () => {
27+
it('not reuse existing storage', async () => {
2828
const { mem1, aggr, ws1, testCtx } = prepare1()
2929

3030
// Test default provider
@@ -37,7 +37,7 @@ describe('aggregator tests', () => {
3737
// Test content typed provider
3838
await aggr.put(testCtx, ws1, 'test', 'data2', 'text/plain')
3939
const stat2 = await aggr.stat(testCtx, ws1, 'test')
40-
expect(stat2?.provider).toEqual('mem1')
40+
expect(stat2?.provider).toEqual('mem2')
4141

4242
const dta = Buffer.concat(await aggr.read(testCtx, ws1, 'test')).toString()
4343
expect(dta).toEqual('data2')

Diff for: server/core/src/server/aggregator.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,11 @@ export class AggregatorStorageAdapter implements StorageAdapter, StorageAdapterE
317317
contentType: string,
318318
size?: number | undefined
319319
): Promise<UploadedObjectInfo> {
320-
// We need to reuse same provider for existing documents.
321320
const stat = (
322321
await this.dbAdapter.find<Blob>(ctx, workspaceId, DOMAIN_BLOB, { _id: objectName as Ref<Blob> }, { limit: 1 })
323322
).shift()
324323

325-
const { provider, adapter } = this.selectProvider(stat?.provider, contentType)
324+
const { provider, adapter } = this.selectProvider(undefined, contentType)
326325

327326
const result = await adapter.put(ctx, workspaceId, objectName, stream, contentType, size)
328327

@@ -351,6 +350,13 @@ export class AggregatorStorageAdapter implements StorageAdapter, StorageAdapterE
351350
}
352351

353352
await this.dbAdapter.upload<Blob>(ctx, workspaceId, DOMAIN_BLOB, [blobDoc])
353+
354+
// If the file is already stored in different provider, we need to remove it.
355+
if (stat !== undefined && stat.provider !== provider) {
356+
const adapter = this.adapters.get(stat.provider)
357+
await adapter?.remove(ctx, workspaceId, [stat._id])
358+
}
359+
354360
return result
355361
}
356362
}

0 commit comments

Comments
 (0)