Skip to content

Presenting a file

Some operations answer with a file rather than a record: an export, an invoice PDF, a label to print. The kernel declares that once, on the spec's presenter, and each transport reads the declaration when it registers the spec and answers in its own terms: a download over HTTP, a blob to an MCP client, bytes to an agent, a refusal where it has no answer for a file.

Three names carry it:

  • FileResult is the file: its content, its name, its media type and, when known, its size.
  • FilePresenter is the declaration, in the presenter seat every spec already has.
  • file_output is what a transport asks at registration.

A service that builds the file

def export_books(*, user: Any) -> FileResult:
    out = io.StringIO()
    writer = csv.writer(out)
    writer.writerow(["title", "price"])
    for book in Book.objects.order_by("title"):
        writer.writerow([book.title, book.price])
    return FileResult(out.getvalue().encode(), filename="books.csv")


export = ServiceSpec(
    service=export_books,
    permissions=[IsSignedIn()],
    presenter=FilePresenter("text/csv"),
)

The service returns a FileResult carrying its own name, and the presenter fills in the media type it declares. A FileResult that already names one passes through unchanged.

The content is bytes, a binary file object (a Django File, an open IO[bytes]) or an iterable of bytes, which a transport may stream. A str is refused, being text rather than a file, and so is an async iterable, which no transport streams yet. size is read from bytes or a Django File when it is not given, and is what an HTTP response sends as Content-Length.

A name that is safe to show

The filename is shown to a caller and quoted into a header, never joined to a path. So a FileResult refuses, when it is built, a name that is empty, . or .., or that holds /, \, a NUL or any other control character. A name built from row data is checked like any other.

A selector that renders a row

def render_label(book: Book) -> bytes:
    return f"{book.title}\n{book.price}\n".encode()


label = SelectorSpec(
    kind=SelectorKind.RETRIEVE,
    selector=lambda *, pk: Book.objects.filter(pk=pk),
    reads=Parameters.of(Parameter("pk", "integer", required=True)),
    permissions=[IsSignedIn()],
    presenter=FilePresenter(
        "text/plain",
        filename=lambda book: f"label-{book.pk}.txt",
        render=render_label,
    ),
)

When the value is not a FileResult, render(value) gives the content, and the presenter wraps it under filename: a string, or a callable of the value. Without render the value is the content. A value with no name to give it, neither from the presenter nor carried by a FileResult, is refused when it is presented.

A service spec with an output selector whose presenter is a FilePresenter presents a file too, through presenter_for_output().

What a transport does with it

def serve(spec: ServiceSpec | SelectorSpec, principal: Any, **arguments: Any) -> dict[str, Any]:
    declared = file_output(spec)
    if declared is None:
        raise ValueError("Not a file spec; serve it as JSON.")
    result = dispatch(spec, principal=principal, arguments=arguments)
    file: FileResult = present(spec, result)
    try:
        body = file.read(max_bytes=1_000_000)
    except FileTooLarge as refused:
        return {"error": f"over {refused.max_bytes} bytes"}
    return {"name": file.filename, "type": file.content_type, "body": body}

file_output answers the FilePresenter a spec presents through, or None for a spec whose result is JSON. It refuses, with ImproperlyConfigured, a file that would be one of many: a LIST selector spec, a service spec whose output selector is a LIST, or one declared many or returns_many. A list of files has no answer on any transport. It refuses a file presented with affordances too, since those are keys on an object. A service spec's own affordances only check the call, so they are allowed, and so is allow_none: a None is answered as the transport answers nothing for any other spec.

read(max_bytes=) gives the whole content and closes it. Past the ceiling it closes the content and raises FileTooLarge, which names the ceiling. A file object is never asked for more than the ceiling plus one byte in all, however short its reads run; an iterable's chunks are taken whole, so reading stops at the first chunk past the ceiling, and that chunk is in memory. A transport that refuses after presenting calls close().

present, present_for_audience and project_payload hand a FileResult on untouched: it is neither an object nor a list.

No JSON schema

A FilePresenter's output() is None, so spec.output() is None and spec_output_schema describes nothing. An empty Output() would advertise an object that never arrives.