From e62f1adaae0719e54571dbb72d70357575a30547 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sun, 16 Apr 2017 02:00:30 +0900 Subject: [PATCH] Add helper --- accounts_test.go | 2 -- helper.go | 36 ++++++++++++++++++++++++++++++++++++ helper_test.go | 13 +++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 helper.go create mode 100644 helper_test.go diff --git a/accounts_test.go b/accounts_test.go index a369c3b..e7bc167 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -34,8 +34,6 @@ func TestAccountUpdate(t *testing.T) { } } -func String(v string) *string { return &v } - func TestGetBlocks(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`) diff --git a/helper.go b/helper.go new file mode 100644 index 0000000..b606695 --- /dev/null +++ b/helper.go @@ -0,0 +1,36 @@ +package mastodon + +import ( + "encoding/base64" + "net/http" + "os" +) + +func Base64EncodeFileName(filename string) (string, error) { + file, err := os.Open(filename) + if err != nil { + return "", err + } + defer file.Close() + + return Base64Encode(file) +} + +func Base64Encode(file *os.File) (string, error) { + fi, err := file.Stat() + if err != nil { + return "", err + } + + d := make([]byte, fi.Size()) + _, err = file.Read(d) + if err != nil { + return "", err + } + + return "data:" + http.DetectContentType(d) + + ";base64," + base64.StdEncoding.EncodeToString(d), nil +} + +// String is a helper function to get the pointer value of a string. +func String(v string) *string { return &v } diff --git a/helper_test.go b/helper_test.go new file mode 100644 index 0000000..a4a6429 --- /dev/null +++ b/helper_test.go @@ -0,0 +1,13 @@ +package mastodon + +import ( + "testing" +) + +func TestString(t *testing.T) { + s := "test" + sp := String(s) + if *sp != s { + t.Fatalf("want %q but %q", s, *sp) + } +}