Add helper
This commit is contained in:
parent
62318331a3
commit
e62f1adaae
|
@ -34,8 +34,6 @@ func TestAccountUpdate(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func String(v string) *string { return &v }
|
|
||||||
|
|
||||||
func TestGetBlocks(t *testing.T) {
|
func TestGetBlocks(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)
|
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)
|
||||||
|
|
36
helper.go
Normal file
36
helper.go
Normal file
|
@ -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 }
|
13
helper_test.go
Normal file
13
helper_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user