mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-27 02:55:42 +00:00
68 lines
1.5 KiB
Go
68 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)
|
||
|
}
|
||
|
}
|