Files
Uploads, Convex-style: the page uploads the file, gets its id, and passes the id to an ordinary mutation that stores
it in a file() field. Queries turn ids into links.
// server/index.ts
import { file, Files, … } from "canica/server"
todos: table({ text: string(), owner: userId(), image: file().optional() }) // an Id<"_files">
todos: query(function* () {
const files = yield* Files // url, meta; mutations also delete
const list = yield* (yield* Db).select().from(todos).where(eq(todos.owner, (yield* Viewer).userId))
// "medium": the image at most 640 px wide (WebP); thumbnails ask a smaller size of the same link
return list.map((t) => ({ ...t, imageUrl: t.image ? files.url(t.image, { size: "medium" }) : null }))
}),
setImage: mutation({ id: id("todos"), image: file().optional() }, function* ({ id, image }) {
const todo = yield* ownTodo(id)
yield* (yield* Db).update(todos).set({ image: image ?? null }).where(eq(todos.id, id))
if (todo.image && todo.image !== image) yield* (yield* Files).delete(todo.image) // or let the cleanup do it
}),
// in a component (a modal, a form): FileInput uploads with progress, then calls onUpload with the id
<FileInput label="Image" accept="image/*" description="Up to 25 MB" onUpload={(image) => void setImage({ id, image })} />
{todo.imageUrl && <Thumbnail src={todo.imageUrl} alt={`Image of ${todo.text}`} size="sm" />} // sm md lg xl
// your own UI: const upload = useUpload(); const r = await upload(file) → r.value is the id; .progress, .waiting, .status
files.url(id)is a signed link (/_canica/files/<id>?e=…&s=…), the same for an hour and valid one more: whoever reads the row through your query gets it, nobody else (no link, no access). Filter queries by owner as for any data.yield* files.meta(id)→{ id, name, type, size, uploadedAt }(orNotFound).- Image sizes:
files.url(id, { size: "thumb" })(320 px wide),"medium"(640),"large"(1280),"full"(the original, the default), or{ width }(snapped up to 160/320/640/1280). The link grants that width (signed: a changed size is a 403) as WebP, resized once and kept; the page may ask any smaller width of the list.<Thumbnail>does, at twice its size (sm40 px,md64,lg160,xlup to 320), withwidth/heightand lazy loading: give lists and modals one"medium"link. Non-images, SVG and AVIF are served as they are; GIFs become their first frame. The dev server and accounts without Cloudflare's Images binding serve the original (headerx-canica-image: original; …). Resized copies are deleted with the file and do not count toward storage. - A mutation can store only an upload of the same person, or a file already stored in some row (else
FileNotFound). Uploads follow the app's access (see Access) and count as requests. - Files no
file()field refers to are deleted: 24 h after upload if never stored, within the hour once their row was deleted or changed (or right away withfiles.delete(id)in a mutation).file()cannot sit insidearray/object: several files are a child table. - Limits: 25 MB a file, 5 GB per app (
canica usage); failures areFileTooLarge,StorageFull(FileInput words them). - Served safely: images (not SVG), PDF, audio, video and plain text show in the page; anything else (HTML, SVG, …) downloads. Agents (MCP) get absolute links.
- Actions:
yield* ctx.files.store(response | blob | bytes, { name, type? })→ id (then store it withctx.runMutation),yield* ctx.files.get(id)→{ name, type, size, bytes, text() },ctx.files.url,meta. - Tests:
const image = await t.as("ana").uploadOk(new Blob([bytes], { type: "image/png" }), "cat.png"),await t.fetch(row.imageUrl)(aResponse; sized links come back from a fake resizer as 6 bytes ofimage/webp); scenes:upload(label("Image"), new File([bytes], "cat.png", { type: "image/png" })). The dev server keeps files in.canica/files(reset with the data).