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.
64 lines
2.0 KiB
TypeScript
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 })
|
|
},
|
|
},
|
|
},
|
|
})
|