# Files

Every app with a backend can store private files. There is nothing to declare. IT can turn this off for a workspace.

The address `https://files.boringcloud.internal` exists only for your backend. The platform keeps each app's files apart.

| Call                                        | Answer                                                                           |
| ------------------------------------------- | -------------------------------------------------------------------------------- |
| `PUT /<key>` with a body and `Content-Type` | `201 { key, size }`. Replaces an existing file                                   |
| `GET /<key>`                                | the file with its type, or `404`                                                 |
| `HEAD /<key>`                               | the type and size only                                                           |
| `DELETE /<key>`                             | `204`, even when the file did not exist                                          |
| `GET /?prefix=invoices/&after=<last key>`   | `{ files: [{ key, size, contentType, updatedAt }] }`, at most 200, sorted by key |

A key is a relative path of at most 500 characters, such as `invoices/2026-01.pdf`, without `..` or empty parts. Encode it in the address: `key.split("/").map(encodeURIComponent).join("/")`.

## Upload

In the page:

```js
await fetch("/api/photos", { method: "POST", headers: { "content-type": file.type }, body: file });
```

In the backend:

```js
const TYPES = { "image/png": "png", "image/jpeg": "jpg", "application/pdf": "pdf" };

if (url.pathname === "/api/photos" && request.method === "POST") {
  const type = request.headers.get("content-type") ?? "";
  const ext = Object.hasOwn(TYPES, type) ? TYPES[type] : null;
  if (!ext) return json({ error: "Choose a PNG, JPEG or PDF file." }, 400);
  const body = await request.arrayBuffer();
  if (body.byteLength > 5 * 1024 * 1024) return json({ error: "The file can be at most 5 MB." }, 413);
  const key = `photos/${crypto.randomUUID()}.${ext}`; // never the name sent by the browser
  const put = await fetch(`https://files.boringcloud.internal/${key}`, {
    method: "PUT",
    headers: { "content-type": type },
    body,
  });
  if (!put.ok) return json({ error: (await put.json()).message }, 502);
  return json({ key }, 201);
}
```

Keep the key, the original name and the owner in the [database](https://boring-cloud.app/docs/database.md).

## Serve

A browser cannot reach the files directly. Serve them through your backend, after checking that this person may see this file:

```js
const match = url.pathname.match(/^\/api\/photos\/([a-f0-9-]{36}\.(?:png|jpg|pdf))$/);
if (match && request.method === "GET") {
  const file = await fetch(`https://files.boringcloud.internal/photos/${match[1]}`);
  if (!file.ok) return json({ error: "Not found." }, 404);
  return new Response(file.body, { headers: { "content-type": file.headers.get("content-type") } });
}
```

## Limits and errors

10 MiB per file and 1 GiB per app. Errors have the form `{ "error": "<code>", "message": "…" }`.

| Status | `error`             | Meaning                                                     |
| ------ | ------------------- | ----------------------------------------------------------- |
| 400    | `invalid_key`       | the key is not a plain relative path                        |
| 403    | `files_disabled`    | IT turned file storage off. Reading and deleting still work |
| 404    | `not_found`         | no file with this key                                       |
| 413    | `payload_too_large` | more than 10 MiB                                            |
| 507    | `storage_full`      | the app used its 1 GiB                                      |

A rollback does not change files.
