Skip to content

Files

A file field is a text column that holds the file’s URI. The bytes live with a provider, a named place for files: a folder on disk or an S3-compatible bucket. Protobase adds no table, trigger or foreign key to your database: a file field needs a text column and nothing else.

docs-examples/files/bikes.ts
import { f, resource } from '@protobase/schema'
import { aspectRatio, fileSize, imageSize, processor } from '@protobase/server'
// A processor of your own reads the whole file when it needs to: here a SHA-256, to spot the same manual uploaded twice.
const sha256 = processor({
run: async (file) => {
const digest = await crypto.subtle.digest('SHA-256', await new Response(await file.read()).arrayBuffer())
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('')
},
})
// A bike shop's bikes for sale: a photo with its size in two more columns, a manual, and a public picture for the
// shop's website, stored at most 2048 pixels wide as WebP, with a card-sized copy and a placeholder.
export const bikes = resource('bikes')
.table('shop.bikes')
.fields({
id: f.integer().readOnly().dbDefault(),
name: f.text(),
photo: f.file().accept(['image/*']).maxSize('10 MB').optional().derive({ photo_width: imageSize('width'), photo_ratio: aspectRatio() }),
photo_width: f.integer().readOnly().optional(),
photo_ratio: f.decimal({ precision: 8, scale: 4 }).readOnly().optional(),
manual: f.file().accept(['application/pdf']).optional().derive({ manual_bytes: fileSize(), manual_sha256: sha256 }),
manual_bytes: f.integer().readOnly().optional(),
manual_sha256: f.text().readOnly().optional(),
listing: f
.file()
.accept(['image/png', 'image/jpeg', 'image/webp'])
.maxSize('2 MB')
.public()
.optional()
.placeholder('listing_placeholder')
.variants({ card: { width: 640, height: 480, fit: 'cover', format: 'webp', quality: 75 } }, { original: { width: 2048, format: 'webp' } }),
listing_placeholder: f.text().readOnly().optional(),
})
.primaryKey((r) => r.id)
Option
.accept(['image/*', 'application/pdf']) Allowed types: exact types and type/* wildcards (*/* must be written out). They are checked against the type detected from the content (see How the type is decided). Without it any type is taken.
.maxSize('10 MB') The largest upload, as bytes or text (512 KiB, 1.5 GB); default 50 MB.
.public() Stored with the public provider, whose files anyone with the link may open.
.storage('archive') Stored with another provider; default private.
.typeField('attachment_type') Each file’s media type in that text field, which responses and downloads read, see Type and size fields.
.sizeField('attachment_size') Each file’s size in bytes in that integer or bigint field, see Type and size fields.
.derive({ field: processor }) Values computed from each upload and written to other fields of the row, see Derived fields.
.placeholder('photo_placeholder') Each image’s ThumbHash in that text field, drawn while the image loads, see Image variants and placeholders.
.variants({ card: { … } }, { original? }) Resized copies of each image stored beside it, see Image variants and placeholders.
.externalUrls() Also takes an https URL of a file elsewhere instead of an upload, see External URLs.
.capture(), .capture('user') Opens the rear camera, or the front one with 'user', on phones and tablets, and takes only images, see Camera capture.

A file field cannot be filterable, sortable, aliased, searched, sensitive, or the key, tenant, owner or soft delete field. protobase doctor checks that its column is a text type.

private:1/2992db9c-776e-4d4e-9e0f-59d485fefaf9.jpg?name=bike+red.png&size=48211
  • The scheme is the provider’s name, so a provider can move from local disk to a bucket without touching any row. Name the role (archive), not the vendor: the name is part of every stored value.
  • The path of an upload is <tenant>/<random id>.<extension>, with _ for a resource without a tenant. Nothing in it comes from the row or from what the user typed, so renames and key changes need nothing.
  • The query holds the uploader’s file name and the size in bytes, and for an image with variants their names and extensions: &variants=card.webp,thumb.webp. A plain {provider}:{path}, such as public:images/test.jpg written by an import, is valid too; its name is its last path segment.
  • The type is not stored in the value. It is the path’s extension, looked up in the built-in table and the app’s own types, which is also what downloads are served as, unless the field has a type field.
  • A field with .externalUrls() may hold an https URL instead, such as https://images.example.com/listings/42.jpg, see External URLs.

