Initial local backup snapshot
This commit is contained in:
commit
acd9e14ba5
367 changed files with 118038 additions and 0 deletions
39
server/storage.ts
Normal file
39
server/storage.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
function normalizeKey(relKey: string): string {
|
||||
return relKey
|
||||
.replace(/^\/+/, "")
|
||||
.split("/")
|
||||
.filter(Boolean)
|
||||
.map(segment => segment.replace(/[^a-zA-Z0-9._-]/g, "_"))
|
||||
.join("/");
|
||||
}
|
||||
|
||||
function getUploadRoot() {
|
||||
return path.resolve(process.cwd(), "uploads");
|
||||
}
|
||||
|
||||
function getPublicUrl(key: string) {
|
||||
return `/uploads/${key}`;
|
||||
}
|
||||
|
||||
export async function storagePut(
|
||||
relKey: string,
|
||||
data: Buffer | Uint8Array | string,
|
||||
contentType = "application/octet-stream"
|
||||
): Promise<{ key: string; url: string }> {
|
||||
const key = normalizeKey(relKey);
|
||||
const filePath = path.join(getUploadRoot(), key);
|
||||
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
||||
await fs.writeFile(filePath, data);
|
||||
return { key, url: getPublicUrl(key) };
|
||||
}
|
||||
|
||||
export async function storageGet(relKey: string): Promise<{ key: string; url: string; }> {
|
||||
const key = normalizeKey(relKey);
|
||||
return {
|
||||
key,
|
||||
url: getPublicUrl(key),
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue