Add minio and kv modules

This commit is contained in:
2023-11-19 18:36:29 -07:00
parent 7187ed7994
commit 7c40650b9a
9 changed files with 395 additions and 0 deletions

135
src/kv/kv.service.ts Normal file
View File

@@ -0,0 +1,135 @@
import { Injectable } from '@nestjs/common';
import { UploadedObjectInfo } from 'minio';
import { MinioService } from 'src/minio/minio.service';
@Injectable()
export class KvService {
private readonly kvBucketName = 'api.us.dev';
private readonly kvPrefix = 'kv';
private readonly kvMetadataFileName = 'metadata.json';
private readonly kvMetadataPath = `${this.kvPrefix}/${this.kvMetadataFileName}`;
constructor(private readonly minioService: MinioService) {}
public generateFilePath(namespace: string, key: string): string {
return `${this.kvPrefix}/${namespace}/${key}`;
}
private async getMetadataFile(): Promise<Buffer> {
return await this.minioService.getBuffer(
this.kvBucketName,
this.kvMetadataPath,
);
}
private async setMetadataFile(buffer: Buffer): Promise<void> {
await this.minioService.uploadBuffer(
this.kvBucketName,
this.kvMetadataPath,
buffer,
);
}
public async queryObjectMetadata(
namespace: string,
key: string,
secretKey: string,
): Promise<object> {
const metadata = await this.getMetadataFile().then((buffer) =>
JSON.parse(buffer.toString()),
);
if (metadata[namespace]?.secretKey !== secretKey) {
throw new Error('Secret key does not match');
}
const objectMetadata = await this.minioService.getObjectMetadata(
this.kvBucketName,
this.generateFilePath(namespace, key),
);
return objectMetadata?.metaData;
}
public async get(namespace: string, key: string): Promise<string> {
const objectMetadata = await this.minioService.getObjectMetadata(
this.kvBucketName,
this.generateFilePath(namespace, key),
);
console.log(objectMetadata);
if (objectMetadata?.metaData.public !== 'true') {
throw new Error('Object is not public');
}
return await this.minioService
.getBuffer(this.kvBucketName, this.generateFilePath(namespace, key))
.then((buffer) => buffer.toString());
}
public async getWithSecretKey(
namespace: string,
key: string,
secretKey: string,
): Promise<string> {
const metadata = await this.getMetadataFile().then((buffer) =>
JSON.parse(buffer.toString()),
);
if (metadata[namespace]?.secretKey !== secretKey) {
throw new Error('Secret key does not match');
}
return await this.minioService
.getBuffer(this.kvBucketName, this.generateFilePath(namespace, key))
.then((buffer) => buffer.toString());
}
public async claimNamespace(
namespace: string,
extraData: object = {},
): Promise<void> {
const metadata = await this.getMetadataFile().then((buffer) =>
JSON.parse(buffer.toString()),
);
if (metadata[namespace]?.secretKey) {
throw new Error('Namespace already claimed');
}
metadata[namespace] = {
...extraData,
secretKey: Math.random().toString(36).substring(2),
claimed: new Date().toISOString(),
};
await this.setMetadataFile(Buffer.from(JSON.stringify(metadata)));
return metadata[namespace];
}
public async set(
namespace: string,
key: string,
value: string,
secretKey: string,
options: {
public: boolean;
} = {
public: false,
},
): Promise<UploadedObjectInfo> {
const metadata = await this.getMetadataFile().then((buffer) =>
JSON.parse(buffer.toString()),
);
if (metadata[namespace]?.secretKey !== secretKey) {
throw new Error('Incorrect secret key for namespace');
}
return await this.minioService.uploadBuffer(
this.kvBucketName,
this.generateFilePath(namespace, key),
Buffer.from(value),
{
set: new Date().toISOString(),
namespace,
...options,
},
);
}
public async delete(namespace: string, key: string): Promise<void> {
await this.minioService.deleteObject(
this.kvBucketName,
this.generateFilePath(namespace, key),
);
}
}