Responses carry each file field as an object, and the stored value stays the URI:

{ "uri": "private:1/2992db9c-….jpg?name=bike+red.png&size=48211", "name": "bike red.png", "type": "image/jpeg", "size": 48211,
"url": "/api/files/private/1/2992db9c-….jpg?r=bikes&k=42&f=photo&exp=1791673200&sig=…" }

An image with variants also has variants, each { url, type, width, height } by name, and one with a placeholder its placeholder. An external URL comes as { uri, name, type, url } with the URL as both uri and url. A value that does not parse, or names a provider the config lacks, comes back as { uri, missing: true }. The URL is made for each response and never changes the record’s ETag.

protobase.config.ts
import { defineConfig } from '@protobase/config'
export default defineConfig({
files: {
providers: {
private: { kind: 'local', dir: './data/files/private' },
public: { kind: 'local', dir: './data/files/public', public: true },
archive: { kind: 'local', dir: '/mnt/archive', retention: '30 days' },
},
retention: '1 day',
// Makes the image variants and placeholders
images: 'sharp',
},
})
  • { kind: 'local', dir, public?, publicUrl?, retention? } keeps files in a folder on local disk. Writes go to a temporary file first and are renamed into place, so nobody reads half a file. A full disk or quota answers 507.
  • { kind: 's3', bucket, prefix?, endpoint?, region?, public?, publicUrl?, retention? } keeps them in an S3-compatible bucket, see S3-compatible buckets.
  • { kind: 'custom' } is a FileProvider of the project’s own, the default export of files/<name>.ts.
  • Without config, private and public are the folders data/files/private and data/files/public, created when needed. Any other provider a field names must be configured.
  • PROTOBASE_FILES_<NAME> configures a provider from the environment and wins over the config for that name: PROTOBASE_FILES_PRIVATE=file:///data/files/private, or file:///data/files/public?public=1&public_url=https://cdn.example.com/app&retention=0, or an s3:// URL. PROTOBASE_FILES_RETENTION overrides files.retention.
  • Uploads and download links are signed with files.secret, else PROTOBASE_FILES_SECRET, else the app’s secret (PROTOBASE_SECRET). The server does not start with file fields and none of them set.

maxBytes ('1 GB', as for .maxSize()) and maxFiles cap what one provider holds, for either kind; in a URL they are max_bytes=1GB&max_files=20000. An upload that would pass either is refused with 507, and one that passes maxBytes while it streams stops there and leaves nothing. A file put again at its path takes no second file and counts only what it grows past the one it replaces; puts to one path wait for each other, so each replaces what the one before left. Scheduled deletes (.cleanup/) do not count, so a full provider can still be emptied.

A bucket has no quota of its own, so the provider counts what it holds with list on the first upload, and again once that count is five minutes old; in between it adds its own uploads. Several instances, or files added by hand, can therefore pass a limit by what they wrote since the last count.

A seed, an import or another script that writes files outside a request uses storeFile from @protobase/server, so the same script fills the folder locally and the bucket where the app has one. It finds the provider as the server does, from env, and from files when the script passes the config’s, as it is:

docs-examples/files/seed.ts
import { storeFile } from '@protobase/server'
import config from './protobase.config'
// A row then holds public:images/test.png
export const seedImage = (bytes: Uint8Array) => storeFile({ provider: 'public', path: 'images/test.png', body: bytes, files: config.files, env: process.env })

A custom provider is the project’s own code, so the script imports the default export of files/<name>.ts and passes it in files.providers in place of { kind: 'custom' }.

body is bytes or a stream. A relative folder, such as the default data/files/public, is taken from the working directory, so run the script where the server runs. A script that makes variants stores each at variantPath(path, name, extension) the same way.

