aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared/assetdb.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/shared/assetdb.ts')
-rw-r--r--packages/shared/assetdb.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/packages/shared/assetdb.ts b/packages/shared/assetdb.ts
index 4edfa1ec..64413e9f 100644
--- a/packages/shared/assetdb.ts
+++ b/packages/shared/assetdb.ts
@@ -1,5 +1,6 @@
import * as fs from "fs";
import * as path from "path";
+import { Glob } from "glob";
import { z } from "zod";
import serverConfig from "./config";
@@ -120,6 +121,25 @@ export async function readAsset({
return { asset, metadata };
}
+export async function readAssetMetadata({
+ userId,
+ assetId,
+}: {
+ userId: string;
+ assetId: string;
+}) {
+ const assetDir = getAssetDir(userId, assetId);
+
+ const metadataStr = await fs.promises.readFile(
+ path.join(assetDir, "metadata.json"),
+ {
+ encoding: "utf8",
+ },
+ );
+
+ return zAssetMetadataSchema.parse(JSON.parse(metadataStr));
+}
+
export async function getAssetSize({
userId,
assetId,
@@ -154,3 +174,25 @@ export async function deleteUserAssets({ userId }: { userId: string }) {
}
await fs.promises.rm(userDir, { recursive: true });
}
+
+export async function* getAllAssets() {
+ const g = new Glob(`/**/**/asset.bin`, {
+ maxDepth: 3,
+ root: ROOT_PATH,
+ cwd: ROOT_PATH,
+ absolute: false,
+ });
+ for await (const file of g) {
+ const [userId, assetId] = file.split("/").slice(0, 2);
+ const [size, metadata] = await Promise.all([
+ getAssetSize({ userId, assetId }),
+ readAssetMetadata({ userId, assetId }),
+ ]);
+ yield {
+ userId,
+ assetId,
+ ...metadata,
+ size,
+ };
+ }
+}