From c16173ea0fdbf6cc47b13756c0a77e8399669055 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sat, 12 Oct 2024 16:47:22 +0000 Subject: feature: Introduce a mechanism to cleanup dangling assets --- packages/shared/assetdb.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'packages/shared/assetdb.ts') 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, + }; + } +} -- cgit v1.2.3-70-g09d2