UBERF-6161: Storage configuration (#5109)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev
2024-04-02 14:05:16 +07:00
committed by GitHub
parent 5da63b70f4
commit 0803bb4ea2
50 changed files with 1254 additions and 713 deletions
+20 -11
View File
@@ -1,4 +1,4 @@
import { WorkspaceId } from '@hcengineering/core'
import { MeasureContext, WorkspaceId } from '@hcengineering/core'
import { StorageAdapter } from '@hcengineering/server-core'
import { createReadStream, createWriteStream, existsSync } from 'fs'
import { mkdir, readFile, writeFile } from 'fs/promises'
@@ -55,35 +55,43 @@ class AdapterStorage implements BackupStorage {
constructor (
readonly client: StorageAdapter,
readonly workspaceId: WorkspaceId,
readonly root: string
readonly root: string,
readonly ctx: MeasureContext
) {}
async loadFile (name: string): Promise<Buffer> {
const data = await this.client.read(this.workspaceId, join(this.root, name))
const data = await this.client.read(this.ctx, this.workspaceId, join(this.root, name))
return Buffer.concat(data)
}
async write (name: string): Promise<Writable> {
const wr = new PassThrough()
void this.client.put(this.workspaceId, join(this.root, name), wr)
void this.client.put(this.ctx, this.workspaceId, join(this.root, name), wr, 'application/octet-stream')
return wr
}
async load (name: string): Promise<Readable> {
return await this.client.get(this.workspaceId, join(this.root, name))
return await this.client.get(this.ctx, this.workspaceId, join(this.root, name))
}
async exists (name: string): Promise<boolean> {
try {
await this.client.stat(this.workspaceId, join(this.root, name))
return true
return (await this.client.stat(this.ctx, this.workspaceId, join(this.root, name))) !== undefined
} catch (err) {
return false
}
}
async writeFile (name: string, data: string | Buffer): Promise<void> {
void this.client.put(this.workspaceId, join(this.root, name), data, data.length)
// TODO: add mime type detection here.
await this.client.put(
this.ctx,
this.workspaceId,
join(this.root, name),
data,
'application/octet-stream',
data.length
)
}
}
@@ -101,12 +109,13 @@ export async function createFileBackupStorage (fileName: string): Promise<Backup
* @public
*/
export async function createStorageBackupStorage (
ctx: MeasureContext,
client: StorageAdapter,
workspaceId: WorkspaceId,
root: string
): Promise<BackupStorage> {
if (!(await client.exists(workspaceId))) {
await client.make(workspaceId)
if (!(await client.exists(ctx, workspaceId))) {
await client.make(ctx, workspaceId)
}
return new AdapterStorage(client, workspaceId, root)
return new AdapterStorage(client, workspaceId, root, ctx)
}