aboutsummaryrefslogtreecommitdiff
path: root/mastodon/helper.go
diff options
context:
space:
mode:
authorr <r@freesoftwareextremist.com>2019-12-13 18:08:26 +0000
committerr <r@freesoftwareextremist.com>2019-12-13 18:26:24 +0000
commit5e4da01c3ae3ae2e870faba9085d9d9213c01c29 (patch)
tree39d6f1e76b901549f194ddbac3c6cb82e0abd019 /mastodon/helper.go
downloadbloat-5e4da01c3ae3ae2e870faba9085d9d9213c01c29.tar.gz
bloat-5e4da01c3ae3ae2e870faba9085d9d9213c01c29.zip
Initial commit
Diffstat (limited to 'mastodon/helper.go')
-rw-r--r--mastodon/helper.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/mastodon/helper.go b/mastodon/helper.go
new file mode 100644
index 0000000..05af20f
--- /dev/null
+++ b/mastodon/helper.go
@@ -0,0 +1,55 @@
+package mastodon
+
+import (
+ "encoding/base64"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "net/http"
+ "os"
+)
+
+// Base64EncodeFileName returns the base64 data URI format string of the file with the file name.
+func Base64EncodeFileName(filename string) (string, error) {
+ file, err := os.Open(filename)
+ if err != nil {
+ return "", err
+ }
+ defer file.Close()
+
+ return Base64Encode(file)
+}
+
+// Base64Encode returns the base64 data URI format string of the 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 }
+
+func parseAPIError(prefix string, resp *http.Response) error {
+ errMsg := fmt.Sprintf("%s: %s", prefix, resp.Status)
+ var e struct {
+ Error string `json:"error"`
+ }
+
+ json.NewDecoder(resp.Body).Decode(&e)
+ if e.Error != "" {
+ errMsg = fmt.Sprintf("%s: %s", errMsg, e.Error)
+ }
+
+ return errors.New(errMsg)
+}