Skip to content

Commit

Permalink
[U] 优化命名
Browse files Browse the repository at this point in the history
  • Loading branch information
zxysilent committed Jul 10, 2020
1 parent b76dee7 commit 3512d39
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
2 changes: 1 addition & 1 deletion control/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func Vcode(ctx echo.Context) error {
Vcode string `json:"vcode"`
Vreal string `json:"vreal"`
}{
Vcode: vcode.NewImage(rnd).Bs64(),
Vcode: vcode.NewImage(rnd).Base64(),
Vreal: hmc(rnd, "v.c.o.d.e"),
}
return ctx.JSON(utils.Succ("succ", out))
Expand Down
14 changes: 4 additions & 10 deletions internal/vcode/vcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"image"
"image/color"
"image/png"
"io"
"math"
"math/rand"
"time"
Expand Down Expand Up @@ -75,23 +74,18 @@ func (m *Image) getRandomPalette() color.Palette {
return p
}

// encodedPNG encodes an image to PNG and returns
// EncodedPNG encodes an image to PNG and returns
// the result as a byte slice.
func (m *Image) encodedPNG() []byte {
func (m *Image) EncodedPNG() []byte {
var buf bytes.Buffer
if err := png.Encode(&buf, m.Paletted); err != nil {
panic(err.Error())
}
return buf.Bytes()
}

// WriteTo writes captcha image in PNG format into the given writer.
func (m *Image) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(m.encodedPNG())
return int64(n), err
}
func (m *Image) Bs64() string {
return base64.RawStdEncoding.EncodeToString(m.encodedPNG())
func (m *Image) Base64() string {
return base64.RawStdEncoding.EncodeToString(m.EncodedPNG())
}

func (m *Image) calculateSizes(stdWidth, stdHeight, ncount int) {
Expand Down
6 changes: 1 addition & 5 deletions internal/vcode/vcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package vcode

import (
"fmt"
"os"
"testing"

"github.com/zxysilent/utils"
Expand All @@ -13,8 +12,5 @@ func TestVcode(t *testing.T) {
for i := 0; i < len(s); i++ {
fmt.Println(s[i])
}
fout, _ := os.Create("out.png")
NewImage(utils.RandDigitStr(6)).WriteTo(fout)
fmt.Println(NewImage(utils.RandDigitStr(6)).Bs64())
fout.Close()
fmt.Println(NewImage(utils.RandDigitStr(6)).Base64())
}

0 comments on commit 3512d39

Please sign in to comment.