mirror of
https://gitee.com/johng/gf
synced 2026-07-06 13:42:46 +08:00
add EncodeFile/EncodeFileToString functions for gbase64
This commit is contained in:
@ -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 <path> 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 <path> 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))
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
1
encoding/gbase64/testdata/test
vendored
Normal file
1
encoding/gbase64/testdata/test
vendored
Normal file
@ -0,0 +1 @@
|
||||
test
|
||||
Reference in New Issue
Block a user