CI / Lint, test and scan (push) Successful in 1m41s
Restructure the daemon to follow the project's Go coding standards and
expand its features.
Layout and tooling
- cmd/uberbringer/main.go is now thin; all logic lives under internal/.
- Module path is git.anomalous.dev/57_Wolve/uberbringer, go 1.26, every
dependency upgraded, vendor/ removed.
- staticcheck, govulncheck and goimports pinned as go tools; add a
Makefile, CI workflow, goreleaser v2 config and Alpine Dockerfile.
Features
- Background print queue with job status (GET /api/v1/jobs/:id), send
retries with backoff, and graceful shutdown.
- New {"elements": [...]} job format on POST /api/v1/print. Breaking:
replaces {"data": [...]}, and the response is now JSON 202.
- In-house ESC/POS library replacing justinmichaelvieira/escpos: sizes
1-8, Font B, CODE39/CODE128, QR, banded raster images, DLE EOT status.
- UTF-8 text transcoded to CP437 (configurable) with word-aware wrapping.
- Images from URL or base64, scaled and dithered; URL fetches refuse
internal addresses (SSRF guard).
- Fiber v3 HTTP server, TypeID (UUIDv7) job and request IDs, /healthz
with live printer status, `secret` and `token` subcommands.
Fixes
- Jobs are validated before anything is sent; a failed printer dial no
longer panics.
- Request logs never contain tokens; Sentry no longer receives the
Authorization header or print contents.
- Rate limiting is per client behind nginx (X-Real-IP).
- install.sh writes the [printer] section, runs the service as a
dedicated user and uses a current nginx TLS config.
config.toml is no longer tracked; start from configs/config.example.toml.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
package escpos
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
)
|
|
|
|
// rasterBandHeight is the number of rows sent per GS v 0 command. Splitting
|
|
// tall images keeps every command within the height limits of all supported
|
|
// models (TM-T20 accepts up to 2303 rows).
|
|
const rasterBandHeight = 256
|
|
|
|
// maxRasterWidthBytes bounds the image width so xL/xH stay meaningful.
|
|
const maxRasterWidthBytes = 0xFFFF
|
|
|
|
// RasterImage appends img as one or more GS v 0 raster bit images. Pixels
|
|
// darker than 50% luminance, after compositing onto white paper, print black.
|
|
// Dither images first (see the raster package) for photographic content. The
|
|
// width is padded with white up to a multiple of 8 dots.
|
|
func (b *Builder) RasterImage(img image.Image) error {
|
|
bounds := img.Bounds()
|
|
width, height := bounds.Dx(), bounds.Dy()
|
|
if width <= 0 || height <= 0 {
|
|
return fmt.Errorf("%w: image is empty", ErrInvalidArgument)
|
|
}
|
|
widthBytes := (width + 7) / 8
|
|
if widthBytes > maxRasterWidthBytes {
|
|
return fmt.Errorf("%w: image is %d dots wide", ErrInvalidArgument, width)
|
|
}
|
|
|
|
dark := darkFunc(img)
|
|
row := make([]byte, widthBytes)
|
|
for top := 0; top < height; top += rasterBandHeight {
|
|
rows := min(rasterBandHeight, height-top)
|
|
b.buf.Write([]byte{gs, 'v', '0', 0,
|
|
byte(widthBytes), byte(widthBytes >> 8),
|
|
byte(rows), byte(rows >> 8)})
|
|
for y := top; y < top+rows; y++ {
|
|
clear(row)
|
|
for x := range width {
|
|
if dark(bounds.Min.X+x, bounds.Min.Y+y) {
|
|
row[x/8] |= 0x80 >> (x % 8)
|
|
}
|
|
}
|
|
b.buf.Write(row)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// darkFunc returns a pixel test for img, with fast paths for the image types
|
|
// the raster package produces.
|
|
func darkFunc(img image.Image) func(x, y int) bool {
|
|
switch m := img.(type) {
|
|
case *image.Paletted:
|
|
dark := make([]bool, len(m.Palette))
|
|
for i, c := range m.Palette {
|
|
dark[i] = isDark(c)
|
|
}
|
|
return func(x, y int) bool {
|
|
idx := m.ColorIndexAt(x, y)
|
|
return int(idx) < len(dark) && dark[idx]
|
|
}
|
|
case *image.Gray:
|
|
return func(x, y int) bool {
|
|
return m.GrayAt(x, y).Y < 0x80
|
|
}
|
|
}
|
|
return func(x, y int) bool {
|
|
return isDark(img.At(x, y))
|
|
}
|
|
}
|
|
|
|
// isDark composites c onto white and reports whether its luminance is below
|
|
// 50%. RGBA returns alpha-premultiplied values, so adding the uncovered
|
|
// fraction of white is enough to composite.
|
|
func isDark(c color.Color) bool {
|
|
r, g, bl, a := c.RGBA()
|
|
white := 0xFFFF - a
|
|
luma := (299*(r+white) + 587*(g+white) + 114*(bl+white)) / 1000
|
|
return luma < 0x8000
|
|
}
|