An s3 provider stores files in any bucket with the S3 API, such as Cloudflare R2, Scaleway Object Storage or AWS S3, under prefix when given, so one bucket can hold several providers or apps:

// Cloudflare R2, in the EU jurisdiction
private: { kind: 's3', bucket: 'acme-files', prefix: 'private', endpoint: 'https://<account id>.eu.r2.cloudflarestorage.com', region: 'auto' },
public: { kind: 's3', bucket: 'acme-files', prefix: 'public', endpoint: 'https://<account id>.eu.r2.cloudflarestorage.com', region: 'auto', publicUrl: 'https://files.acme.com' },
// Scaleway, in Paris
archive: { kind: 's3', bucket: 'acme-archive', endpoint: 'https://s3.fr-par.scw.cloud', region: 'fr-par', retention: '30 days' },

Or from the environment, the same providers:

Terminal window
PROTOBASE_FILES_PRIVATE=s3://acme-files/private?endpoint=https://<account id>.eu.r2.cloudflarestorage.com&region=auto
PROTOBASE_FILES_PUBLIC=s3://acme-files/public?endpoint=https://<account id>.eu.r2.cloudflarestorage.com&region=auto&public_url=https://files.acme.com
PROTOBASE_FILES_ARCHIVE=s3://acme-archive?endpoint=https://s3.fr-par.scw.cloud&region=fr-par&retention=30d
  • Credentials never go in the URL. A provider set from the environment reads PROTOBASE_FILES_<NAME>_ACCESS_KEY_ID and PROTOBASE_FILES_<NAME>_SECRET_ACCESS_KEY, else S3_ACCESS_KEY_ID and S3_SECRET_ACCESS_KEY, else AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, and the server does not start without a pair. In the config, pass accessKeyId and secretAccessKey, or leave them out for the S3_ or AWS_ pair. The key needs to read, write, list and delete objects in the bucket.
  • Uploads still go through Protobase’s upload route, so types are checked and processors run; the body streams on to the bucket in parts, and an upload that fails stores nothing. Each object is stored with the type its upload was decided to be, the app’s own types included, and the disposition that type is served with. A bucket keeps the parts of an upload that never finished until they are aborted: R2 aborts them after 7 days, on Scaleway and AWS add a lifecycle rule that aborts incomplete multipart uploads after a day.
  • Private files keep their signed url. A download checks the link and the row as above, then redirects with 302 to a presigned link to the bucket that lasts as long as the app’s link, served with the same type and disposition. The bytes never pass through the app.
  • Public files are linked at publicUrl followed by the prefix and the path, such as https://files.acme.com/public/_/2992db9c-….png: the bucket’s own public address, an R2 custom domain or a Scaleway public bucket. Without publicUrl, /api/files/<provider>/<path> redirects to a presigned link instead.
  • The bucket serves its files itself, so the nosniff and sandbox headers of the app’s own downloads do not apply. The type is always the one the upload was decided to be, never HTML, and the bucket is on another domain than the app. A page that fetches file URLs, rather than showing them in <img> or linking them, needs CORS on the bucket.
  • It runs on Bun, whose built-in S3 client it uses: protobase-serve.js, the protobase image, or the CLI started with Bun. On Node the first file operation fails.

Uploads go through Protobase’s own route, so the type is checked and processors run on the bytes:

  1. POST /api/v1/{resource}:upload?field=photo&name=bike.png with the file as the body. A multipart/form-data body with exactly one file part works too, using that part’s name and type; it is read whole, so the raw body is better for large files. The caller needs create or update on the resource and write access to the field.
  2. The server reads the first bytes, decides the type and refuses a type the field does not allow with 415, before anything is stored. A Content-Length over maxSize gets 413 before a byte is read, and a body that turns out larger stops there with 413.
  3. The rest streams to the provider while it is counted. The answer is 201 { value, file: { name, type, size }, derived, corrected? }.
  4. Send value as the field’s value in a create, update or batch write within 24 hours. It is a ticket, signed for that user, tenant, resource and field: the write stores the clean URI and the derived values in the same transaction.

