mirror of
https://github.com/hay-kot/homebox.git
synced 2024-12-19 05:26:31 +00:00
c19fe94c08
* code gen updates * qrcode support * remove opacity on toast * update item view to use tab-like pages * adjust view for cards * fix old API calls for ioutils * move embed * extract QR code * add docs for QR codes * add QR code
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 Encode data into QRCode
|
|
// @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)
|
|
}
|
|
}
|