Concepts

Image, file and upload fields

How to declare a photo, an avatar, a logo, a document or a gallery on a resource, what each one stores, and what you get in the admin without writing any of it.

stableProven: named tests, and the live suite drives it over HTTP on every push.What is tested

The short answer

grit generate resource Product --fields "cover:file[image],gallery:files[image]"

file is one. files is many. The brackets say what the field accepts, and leaving them off accepts anything the server allows. That is the whole syntax; everything below is what it does.

What that writes

Copied from a real run, not paraphrased. The Go model:

apps/api/internal/models/product.go
Cover *files.FileRef `gorm:"type:json" json:"cover"`
Gallery files.FileRefs `gorm:"type:json" json:"gallery"`

Note what it is not: a string holding a path. A FileRef is a JSON column carrying the key, the original filename, the size, the content type and the renditions, which is why the admin can show a thumbnail and a file size without a second request, and why deleting a record can delete its files.

And the admin field definition, which is where the accept list goes:

apps/admin/resources/products/products.ts
{ key: "cover", label: "Cover", type: "file", accepts: ["image"], maxSizeMB: 5 },
{ key: "gallery", label: "Gallery", type: "files", accepts: ["image"], maxSizeMB: 5, max: 5 },

You get a dropzone for each, single or multiple, with drag and drop, a preview, a progress bar and the accept list wired in. max: 5 is the default cap on how many files a files field takes; change it in that line.

What it accepts

An alias, or a bracketed list of them. Both of these are the same field with a different vocabulary, and both parse:

cover:file[image] # the bracket form, used everywhere in these docs
cover:file:image # the colon form, also accepted
docs:files[pdf, doc] # a list. Spaces after the commas are fine
attachment:file # no brackets: anything the server allows
AliasLets through
imagejpg, jpeg, png, gif, webp, avif, svg
videothe common web video container types
audiothe common web audio types
pdfapplication/pdf
docWord, Excel, PowerPoint, CSV and plain text
allanything the server allows. The same as leaving the brackets off.

The accept list is not only a browser hint

This is the part worth knowing, because in most stacks it is not true. The dropzone uses the list to filter the file picker, and it also travels to the server:

POST /api/uploads?accepts=image&max_size=5242880

The upload handler resolves those aliases itself through internal/files/accepts.go and refuses anything outside them, so a client that edits the request, or one that never used your form, gets the same answer. The size cap is enforced in the same place.

UPLOAD_ALLOWED_MIME in .env is the outer boundary for the whole project. A field's accept list can narrow it and cannot widen it.

Images get more than storage

An upload that is an image goes through the processing pipeline on the way in, with no configuration: it is resized, re-encoded and stripped of its EXIF, which matters more than it sounds because a phone photo carries the GPS coordinates of where it was taken. The renditions land in the same FileRef. Media and image processing has the profiles and the measured numbers.

Where the bytes go

Wherever STORAGE_DRIVER says: local needs nothing at all, minio is what docker compose up starts, and s3, r2 and b2 need credentials. The field declaration is the same in every case, which is the point of the FileRef being a reference rather than a path. File storage covers the drivers.

Three things that catch people

  • A file field is nullable and a files field is not. *files.FileRef can be null; files.FileRefs is an empty list when there is nothing in it. Write your frontend against that rather than against undefined.
  • Marking it required does not mean the upload succeeded. The form sends a reference, and the file arrived through a separate request before that. If you need the two to be atomic, check the reference in your service.
  • Renaming the field does not move the files. The key stored in the FileRef is where the object actually is. Changing the column name changes nothing in the bucket, which is usually what you want.

Every other field type

This page is only about files because it is the question asked most. The field types reference has all thirty-two, with the Go type, the TypeScript type, the Zod schema and the admin control each one produces.