A write takes only a ticket, null to clear the field, or the stored uri unchanged, and in a field with .externalUrls() an https URL. Anything else is refused with 400, so a row can only point at a file its writer uploaded for that field, or at a URL elsewhere where the field allows one. From the client, client.upload(resource, field, file, { onProgress }) does step 1; see Client.

In the admin app’s create and edit forms, a file field takes a dropped or chosen file, limited to the field’s .accept() types, and uploads it at once with a progress bar; Save then stores it with the record. A stored file shows its thumbnail or an icon, its name linking to the file, its type and size, and the type it was sent as when the server corrected it. Replace picks another file, and Remove, for an optional field, clears it. Tables show the file’s name, beside a small thumbnail for an image.

A field with .capture() opens the camera directly on phones and tablets, so a photo is taken and attached in one step, such as a mechanic photographing a meter. The rear camera opens by default, and .capture('user') opens the front one.

meter_photo: f.file().capture(),
selfie: f.file().capture('user').optional(),

The field takes only images: the image types of its .accept(), or any image when it names none, and an .accept() after it that names another type, or none, is an error. On a phone or tablet the form offers Take a photo, which opens the camera, and Choose a photo, which opens the phone’s own chooser for a photo taken before; a stored photo offers Retake beside Replace. A desktop browser has no camera to open, so the field picks or takes a dropped image file.

An upload carries three hints: what the content is (C, from its magic bytes), what the name’s extension says (E), and what the client declared (H, the request’s or the file part’s Content-Type; application/octet-stream counts as none).

Content Type used When the hints disagree
Recognized: images, PDF, archives, Office files, audio, video, executables, … C C wins and the answer reports corrected: { from, to }: a JPEG sent as photo.png is image/jpeg, and an executable named invoice.pdf is refused by accept: ['application/pdf'].
Not recognized, and text (UTF-8 without NUL bytes) E when it is a text type (csv, tsv, txt, md, html, json); H when there is no extension; else text/plain E wins over H: HTML named .csv is stored as text/csv. HTML and SVG are never taken as images.
Not recognized, and binary E, only when accept lists that type exactly and H does not disagree; else application/octet-stream Any disagreement gives application/octet-stream, which passes only an accept that names it.

The decided type picks the stored extension, and downloads are served by that extension, so nothing posing as a PDF is ever served as something else. An HTML file is stored as text/html but always downloads as an attachment that never renders on the app’s origin; the admin app opens it in its HTML viewer.

files.types adds the app’s own media types, each with its extensions, the first being the one its files are stored with. A file named with such an extension whose content is not a recognized type takes that type: weekly.report.json below is stored and served as application/vnd.example.report+json, while weekly.json stays application/json. An extension may hold dots, and the longest one a name ends in counts. A malformed type, a built-in type or a built-in extension stops startup with an error naming it.

export default defineConfig({
files: { types: { 'application/vnd.example.report+json': ['report.json'] } },
})

Such files download as attachments. A viewer opens them in the admin app instead.

GET /api/files/<provider>/<path> on the app’s own host:

  • Private files need the signed url from a response, made only for a caller who may read that row and field. It needs no token, so it works in an <img src>, and it is valid one to two hours (the same URL within the hour, so browsers cache it). Each download checks that the row still holds the file and that the file sits under that row’s tenant: a replaced or cleared file stops downloading at once, and a URI copied into another tenant’s row by SQL is never served.
  • Public files are served to anyone at a permanent address, /api/files/public/<path>, cached for a year. publicUrl links them from elsewhere instead, such as a CDN in front of that route.
  • Every download is nosniff with Content-Security-Policy: sandbox; default-src 'none'. Raster images, PDF, audio and video are inline; everything else, SVG included, is an attachment. A private file is named as its uploader named it, with the extension matched to the type (photo.png holding a JPEG downloads as photo.jpg); a public file by its stored path. Range requests work.
  • A provider in a bucket answers with a redirect to the bucket instead, after the same checks, see S3-compatible buckets.
  • With ?via=app the app serves the bytes itself after the same checks, from a bucket too, so a page can read them with fetch without the bucket sending CORS headers. A viewer’s useFileText reads this way, and reads a public file linked at a publicUrl from /api/files/<provider>/<path>. Such a read takes files up to 10 MB and answers a larger one with 403.

