Breaking: a job with a raw element prints only with a token issued with uberbringer token --raw. Every other token, every existing one included, gets 403 token_scope for it, with the raw elements in details, and keeps every other route. The notify module prints a notification card from a title, message, priority and tags, with topic, click, up to three actions, image, markdown and time as options; it decodes strictly and refuses a payload over its length caps. The ntfy module takes one ntfy message as ntfy sends it, such as the ntfy client's $NTFY_RAW, ignores fields it does not know, cuts text over the caps, and answers 204 for other events and binary bodies. Both print every message plain: one message cannot tell the first of a sequence from a replacement, since a publisher may choose the sequence ID. internal/pkg/ntfy holds ntfy's message types. Both print the same card. The priority picks the form: a dark URGENT rule and a double box, a double box headed HIGH, a single box with the title in its border, or a rule with no box. Tags print as markers in front of the title, the rest on a Tags row; links print as QR codes with their labels; attached PNG, JPEG, GIF and WebP images print after the card. A line-level markdown subset is laid out without a dependency, keeping marks inside code spans and URLs, and emoji are removed after a few typed ones become markers. A card that cannot be laid out prints as a plain receipt, and the server logs the fault once at ERROR and reports it to Sentry once. [ntfy.format] and [ntfy.markers] shape the cards; the markers table is a setting with the user's own keys, also settable as UBERBRINGER_NTFY_MARKERS=name=value,... uberbringer token --modules issues a token limited to those modules' print and preview routes, job status and the module list; each route in the table says which tokens it takes, and a scope claim that could read as absent makes the token invalid. image elements take optional, which also shrinks a too-wide image to the paper; a QR code that could be too wide for the paper prints with smaller modules; box takes title_bold. Submit takes a Meta whose source shows in job status and, with a copy of the request ID, in every worker log line of the job. The four ntfy cards passed on the TM-T88IV, posted as the client recipe posts them, and their bytes are pinned in TestPaperReceipts. docs/api.md describes the modules, the card and the client recipe, with tokens read from a header file; docs/swagger.json is regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
143 lines
4.2 KiB
Go
143 lines
4.2 KiB
Go
package receipt
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"net/url"
|
|
"path"
|
|
|
|
"uberbringer/internal/pkg/escpos"
|
|
)
|
|
|
|
// Preview is what a job prints, read back from its command stream.
|
|
type Preview struct {
|
|
// Columns is the number of Font A characters on a line.
|
|
Columns int
|
|
|
|
CodePage escpos.CodePage
|
|
|
|
// LineCount is the number of lines the job prints, all copies counted:
|
|
// text lines, fed lines, and one for each QR code, barcode and image.
|
|
// Options.MaxLines limits it.
|
|
LineCount int
|
|
|
|
// Lines are the printed lines. Image lines name the image's source
|
|
// instead of its size, since a preview loads no images.
|
|
Lines []escpos.PreviewLine
|
|
|
|
// Warnings name the elements with characters the code page lacks and
|
|
// images that would fail to print.
|
|
Warnings []string
|
|
}
|
|
|
|
// Preview validates and renders job as Render does, then reads the command
|
|
// stream back with escpos.Interpret, so that it shows what the bytes say. It
|
|
// neither fetches nor decodes images: each prints as a blank placeholder. A
|
|
// job over Options.MaxLines fails with a *LineLimitError, as it does when it
|
|
// prints.
|
|
func (r *Renderer) Preview(ctx context.Context, job Job) (Preview, error) {
|
|
if err := job.Validate(); err != nil {
|
|
return Preview{}, err
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return Preview{}, err
|
|
}
|
|
images, imageWarnings := r.placeholderImages(job)
|
|
out, err := r.render(job, images)
|
|
if err != nil {
|
|
return Preview{}, err
|
|
}
|
|
lines, err := escpos.Interpret(out.stream, r.options.CodePage, r.options.Profile, out.raw)
|
|
if err != nil {
|
|
return Preview{}, fmt.Errorf("reading back the rendered receipt: %w", err)
|
|
}
|
|
labelImages(lines, job, r.options.Profile.DotsPerLine)
|
|
|
|
var warnings []string
|
|
for i := range job.Elements {
|
|
if w, ok := imageWarnings[i]; ok {
|
|
warnings = append(warnings, w)
|
|
}
|
|
if n := out.unknown[i]; n > 0 {
|
|
noun := "characters"
|
|
if n == 1 {
|
|
noun = "character"
|
|
}
|
|
warnings = append(warnings, fmt.Sprintf("elements[%d]: %d %s not in %s printed as ?", i, n, noun, r.options.CodePage))
|
|
}
|
|
}
|
|
return Preview{
|
|
Columns: r.textLineDots() / r.options.Profile.FontAWidth,
|
|
CodePage: r.options.CodePage,
|
|
LineCount: out.lines,
|
|
Lines: lines,
|
|
Warnings: warnings,
|
|
}, nil
|
|
}
|
|
|
|
// placeholderImages returns a blank one-row raster across the paper for
|
|
// every image element, and a warning for each image that would fail to
|
|
// print because it is wider than the paper. An optional image that is
|
|
// wider shrinks to the paper instead.
|
|
func (r *Renderer) placeholderImages(job Job) (map[int]*image.Paletted, map[int]string) {
|
|
paper := r.options.Profile.DotsPerLine
|
|
images := make(map[int]*image.Paletted)
|
|
warnings := make(map[int]string)
|
|
for i, el := range job.Elements {
|
|
if el.Type != TypeImage {
|
|
continue
|
|
}
|
|
if el.Width > paper && !el.Optional {
|
|
warnings[i] = fmt.Sprintf("elements[%d] (image): width %d is wider than the %d-dot paper, so the job would fail", i, el.Width, paper)
|
|
}
|
|
images[i] = image.NewPaletted(image.Rect(0, 0, paper, 1), color.Palette{color.White, color.Black})
|
|
}
|
|
return images, warnings
|
|
}
|
|
|
|
// labelImages replaces the size of each image placeholder with the source
|
|
// of its element, in order, copy after copy. An optional image wider than
|
|
// the paper, paper dots, is labelled with the width it prints at.
|
|
func labelImages(lines []escpos.PreviewLine, job Job, paper int) {
|
|
var sources []Element
|
|
for _, el := range job.Elements {
|
|
if el.Type == TypeImage {
|
|
sources = append(sources, el)
|
|
}
|
|
}
|
|
n := 0
|
|
for i := range lines {
|
|
if lines[i].Kind != escpos.LineImage || len(sources) == 0 {
|
|
continue
|
|
}
|
|
el := sources[n%len(sources)]
|
|
n++
|
|
name, data := "base64", "base64"
|
|
if el.URL != "" {
|
|
name, data = imageName(el.URL), el.URL
|
|
}
|
|
lines[i].Text = fmt.Sprintf("[image: %s]", name)
|
|
if width := el.Width; width > 0 {
|
|
if el.Optional {
|
|
width = min(width, paper)
|
|
}
|
|
lines[i].Text = fmt.Sprintf("[image %d dots: %s]", width, name)
|
|
}
|
|
lines[i].Data = data
|
|
}
|
|
}
|
|
|
|
// imageName is the last path segment of an image URL, or its host.
|
|
func imageName(raw string) string {
|
|
u, err := url.Parse(raw)
|
|
if err != nil {
|
|
return raw
|
|
}
|
|
if name := path.Base(u.Path); name != "." && name != "/" {
|
|
return name
|
|
}
|
|
return u.Host
|
|
}
|