forked from mirrors/homebox
23b5892aef
* introduce scaffold for new models * wip: shoutrrr wrapper (may remove) * update schema files * gen: ent code * gen: migrations * go mod tidy * add group_id to notifier * db migration * new mapper helpers * notifier repo * introduce experimental adapter pattern for hdlrs * refactor adapters to fit more common use cases * new routes for notifiers * update errors to fix validation panic * go tidy * reverse checkbox label display * wip: notifiers UI * use badges instead of text * improve documentation * add scaffold schema reference * remove notifier service * refactor schema folder * support group edges via scaffold * delete test file * include link to API docs * audit and update documentation + improve format * refactor schema edges * refactor * add custom validator * set validate + order fields by name * fix failing tests
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"image/png"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
|
"github.com/hay-kot/homebox/backend/pkgs/server"
|
|
"github.com/yeqown/go-qrcode/v2"
|
|
"github.com/yeqown/go-qrcode/writer/standard"
|
|
|
|
_ "embed"
|
|
)
|
|
|
|
//go:embed assets/QRIcon.png
|
|
var qrcodeLogo []byte
|
|
|
|
// HandleGenerateQRCode godoc
|
|
//
|
|
// @Summary Create QR Code
|
|
// @Tags Items
|
|
// @Produce json
|
|
// @Param data query string false "data to be encoded into qrcode"
|
|
// @Success 200 {string} string "image/jpeg"
|
|
// @Router /v1/qrcode [GET]
|
|
// @Security Bearer
|
|
func (ctrl *V1Controller) HandleGenerateQRCode() server.HandlerFunc {
|
|
const MaxLength = 4_296 // assume alphanumeric characters only
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
|
data := r.URL.Query().Get("data")
|
|
|
|
image, err := png.Decode(bytes.NewReader(qrcodeLogo))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if len(data) > MaxLength {
|
|
return validate.NewFieldErrors(validate.FieldError{
|
|
Field: "data",
|
|
Error: "max length is 4,296 characters exceeded",
|
|
})
|
|
}
|
|
|
|
qrc, err := qrcode.New(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
toWriteCloser := struct {
|
|
io.Writer
|
|
io.Closer
|
|
}{
|
|
Writer: w,
|
|
Closer: io.NopCloser(nil),
|
|
}
|
|
|
|
qrwriter := standard.NewWithWriter(toWriteCloser, standard.WithLogoImage(image))
|
|
|
|
// Return the QR code as a jpeg image
|
|
w.Header().Set("Content-Type", "image/jpeg")
|
|
w.Header().Set("Content-Disposition", "attachment; filename=qrcode.jpg")
|
|
return qrc.Save(qrwriter)
|
|
}
|
|
}
|