A plugin, or the app’s own protobase.ui.tsx, registers a viewer for a media type (see File viewers). A file field’s link to a file of that type, on the record page and its form, then opens the viewer on the record’s address with ?file=<field>, such as /attachments/7?file=file, in place of a download: a bar with the way back to the record, the file’s name and size and a Download button, and the viewer below it. A file without a viewer links to its download as before; its own address with ?file=<field>, such as a page’s link to it, shows the bar and says there is no preview.

The viewer gets the file and its siblings: the record’s other files, and the files of the records of the same resource that point at the same records through filterable relations, read in one list call of at most 100 records. For attachments that each point at a task, the siblings of one are the other attachments of that task, so a report can show the images it names by file name.

HTML files open in a sandboxed frame that cannot reach your app. Relative images, videos and links resolve to the record’s other files. Your own viewer can draw the HTML it generates in the same frame and use the same link resolution.

Protobase brings this viewer itself, for text/html, the type an upload named .html or .htm is stored as; an app or plugin that registers its own text/html viewer replaces it. The page shows below the file’s bar, as tall as its content, so the app’s page scrolls. A page sized by its frame, such as one 100vh tall, would grow each time the frame did: the frame stays 60% of the screen tall for it, and the page scrolls inside. An empty page, measured 0 tall, keeps that height too:

  • The frame has an opaque origin: the page cannot read the app’s cookies or storage, call its API as the signed-in user, or navigate the app’s window. It loads nothing from the network, so an image or stylesheet from another site stays blank, and it submits no forms.
  • A relative src, poster, srcset candidate, <link href> or CSS url() in a style attribute or <style> element that names a sibling by its file name, such as shots/before.png or ./clip.webm, shows that sibling. The admin app reads each such sibling once, with the signed-in user’s rights, and hands it to the page as its bytes. A name with escapes, such as my%20chart.png, matches my chart.png. A reference to no sibling, a path from the root or one that climbs out with .. stays as written, so it shows nothing, and such a link becomes plain text.
  • A link to a sibling opens it on its record’s address with ?file=<field>, the address the link shows: in its viewer, such as another HTML page, or beside its Download button when it has none. A middle or modified click opens it in a new tab. A link to an http or https address opens it in a new tab that cannot reach back to the app. A link to a place on the page, such as #results, scrolls the app’s page to it and moves the focus there. Any other link, such as javascript:, does nothing.
An HTML file’s siblings over 10 MB, such as a long video, cannot show in its page because reading larger files in the app is not implemented. A url() inside a linked stylesheet cannot name a sibling because rewriting stylesheet files is not implemented. A link to a place on a page that scrolls inside its frame cannot scroll the frame to it because moving the frame’s own scroll from the link script is not implemented.

Your own viewer draws a page in the same frame with HtmlFrame, and resolves its references with useSiblingPage, such as for a page it builds from a template and a JSON file:

import { HtmlFrame, useFileText, useSiblingPage, type FileViewerProps } from '@protobase/ui'
// renderReport is your own template: the report's JSON in, a page out.
const ReportViewer = ({ file, siblings }: FileViewerProps) => {
const read = useFileText(file)
const page = useSiblingPage(read.data && renderReport(JSON.parse(read.data)), siblings)
if (!page) return null
return <HtmlFrame html={page.html} title={file.name} links={page.links} onLink={(link) => link.kind === 'external' && window.open(link.url, '_blank', 'noopener,noreferrer')} />
}
HtmlFrame props
html The page, a whole document or a fragment.
title The frame’s accessible name, such as the file’s name.
links The targets of the page’s links, as useSiblingPage gives them: { kind: 'file', file } for a sibling, { kind: 'external', url } for an http or https address.
onLink (link, { newTab }), called when the page’s link to link is clicked; newTab for a middle or modified click. Nothing else in the page navigates.
scripts 'bridge' (the default) runs none of the page’s scripts. 'trusted-page' runs its inline scripts, for a page your viewer generates itself; the frame keeps its opaque origin and still loads nothing from the network.

