From 7cbd3d4d7c1d2568a47d16a3a3780c4af98d3d22 Mon Sep 17 00:00:00 2001 From: mandriota Date: Mon, 22 May 2023 12:28:56 +0200 Subject: [PATCH 1/2] add item.EncodeBinary interface method --- go.mod | 2 +- interface_item.go | 8 +++++--- item_audio.go | 16 ++++++++++------ item_char.go | 27 ++++++++++++++------------- item_char_test.go | 2 +- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 8ae257e..d47ea0d 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/mojocn/base64Captcha +module github.com/mandriota/base64Captcha go 1.16 diff --git a/interface_item.go b/interface_item.go index d0e679c..b5d958d 100644 --- a/interface_item.go +++ b/interface_item.go @@ -2,10 +2,12 @@ package base64Captcha import "io" -//Item is captcha item interface +// Item is captcha item interface type Item interface { - //WriteTo writes to a writer + // WriteTo writes to a writer WriteTo(w io.Writer) (n int64, err error) - //EncodeB64string encodes as base64 string + // EncodeBinary encodes as raw byte slice + EncodeBinary() []byte + // EncodeB64string encodes as base64 string EncodeB64string() string } diff --git a/item_audio.go b/item_audio.go index 7e8ca49..3df55a8 100644 --- a/item_audio.go +++ b/item_audio.go @@ -13,7 +13,7 @@ import ( "math/rand" ) -//ItemAudio captcha-audio-engine return type. +// ItemAudio captcha-audio-engine return type. type ItemAudio struct { answer string body *bytes.Buffer @@ -120,6 +120,14 @@ func (a *ItemAudio) makeWhiteNoise(length int, level uint8) []byte { return noise } +func (a *ItemAudio) EncodeBinary() []byte { + var buf bytes.Buffer + if _, err := a.WriteTo(&buf); err != nil { + panic(err) + } + return buf.Bytes() +} + // WriteTo writes captcha audio in WAVE format into the given io.Writer, and // returns the number of bytes written and an error if any. func (a *ItemAudio) WriteTo(w io.Writer) (n int64, err error) { @@ -160,10 +168,6 @@ func (a *ItemAudio) WriteTo(w io.Writer) (n int64, err error) { // EncodeB64string encodes a sound to base64 string func (a *ItemAudio) EncodeB64string() string { - var buf bytes.Buffer - if _, err := a.WriteTo(&buf); err != nil { - panic(err) - } - return fmt.Sprintf("data:%s;base64,%s", MimeTypeAudio, base64.StdEncoding.EncodeToString(buf.Bytes())) + return fmt.Sprintf("data:%s;base64,%s", MimeTypeAudio, base64.StdEncoding.EncodeToString(a.EncodeBinary())) } diff --git a/item_char.go b/item_char.go index 53c7d2b..a193890 100644 --- a/item_char.go +++ b/item_char.go @@ -5,9 +5,6 @@ import ( "encoding/base64" "errors" "fmt" - "github.com/golang/freetype" - "github.com/golang/freetype/truetype" - "golang.org/x/image/font" "image" "image/color" "image/draw" @@ -16,9 +13,13 @@ import ( "log" "math" "math/rand" + + "github.com/golang/freetype" + "github.com/golang/freetype/truetype" + "golang.org/x/image/font" ) -//ItemChar captcha item of unicode characters +// ItemChar captcha item of unicode characters type ItemChar struct { bgColor color.Color width int @@ -26,16 +27,16 @@ type ItemChar struct { nrgba *image.NRGBA } -//NewItemChar creates a captcha item of characters +// NewItemChar creates a captcha item of characters func NewItemChar(w int, h int, bgColor color.RGBA) *ItemChar { d := ItemChar{width: w, height: h} m := image.NewNRGBA(image.Rect(0, 0, w, h)) - draw.Draw(m, m.Bounds(), &image.Uniform{bgColor}, image.ZP, draw.Src) + draw.Draw(m, m.Bounds(), &image.Uniform{bgColor}, image.Point{}, draw.Src) d.nrgba = m return &d } -//drawHollowLine draw strong and bold white line. +// drawHollowLine draw strong and bold white line. func (item *ItemChar) drawHollowLine() *ItemChar { first := item.width / 20 @@ -72,7 +73,7 @@ func (item *ItemChar) drawHollowLine() *ItemChar { return item } -//drawSineLine draw a sine line. +// drawSineLine draw a sine line. func (item *ItemChar) drawSineLine() *ItemChar { var py float64 @@ -116,7 +117,7 @@ func (item *ItemChar) drawSineLine() *ItemChar { return item } -//drawSlimLine draw n slim-random-color lines. +// drawSlimLine draw n slim-random-color lines. func (item *ItemChar) drawSlimLine(num int) *ItemChar { first := item.width / 10 @@ -231,8 +232,8 @@ func (item *ItemChar) drawText(text string, fonts []*truetype.Font) error { return nil } -//BinaryEncoding encodes an image to PNG and returns a byte slice. -func (item *ItemChar) BinaryEncoding() []byte { +// BinaryEncoding encodes an image to PNG and returns a byte slice. +func (item *ItemChar) EncodeBinary() []byte { var buf bytes.Buffer if err := png.Encode(&buf, item.nrgba); err != nil { panic(err.Error()) @@ -243,13 +244,13 @@ func (item *ItemChar) BinaryEncoding() []byte { // WriteTo writes captcha character in png format into the given io.Writer, and // returns the number of bytes written and an error if any. func (item *ItemChar) WriteTo(w io.Writer) (int64, error) { - n, err := w.Write(item.BinaryEncoding()) + n, err := w.Write(item.EncodeBinary()) return int64(n), err } // EncodeB64string encodes an image to base64 string func (item *ItemChar) EncodeB64string() string { - return fmt.Sprintf("data:%s;base64,%s", MimeTypeImage, base64.StdEncoding.EncodeToString(item.BinaryEncoding())) + return fmt.Sprintf("data:%s;base64,%s", MimeTypeImage, base64.StdEncoding.EncodeToString(item.EncodeBinary())) } type point struct { diff --git a/item_char_test.go b/item_char_test.go index 5867090..556aade 100644 --- a/item_char_test.go +++ b/item_char_test.go @@ -160,7 +160,7 @@ func TestItemChar_BinaryEncoding(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.item.BinaryEncoding(); !reflect.DeepEqual(got, tt.want) { + if got := tt.item.EncodeBinary(); !reflect.DeepEqual(got, tt.want) { t.Errorf("ItemChar.BinaryEncoding() = %v, want %v", got, tt.want) } }) From 91ba88c40b84f36da8b10b0868e98243972ad3ce Mon Sep 17 00:00:00 2001 From: Mark Mandriota <62650188+mandriota@users.noreply.github.com> Date: Mon, 22 May 2023 14:27:12 +0200 Subject: [PATCH 2/2] prepare module for pool request --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d47ea0d..8ae257e 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/mandriota/base64Captcha +module github.com/mojocn/base64Captcha go 1.16