Files
qrcode/app/internal/qrcode/vo/qrimg.go
Michael a14103ad8e init
2026-04-29 20:11:57 +02:00

67 lines
1.1 KiB
Go

package vo
import (
"net/url"
"strings"
"git.heldm.com/held/qrcode/internal/qrcode"
)
type QRImg struct {
link string
img []byte
imgFormat string
imgSize int
}
func NewQR(link, imgFormat string, imgSize int) (*QRImg, error) {
link = strings.TrimSpace(link)
imgFormat = strings.TrimSpace(strings.ToLower(imgFormat))
if link == "" {
return nil, qrcode.ErrInvalidURL
}
u, err := url.ParseRequestURI(link)
if err != nil {
return nil, qrcode.ErrInvalidURL
}
if u.Scheme != "http" && u.Scheme != "https" {
return nil, qrcode.ErrInvalidURL
}
// imgFormat should be either "png" or "svg", default to "png" if invalid
switch imgFormat {
case "png", "svg":
default:
imgFormat = "png"
}
// imgSize should be between 256 and 2048, default to 256 if invalid
if imgSize <= 0 {
imgSize = 256
}
if imgSize > 2048 {
imgSize = 2048
}
return &QRImg{
link: link,
imgFormat: imgFormat,
imgSize: imgSize,
}, nil
}
func (q QRImg) Link() string {
return q.link
}
func (q QRImg) ImgFormat() string {
return q.imgFormat
}
func (q QRImg) ImgSize() int {
return q.imgSize
}