Files
dahoam/src/routes/api/icon.ts
T
niklas e85244e820
images / check (push) Successful in 33s
images / image (push) Successful in 16s
feat: several dashboards with a default, icon search and icon upload
Dashboards are separate sets of cards (Home, Work). Settings manages them and
sets the one that opens at sign-in, the header and the search bar switch.
Migration 0006 puts the existing cards on a dashboard "Home".

The link dialog searches dashboard-icons and uploads custom icons (/api/icon,
up to 1 MB, SVG served sandboxed). Wallpaper and icons share the upload checks.
2026-11-18 17:53:27 +01:00

64 lines
2.0 KiB
TypeScript

import { createFileRoute } from "@tanstack/react-router"
import { and, eq, sql } from "drizzle-orm"
import { z } from "zod"
import { db } from "@/db"
import { icon, link } from "@/db/schema"
import { UPLOADED_ICON } from "@/lib/icons"
import { readImage, serveImage, userId } from "@/lib/upload"
const LIMITS = {
// SVG is safe here: serveImage sandboxes it
types: [
"image/png",
"image/jpeg",
"image/webp",
"image/avif",
"image/svg+xml",
],
formats: "PNG, JPEG, WebP, AVIF or SVG",
maxMb: 1,
}
export const Route = createFileRoute("/api/icon")({
server: {
handlers: {
GET: async ({ request }) => {
const user = await userId(request)
if (!user) return new Response("Unauthorized", { status: 401 })
const id = z
.uuid()
.safeParse(new URL(request.url).searchParams.get("id"))
if (!id.success) return new Response("Not found", { status: 404 })
return serveImage(
(
await db
.select()
.from(icon)
.where(and(eq(icon.id, id.data), eq(icon.userId, user)))
).at(0)
)
},
POST: async ({ request }) => {
const image = await readImage(request, LIMITS)
if (image instanceof Response) return image
// Icons that no link uses any more go away with the next upload. An hour of
// grace, because a new icon has no link until its dialog is saved.
// shortcut: no limit on uploads per user, add one if strangers can sign up
await db.execute(sql`
delete from ${icon}
where ${icon.userId} = ${image.userId}
and ${icon.createdAt} < now() - interval '1 hour'
and not exists (
select 1 from ${link}
where ${link.icon} = ${UPLOADED_ICON} || ${icon.id}::text
)`)
const [row] = await db
.insert(icon)
.values(image)
.returning({ id: icon.id })
return Response.json({ icon: UPLOADED_ICON + row.id })
},
},
},
})