useSiblingPage(html, siblings, address?) gives { html, links } once html is there and the siblings it names are read, and undefined until then. address(link) gives the address each link shows, for the browser’s link preview and copy link, such as the sibling’s own viewer address; without one a link keeps the address its page gave it. siblingPage(html, siblings, contents, address?) does the same rewriting without reading, with each sibling’s data: URL in contents by its uri.

Beside its HTML viewer, Protobase brings no viewer of its own. Two plugins, each a package of its own, open images and PDFs in the app; an app that wants them installs them and extends its protobase.ui.tsx with them:

Terminal window
npm install @protobase/image-viewer @protobase/pdf-viewer
protobase.ui.tsx
import { defineUi } from '@protobase/ui'
import imageViewer from '@protobase/image-viewer'
import pdfViewer from '@protobase/pdf-viewer'
export default defineUi({ extends: [imageViewer, pdfViewer] })

Each viewer’s code is a chunk of the app’s build that loads only when such a file opens, so an app that extends them loads nothing more until then. On a phone both are as wide as the screen. A file a viewer cannot draw shows the Download button in its place.

  • @protobase/image-viewer opens PNG, JPEG, GIF, WebP, AVIF and BMP, the images downloads serve inline: fitted to the page, with its size in pixels and an Actual size button that shows it pixel for pixel, scrolling in its own box when it is wider than the page. SVG stays a download.
  • @protobase/pdf-viewer opens PDF files with an open-source PDF renderer that runs in a web worker: the pages one under the other, as wide as the page up to 60rem, each drawn when it comes within a screen of being shown and let go again once it is further away, so a long PDF holds only the pages around the screen. Scanned pages in JPEG 2000 or JBIG2 are drawn with the renderer’s decoders, which the viewer brings. The renderer reads the file with fetch, so a PDF served from another origin than the app’s, from a bucket or an external URL’s host, opens only when that host allows the app’s origin with CORS; otherwise the viewer shows the Download button. The PDF viewer cannot select or search a page’s text because a text layer is not implemented, so the page is an image and its text is read in the download. The PDF viewer cannot draw text that uses one of the renderer’s predefined CMaps, as many Chinese, Japanese and Korean PDFs do whether or not they embed the font, because bringing those CMaps is not implemented, so such text is read in the download.

Each package also exports its viewer component (ImageViewer, PdfViewer), for an app that registers it for a type of its own in fileViewers.

.typeField() and .sizeField() keep each file’s media type and size in columns of their own, so a list can show them and SQL can count and sum by them:

attachment: f.file().optional().typeField('attachment_type').sizeField('attachment_size'),
attachment_type: f.text().readOnly().optional(),
attachment_size: f.bigint().readOnly().optional(),
select attachment_type, count(*), sum(attachment_size) from attachments group by attachment_type
  • Both fields must be readOnly(), as derived fields are: an upload writes them with the file in the same transaction, and clearing the file sets them to null. The type field is text; the size field an integer, or a bigint for files past 2 GB.
  • An upload writes the type it was decided to be and its size in bytes.
  • An external URL writes the type its extension names, the app’s own types included, and null when the table knows no such extension. Its size field stays null, since Protobase never fetches it.
  • An import that writes rows itself may write both beside the value, such as the type of a file stored under a name with no extension. Leave a field null when the source does not say; never guess.
  • Responses and viewers take the file’s type and size from these fields when the row holds them, else from the value.
  • A private download is served as the type field’s type when that is a type the app’s table knows, and HTML only ever as an attachment; any other value is served by the path’s extension. A public file has no row to read, so it is served by its path, and a bucket serves it with the type its upload was decided to be.

