DocsData
Files and images
Uploads kept with their records, under their rules, and images resized on request.
A file field stores an upload with its record, under the same rules. Sluurp resizes, crops and converts images on request, so a thumbnail costs only a thumbnail’s bytes. To upload a file:
const photos = sluurp.collection("photos");
await photos.upload(record.id, "image", input.files[0]);
// or make the record and upload in one step
await photos.createWithFile({ caption: "Team photo" }, "image", file);Features include:
- Uploads under the record’s rules: updating to upload, viewing to download
- Images resized, cropped and converted to JPEG, PNG or WebP, each size cached
- Files on disk, or in any S3-compatible bucket for several servers
- Storage quotas per project and per user
Upload
Over HTTP, send a multipart POST /api/files/{collection}/{id}/{field} with a part named file.
Uploading counts as updating the record, so the update rule applies. Downloading counts as viewing it, so the view rule applies.
Resize images
To ask for an image at a given size, pass the size to fileUrl(). For several widths at once, use srcset():
img.src = photos.fileUrl(record.id, "image", { w: 320, h: 320, fit: "cover" });
img.srcset = photos.srcset(record.id, "image", [400, 800, 1200]);| Parameter | |
|---|---|
w, h | Width and height, up to 4000 |
fit | contain (the default: fit inside the box) or cover (fill the box, cropped) |
format | jpeg, png or webp |
q | Quality, for jpeg and webp |
Sluurp makes each size once and caches it, under the file and the size, so a new upload never serves an old image. You can clear the cache at any time; sizes are made again when asked for.
Private files
Private files need the user’s token, which an <img> can’t send. To show one, use fileObjectUrl(id, field), which answers a URL an <img> can use. To read a file as text, use fileText(id, field).
Storage
By default, files are kept on disk next to the database, in the data folder’s storage/, or wherever SLUURP_FILES says. See Storage.
For several servers, set up an S3-compatible bucket (AWS, Cloudflare R2, MinIO, Backblaze) with environment variables. Sluurp then copies uploads to the bucket, and a server that lacks a file fetches it from there:
SLUURP_S3_BUCKET=app-files
SLUURP_S3_ENDPOINT=https://<account>.r2.cloudflarestorage.com
SLUURP_S3_REGION=auto
SLUURP_S3_ACCESS_KEY_ID=…
SLUURP_S3_SECRET_ACCESS_KEY=…Quotas
To limit storage, set a quota per project and per user in the admin. Sluurp refuses an upload that would go over it before writing anything. With no quota, there is no limit.
The admin shows images in their records, and lets you replace or remove them.