From e6aa9d3a46e4f6ab8939c1d5471b50252a28bf65 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 31 Oct 2019 14:42:01 +0800 Subject: [PATCH] add EncodeFile/EncodeFileToString functions for gbase64 --- encoding/gbase64/gbase64.go | 19 +++++++++++++++++ encoding/gbase64/gbase64_test.go | 35 +++++++++++++++++++++++++++++++- encoding/gbase64/testdata/test | 1 + 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 encoding/gbase64/testdata/test diff --git a/encoding/gbase64/gbase64.go b/encoding/gbase64/gbase64.go index 409780b66..f709b9bf7 100644 --- a/encoding/gbase64/gbase64.go +++ b/encoding/gbase64/gbase64.go @@ -10,6 +10,7 @@ package gbase64 import ( "encoding/base64" "github.com/gogf/gf/util/gconv" + "io/ioutil" ) // Encode encodes bytes with BASE64 algorithm. @@ -36,6 +37,24 @@ func EncodeToString(src []byte) string { return gconv.UnsafeBytesToStr(Encode(src)) } +// EncryptFile encodes file content of using BASE64 algorithms. +func EncodeFile(path string) ([]byte, error) { + content, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + return Encode(content), nil +} + +// EncodeFileToString encodes file content of to string using BASE64 algorithms. +func EncodeFileToString(path string) (string, error) { + content, err := EncodeFile(path) + if err != nil { + return "", err + } + return gconv.UnsafeBytesToStr(content), nil +} + // DecodeString decodes string with BASE64 algorithm. func DecodeString(str string) ([]byte, error) { return Decode([]byte(str)) diff --git a/encoding/gbase64/gbase64_test.go b/encoding/gbase64/gbase64_test.go index e689e9bd4..378166193 100644 --- a/encoding/gbase64/gbase64_test.go +++ b/encoding/gbase64/gbase64_test.go @@ -3,9 +3,12 @@ // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. + package gbase64_test import ( + "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/os/gfile" "testing" "github.com/gogf/gf/encoding/gbase64" @@ -42,7 +45,7 @@ var pairs = []testPair{ {"sure.", "c3VyZS4="}, } -func TestBase64(t *testing.T) { +func Test_Basic(t *testing.T) { gtest.Case(t, func() { for k := range pairs { // Encode @@ -62,3 +65,33 @@ func TestBase64(t *testing.T) { } }) } + +func Test_File(t *testing.T) { + path := gfile.Join(gdebug.CallerDirectory(), "testdata", "test") + expect := "dGVzdA==" + gtest.Case(t, func() { + b, err := gbase64.EncodeFile(path) + gtest.Assert(err, nil) + gtest.Assert(string(b), expect) + }) + gtest.Case(t, func() { + s, err := gbase64.EncodeFileToString(path) + gtest.Assert(err, nil) + gtest.Assert(s, expect) + }) +} + +func Test_File_Error(t *testing.T) { + path := "none-exist-file" + expect := "" + gtest.Case(t, func() { + b, err := gbase64.EncodeFile(path) + gtest.AssertNE(err, nil) + gtest.Assert(string(b), expect) + }) + gtest.Case(t, func() { + s, err := gbase64.EncodeFileToString(path) + gtest.AssertNE(err, nil) + gtest.Assert(s, expect) + }) +} diff --git a/encoding/gbase64/testdata/test b/encoding/gbase64/testdata/test new file mode 100644 index 000000000..30d74d258 --- /dev/null +++ b/encoding/gbase64/testdata/test @@ -0,0 +1 @@ +test \ No newline at end of file