fix qr-code generation for large configurations (#374)
Some checks are pending
Docker / Build and Push (push) Waiting to run
Docker / release (push) Blocked by required conditions
github-pages / deploy (push) Waiting to run

This commit is contained in:
Christoph Haas
2025-02-26 22:59:11 +01:00
parent 40b4538e78
commit 66ccdc29e9
4 changed files with 16 additions and 93 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/sirupsen/logrus"
evbus "github.com/vardius/message-bus"
"github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/compressed"
)
type Manager struct {
@@ -27,7 +28,13 @@ type Manager struct {
wg WireguardDatabaseRepo
}
func NewConfigFileManager(cfg *config.Config, bus evbus.MessageBus, users UserDatabaseRepo, wg WireguardDatabaseRepo, fsRepo FileSystemRepo) (*Manager, error) {
func NewConfigFileManager(
cfg *config.Config,
bus evbus.MessageBus,
users UserDatabaseRepo,
wg WireguardDatabaseRepo,
fsRepo FileSystemRepo,
) (*Manager, error) {
tplHandler, err := newTemplateHandler()
if err != nil {
return nil, fmt.Errorf("failed to initialize template handler: %w", err)
@@ -156,18 +163,19 @@ func (m Manager) GetPeerConfigQrCode(ctx context.Context, id domain.PeerIdentifi
return nil, fmt.Errorf("failed to read peer config for %s: %w", id, err)
}
code, err := qrcode.New(sb.String())
code, err := qrcode.NewWith(sb.String(),
qrcode.WithErrorCorrectionLevel(qrcode.ErrorCorrectionLow), qrcode.WithEncodingMode(qrcode.EncModeByte))
if err != nil {
return nil, fmt.Errorf("failed to initializeqr code for %s: %w", id, err)
return nil, fmt.Errorf("failed to initialize qr code for %s: %w", id, err)
}
buf := bytes.NewBuffer(nil)
wr := nopCloser{Writer: buf}
option := Option{
option := compressed.Option{
Padding: 8, // padding pixels around the qr code.
BlockSize: 4, // block pixels which represents a bit data.
}
qrWriter := NewCompressedWriter(wr, &option)
qrWriter := compressed.NewWithWriter(wr, &option)
err = code.Save(qrWriter)
if err != nil {
return nil, fmt.Errorf("failed to write code for %s: %w", id, err)

View File

@@ -1,88 +0,0 @@
package configfile
// waiting for https://github.com/yeqown/go-qrcode/pull/85 to get merged
// meanwhile we use our own writer implementation
import (
"image"
"image/color"
"image/png"
"io"
"github.com/yeqown/go-qrcode/v2"
)
type Option struct {
Padding int
BlockSize int
}
// compressedWriter implements issue#69, generating compressed images
// in some special situations, such as, network transferring.
// https://github.com/yeqown/go-qrcode/issues/69
type compressedWriter struct {
fd io.WriteCloser
option *Option
}
var (
backgroundColor = color.Gray{Y: 0xff}
foregroundColor = color.Gray{Y: 0x00}
)
func NewCompressedWriter(writer io.WriteCloser, opt *Option) qrcode.Writer {
return compressedWriter{fd: writer, option: opt}
}
func (w compressedWriter) Write(mat qrcode.Matrix) error {
padding := w.option.Padding
blockWidth := w.option.BlockSize
width := mat.Width()*blockWidth + 2*padding
height := width
img := image.NewPaletted(
image.Rect(0, 0, width, height),
color.Palette([]color.Color{backgroundColor, foregroundColor}),
)
bgColor := uint8(img.Palette.Index(backgroundColor))
fgColor := uint8(img.Palette.Index(foregroundColor))
rectangle := func(x1, y1 int, x2, y2 int, img *image.Paletted, color uint8) {
for x := x1; x < x2; x++ {
for y := y1; y < y2; y++ {
pos := img.PixOffset(x, y)
img.Pix[pos] = color
}
}
}
// background
rectangle(0, 0, width, height, img, bgColor)
mat.Iterate(qrcode.IterDirection_COLUMN, func(x int, y int, v qrcode.QRValue) {
sx := x*blockWidth + padding
sy := y*blockWidth + padding
es := (x+1)*blockWidth + padding
ey := (y+1)*blockWidth + padding
if v.IsSet() {
rectangle(sx, sy, es, ey, img, fgColor)
}
//switch v.IsSet() {
//case false:
// gray = backgroundColor
//default:
// gray = foregroundColor
//}
})
encoder := png.Encoder{CompressionLevel: png.BestCompression}
return encoder.Encode(w.fd, img)
}
func (w compressedWriter) Close() error {
return w.fd.Close()
}