BoardKitBackend
AssetStorage Interface
Abstract storage interface for file uploads
AssetStorage is the abstract class for storing and retrieving uploaded files (images, PDFs).
abstract class AssetStorage {
abstract upload(boardId: string, file: Buffer, meta: AssetMeta): Promise<Asset>;
abstract getUrl(storageKey: string): Promise<string>;
abstract delete(storageKey: string): Promise<void>;
abstract getBoardUsage(boardId: string): Promise<number>;
}AssetMeta
interface AssetMeta {
mimeType: string;
sizeBytes: number;
uploadedBy: string;
}Methods
upload
Stores a file and returns an Asset record with the generated storageKey.
| Parameter | Type | Description |
|---|---|---|
boardId | string | Board this asset belongs to |
file | Buffer | Raw file data |
meta | AssetMeta | File metadata |
getUrl
Returns a URL for accessing the stored file. This could be a signed URL (S3), a local file path, or any retrieval mechanism.
delete
Removes a file from storage by its storageKey.
getBoardUsage
Returns the total storage usage for a board in bytes. Used by AssetService to enforce board-level quotas.
Available Implementations
| Implementation | Description |
|---|---|
LocalAssetAdapter | Stores files on the local filesystem |
S3AssetAdapter | Stores files in Amazon S3 or compatible storage |
InMemoryAssetStorage | In-memory storage for testing |
Implementing a Custom Adapter
import { AssetStorage, type AssetMeta } from '@hfu.digital/boardkit-nestjs';
import type { Asset } from '@hfu.digital/boardkit-core';
import { randomUUID } from 'crypto';
class MyAssetStorage extends AssetStorage {
async upload(boardId: string, file: Buffer, meta: AssetMeta): Promise<Asset> {
const storageKey = `${boardId}/${randomUUID()}`;
// Store file in your preferred storage system
await myStorage.put(storageKey, file);
return {
id: randomUUID(),
boardId,
mimeType: meta.mimeType,
sizeBytes: meta.sizeBytes,
storageKey,
uploadedBy: meta.uploadedBy,
createdAt: new Date().toISOString(),
};
}
async getUrl(storageKey: string): Promise<string> {
return myStorage.getSignedUrl(storageKey);
}
async delete(storageKey: string): Promise<void> {
await myStorage.delete(storageKey);
}
async getBoardUsage(boardId: string): Promise<number> {
return myStorage.getTotalSize(boardId);
}
}