matter.work
Building Apps / The SDK

Files

Upload files into files fields, and show them in your pages.

Rows and tasks hold files in files fields ("type": "attachment" in your manifest): a list of files, each { id, name, size, mime_type, url }. To put a file in one, upload it with matter.files.upload, then write what it gives back into the field, the same as any other value. Since Matter 0.5.9; set minMatterVersion to it if you use them.

A page: an image into a task's files field
const input = document.querySelector<HTMLInputElement>('#image')!
const file = await matter.files.upload(input.files![0])
// -> { id, name, size, mime_type, url }
const task = await matter.board('content').get(id)
await matter.board('content').update(id, { values: { media: [...(task!.values.media ?? []), file] } })

In a worker or the app's tools (Node), pass the bytes and a name. The type comes from the name's extension unless you give one:

Node: a chart the agent made
import { readFile } from 'node:fs/promises'
const chart = await matter.files.upload(await readFile(path), { name: 'q3-hours.png' })

Showing files#

Reads give every file its url, which a page can load as it is: an <img>, a link, a download. matter.files.url(file, { width }) gives a smaller copy of an image, that many pixels wide, for thumbnails:

Thumbnails
for (const file of task.values.media ?? [])
  if (file.mime_type.startsWith('image/')) img.src = matter.files.url(file, { width: 240 })!

Reading Files#

In a worker or tool, matter.files.read(file) gives a file’s bytes and matter.files.download(file) a copy on disk, under MATTER_DATA_DIR in the file’s own name. Both fetch it as the member. Downloads stream, so any size will do, and stay: a file never changes, so the next call for it is instant. Since Matter 0.5.27.

Node: a task's video, for ffmpeg
const [video] = task.values.media ?? []
const path = await matter.files.download(video)
// -> …/files/0190…/launch.mp4

They take a file as a read gave it (or an item from matter.docs.list), its id or its url. Any file in the app’s workspace will do. One that was deleted, or isn’t there, fails with not_found.

What Matter stores#

  • Any file, any size: zips, videos, datasets, whatever your app works with. Files keep their type, which reads give back as mime_type. Since Matter 0.5.26; before it, only images, PDFs and text files up to 20 MB.
  • Limits are the field's: a files field can take only some types (fileTypes), files up to a size (maxFileSize), or one file (allowMultiple: false), set in your manifest or by people in the field's settings. A write that adds a file outside them fails with invalid. A field of one file reads as that file, and writes take it alone or as a list of one.
  • Who can see them: a file's url is the file, as Matter's own files fields and docs serve theirs: anyone holding it can load it. Write it only where people who should see the file can read.
  • Writes: a files field takes a list of files as reads and uploads give them; writing the list replaces it, so add to what a read gave to keep the others.