diff options
| author | MohamedBassem <me@mbassem.com> | 2024-05-26 00:06:32 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-05-26 10:11:53 +0000 |
| commit | dedc5fb24536832eae2c18d84efa2a92272c955c (patch) | |
| tree | 4b9540b819db892fa6bc66a29cf8fc790d06ea67 /packages/shared/assetdb.ts | |
| parent | 033e8a2d26bb0ecaa8301609960d35d3467a88f4 (diff) | |
| download | karakeep-dedc5fb24536832eae2c18d84efa2a92272c955c.tar.zst | |
feature: Full page archival with monolith. Fixes #132
Diffstat (limited to 'packages/shared/assetdb.ts')
| -rw-r--r-- | packages/shared/assetdb.ts | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/packages/shared/assetdb.ts b/packages/shared/assetdb.ts index c070ad54..4cea06b0 100644 --- a/packages/shared/assetdb.ts +++ b/packages/shared/assetdb.ts @@ -6,13 +6,20 @@ import serverConfig from "./config"; const ROOT_PATH = path.join(serverConfig.dataDir, "assets"); -export const SUPPORTED_ASSET_TYPES = new Set([ +// The assets that we allow the users to upload +export const SUPPORTED_UPLOAD_ASSET_TYPES = new Set([ "image/jpeg", "image/png", "image/webp", "application/pdf", ]); +// The assets that we support saving in the asset db +export const SUPPORTED_ASSET_TYPES = new Set([ + ...SUPPORTED_UPLOAD_ASSET_TYPES, + "text/html", +]); + function getAssetDir(userId: string, assetId: string) { return path.join(ROOT_PATH, userId, assetId); } @@ -52,6 +59,32 @@ export async function saveAsset({ ]); } +export async function saveAssetFromFile({ + userId, + assetId, + assetPath, + metadata, +}: { + userId: string; + assetId: string; + assetPath: string; + metadata: z.infer<typeof zAssetMetadataSchema>; +}) { + if (!SUPPORTED_ASSET_TYPES.has(metadata.contentType)) { + throw new Error("Unsupported asset type"); + } + const assetDir = getAssetDir(userId, assetId); + await fs.promises.mkdir(assetDir, { recursive: true }); + + await Promise.all([ + fs.promises.rename(assetPath, path.join(assetDir, "asset.bin")), + fs.promises.writeFile( + path.join(assetDir, "metadata.json"), + JSON.stringify(metadata), + ), + ]); +} + export async function readAsset({ userId, assetId, |