Files stored before a field declared them keep null until they are uploaded again or an import fills them.

.derive({ field: processor }) computes values from each upload into other fields of the same resource. Those fields must be readOnly(), so only the upload sets them.

Processor Gives
imageSize('width'), imageSize('height') an image’s size in pixels as shown (EXIF rotation applied), for an integer field
aspectRatio({ decimals? }) width divided by height as decimal text, 4 places by default, for a decimal field
fileSize() the size in bytes, for an integer field to sort or filter on
fileType() the detected type, for a text field
processor({ accepts?, run }) your own: run(file) gets { name, type, size, head, read() }, where head is the first 256 KB and read() streams the whole file

Processors run once, on the upload route, after the file is stored. A processor whose accepts does not match the type leaves its field null, so a field that takes images and PDFs gets a width only for images. The values travel signed inside the ticket and are written with the file in the same transaction; replacing the file recomputes them and clearing it sets them to null. A value the field’s own schema refuses is an error in the processor. Files stored before a processor was added keep their fields as they are, as processors do not run on existing files.

.placeholder() and .variants() process each image as it is uploaded, with sharp:

photo: f.file().accept(['image/*']).public()
.placeholder('photo_placeholder')
.variants({
card: { width: 800, height: 600, fit: 'cover', format: 'webp', quality: 75 },
thumb: { width: 96, height: 96, fit: 'cover', format: 'webp' },
}),
photo_placeholder: f.text().readOnly().optional(),
  • .placeholder('photo_placeholder') computes the image’s ThumbHash, about 30 characters of base64 that hold its shape and colours, into that read-only text field, written with the file like a derived field. The admin app draws it blurred wherever it shows the image, until the image has loaded.
  • .variants({ name: variant }) stores resized copies beside the file, each at most width by height pixels (either may be left out) and never enlarged. fit: 'inside', the default, keeps the whole image in that box; 'cover' fills the box and crops the rest, and needs both sides. format is webp, avif, jpeg or png, and quality 1 to 100, the format’s default when left out. Names are snake_case. A variant is stored at the file’s path followed by its name and extension (…/2992db9c-….jpg.card.webp) with the same provider, served and deleted with the file. The admin app shows tables, forms and cards with the smallest variant large enough for them.
  • { original: variant }, as .variants()’ second argument, stores that image instead of the upload, so the upload is not kept: { original: { width: 2560, format: 'webp' } } stores at most 2560 pixels wide as WebP.

Variants and placeholders turn the image upright first, following its EXIF orientation, and carry no metadata. JPEG, PNG, WebP, AVIF, GIF and TIFF images are processed; any other file, HEIC and SVG included, is stored as it is, without variants, and its placeholder is null. Files uploaded before a field declared variants keep none, as variants are not made for existing files.

sharp is a native package, so the config names it, the app loads it from the project’s dependencies when it serves, and a built bundle carries it with its add-on for linux:

protobase.config.ts
export default defineConfig({ files: { images: 'sharp' } })

The server does not start with a field that declares variants or a placeholder and no files.images. In code, fileUrl(file, 'card') from @protobase/client links a variant by name, or the file itself when it has none.

.externalUrls() lets a file field hold the https URL of a file another site serves, instead of an upload: an import that links a supplier’s product photos, say, rather than copying them into the app’s storage.

