added untested qrcode location function

This commit is contained in:
icanotc 2023-08-13 05:12:50 -04:00
parent 2cbcc8bb1d
commit 49fe2cc787

View file

@ -2,6 +2,9 @@ package v1
import (
"bytes"
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/core/services"
"github.com/rs/zerolog/log"
"image/png"
"io"
"net/http"
@ -17,6 +20,15 @@ import (
//go:embed assets/QRIcon.png
var qrcodeLogo []byte
type query struct {
// 4,296 characters is the maximum length of a QR code
Data string `schema:"data" validate:"required,max=4296"`
}
type locationQuery struct {
UUID uuid.UUID
}
// HandleGenerateQRCode godoc
//
// @Summary Create QR Code
@ -27,10 +39,6 @@ var qrcodeLogo []byte
// @Router /v1/qrcode [GET]
// @Security Bearer
func (ctrl *V1Controller) HandleGenerateQRCode() errchain.HandlerFunc {
type query struct {
// 4,296 characters is the maximum length of a QR code
Data string `schema:"data" validate:"required,max=4296"`
}
return func(w http.ResponseWriter, r *http.Request) error {
q, err := adapters.DecodeQuery[query](r)
@ -64,3 +72,54 @@ func (ctrl *V1Controller) HandleGenerateQRCode() errchain.HandlerFunc {
return qrc.Save(qrwriter)
}
}
func (ctrl *V1Controller) HandleGenerateQRCodeForLocations() errchain.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
data, err := adapters.DecodeQuery[locationQuery](r)
if err != nil {
return err
}
auth := services.NewContext(r.Context())
locations, err := ctrl.repo.Locations.GetOneByGroup(auth, auth.GID, data.UUID)
if err != nil {
return err
}
image, err := png.Decode(bytes.NewReader(qrcodeLogo))
if err != nil {
panic(err)
}
toWriteCloser := struct {
io.Writer
io.Closer
}{
Writer: w,
Closer: io.NopCloser(nil),
}
var qrCodeBuffer bytes.Buffer
for _, location := range locations.Items {
encodeStr := r.URL.String() + location.ID.String() //concat the url and then UUID
log.Debug().Msg(encodeStr)
qrc, err := qrcode.New(encodeStr)
if err != nil {
return err
}
qrWriter := standard.NewWithWriter(toWriteCloser, standard.WithLogoImage(image))
err = qrc.Save(qrWriter)
if err != nil {
return err
}
}
// Return the concatenated QR code images as a response
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Disposition", "attachment; filename=qrcodes.png")
_, err = w.Write(qrCodeBuffer.Bytes())
return err
}
}