1
0
silverbullet/server/storage_backend.ts
Zef Hemel 9f082c83a9
Major backend refactor (#599)
Backend refactor
2023-12-13 17:52:56 +01:00

56 lines
2.0 KiB
TypeScript

import { DiskSpacePrimitives } from "../common/spaces/disk_space_primitives.ts";
import { SpacePrimitives } from "../common/spaces/space_primitives.ts";
import { path } from "./deps.ts";
import { S3SpacePrimitives } from "../common/spaces/s3_space_primitives.ts";
import { KvPrimitives } from "../plugos/lib/kv_primitives.ts";
import { ChunkedKvStoreSpacePrimitives } from "../common/spaces/chunked_datastore_space_primitives.ts";
import { HttpSpacePrimitives } from "../common/spaces/http_space_primitives.ts";
export async function determineStorageBackend(
kvPrimitives: KvPrimitives,
folder: string,
): Promise<SpacePrimitives> {
if (folder.startsWith("s3://")) {
console.info("Using S3 as a storage backend");
let objectPrefix = folder.slice("s3://".length);
if (objectPrefix !== "") {
// Add a suffix /
objectPrefix += "/";
}
const spacePrimitives = new S3SpacePrimitives(
kvPrimitives,
["meta"],
objectPrefix,
{
accessKey: Deno.env.get("AWS_ACCESS_KEY_ID")!,
secretKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!,
endPoint: Deno.env.get("AWS_ENDPOINT")!,
region: Deno.env.get("AWS_REGION")!,
bucket: Deno.env.get("AWS_BUCKET")!,
},
);
if (Deno.env.get("SB_S3_PERFORM_SYNC") === "true") {
console.log("Performing S3 file list sync");
await spacePrimitives.syncFileList();
console.info("S3 file list sync complete");
}
return spacePrimitives;
} else if (folder === "db://") {
console.info(`Using the database as a storage backend`);
return new ChunkedKvStoreSpacePrimitives(
kvPrimitives,
65536, // For DenoKV, this is the maximum size of a single value
);
} else if (folder.startsWith("http://") || folder.startsWith("https://")) {
return new HttpSpacePrimitives(
folder,
undefined,
Deno.env.get("SB_AUTH_TOKEN"),
);
} else {
folder = path.resolve(Deno.cwd(), folder);
console.info(`Using local disk as a storage backend: ${folder}`);
return new DiskSpacePrimitives(folder);
}
}