photo: f.file().accept(['image/*']).public().externalUrls()
.placeholder('photo_placeholder')
.variants({ card: { width: 800, height: 600, fit: 'cover', format: 'webp' } }),
Terminal window
curl -X PATCH https://app.example.com/api/v1/products/42 -H "authorization: Bearer $TOKEN" -H 'if-match: *' \
-H 'content-type: application/json' \
-d '{ "photo": "https://images.example.com/products/42/front.jpg" }'
  • What is taken: an absolute https:// URL with a host, without a user name or password, written without spaces or other characters a URL escapes. http:, data: and anything else is refused with 400, as in a field without the option.
  • The type is the URL path’s extension, from the same table as stored files, the app’s own types included, and must pass .accept(): an image field takes …/front.jpg and refuses …/front and …/scan.pdf. The name is the path’s last segment. A type field gets that type, and an import may write a better one there.
  • Stored as given: the column holds the URL itself, and Protobase never fetches, uploads or copies it. It has no size unless an import wrote one in the size field, and responses link it as it is, as url, for whoever may read the row.
  • No variants: every place an image is drawn, cards, tables, thumbnails, forms and the record page, uses the URL itself, and fileUrl(file, 'card') gives it too. Size the image at its source when it should be small.
  • No ThumbHash: a write through the API sets the field’s derived fields, placeholder and size field to null, since there are no bytes to compute them from; the image frame stays empty until the image loads. An import that writes the rows itself may write a placeholder beside the URL, and the admin app draws that one.
  • Nothing to delete: replacing an upload with a URL schedules the upload’s delete as any replacement does; a URL that is replaced or cleared schedules nothing.
  • The other site sees each viewer’s IP address when it serves the image, and decides when it changes or disappears. The admin app draws it without a referrer, see Image referrers.

The admin app draws every image of a file field with referrerPolicy="no-referrer", so another site never learns which page showed its image, unless the image comes from a host it trusts:

  • the page’s own origin, which serves the app’s downloads and the public files without publicUrl;
  • the host of each provider’s publicUrl, such as a bucket’s custom domain on another domain than the app;
  • the hosts of the app’s sources;
  • the hosts files.trustedHosts adds, as lowercase host names without a scheme, port or path:
export default defineConfig({
files: { trustedHosts: ['images.example.com'] },
})

A trusted host is matched exactly: images.example.com does not trust cdn.images.example.com. /meta names the trusted hosts besides the page’s own as files.trustedHosts. Download links are opened with rel="noreferrer" wherever they point.

Files are deleted by schedule, never in the write itself, so a restore of the database to an earlier moment still finds its files:

  • A write that replaces or clears a file, or deletes a row (not a soft delete), schedules the delete of each file it took out of its row, with its variants, once it commits, so its transaction never waits on the storage. It is due after the provider’s retention: files.retention, 1 day by default, or 0 for a public provider. When the storage refuses the entry, the write still stands, the file stays in storage, and the error goes to the default export of hooks/error.ts.
  • Every upload schedules one too, due an hour after its ticket expires, so a file nobody saved, or whose write rolled back, goes as well.
  • Each scheduled delete is a small JSON object in the provider’s own storage, in a .cleanup/ folder, named after its due time and saying what to delete: .cleanup/2026-10-12T03-00-00Z-<uuid>.json holding { "provider": "private", "path": "1/2992db9c-….jpg", "due": "…" }.
  • A cleanup run handles the entries that are due. Right before deleting, it checks every file column of every resource for the file: one that a row holds again (a rolled-back write, a restore, a copied value) is kept, and its entry dropped. Two runs at once do no harm. It looks the files up by split_part(column, '?', 1), the file without its query part, which an index on that expression serves; protobase doctor warns of each file column without one and writes it.

A run happens:

  • in protobase serve, every 60 minutes (PROTOBASE_FILES_CLEANUP_MINUTES, 0 turns it off);
  • with protobase files cleanup, from cron or a timer;
  • with POST /api/files:cleanup and an admin’s token, for a hosting platform. It answers { deleted, kept }.

The scheduled deletes sit behind a small interface (CleanupSchedule: add, due, done), so a table in a schema of its own, added by a migration at update time, can replace the folder later.

What this does not cover: a write made with SQL outside the API schedules nothing, so a file whose row SQL changed or deleted stays in storage. The check before deleting looks only at the file fields the config declares; a column holding file URIs that no resource declares is not protected.