Files
petvity/lib/storage.ts
T
GandClaude Opus 5 c65ac4b7d7 chore: add a formatter (#71)
* chore: add a formatter, matching the style this repo already writes

singleQuote=false was chosen by counting this repo's own
imports, not by fleet decree. The fleet is genuinely split and the two repos
that already had a .prettierrc disagreed with each other, so there was no
standard to restore. Quote style does not cross repo boundaries; having a
gate does.

Markdown is ignored for now — prettier rewraps prose, which would bury the
real diff.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* style: format with prettier (252 files)

Mechanical. No behaviour change. This SHA is listed in
.git-blame-ignore-revs so `git blame` skips it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore: teach git blame to skip the reformat

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-29 10:05:01 +00:00

32 lines
1.2 KiB
TypeScript

import { mkdir, writeFile, unlink } from "fs/promises";
import path from "path";
// Local-disk file storage (replaces @vercel/blob on the self-hosted box).
// Files land in UPLOADS_DIR (Caddy serves them under /uploads/*); the
// returned URL is root-relative so it works behind any domain.
const UPLOADS_DIR = process.env.UPLOADS_DIR || path.join(process.cwd(), "uploads");
function safeKey(key: string): string {
const clean = path.posix
.normalize(key)
.replace(/^(\.\.\/?)+/, "")
.replace(/^\/+/, "");
if (clean.includes("..")) throw new Error("invalid storage key");
return clean;
}
export async function putLocal(key: string, file: File | Buffer): Promise<{ url: string }> {
const k = safeKey(key);
const dest = path.join(UPLOADS_DIR, k);
await mkdir(path.dirname(dest), { recursive: true });
const data = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
await writeFile(dest, data);
return { url: `/uploads/${k}` };
}
export async function delLocal(url: string): Promise<void> {
if (!url.startsWith("/uploads/")) return;
const k = safeKey(url.slice("/uploads/".length));
await unlink(path.join(UPLOADS_DIR, k)).catch(() => {});
}