/
base64.go
45 lines (37 loc) · 1.04 KB
/
base64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package utils
import (
"encoding/base64"
"os"
)
func EncodeBase64(data []byte) string {
return base64.StdEncoding.EncodeToString(data)
}
func DecodeBase64(data string) ([]byte, error) {
return base64.StdEncoding.DecodeString(data)
}
// Convenience function to encode image in any format to base64
func Base64FromFile(path string) (string, error) {
fileBytes, err := os.ReadFile(path)
if err != nil {
return "", err
}
return EncodeBase64(fileBytes), nil
}
// Convenience function to encode image in any format to base64 and IGNORE ANY ERRORS that might occur.
//
// This may be helpful when you are SURE it doesn't give you any error
func Base64FromFileIgnore(path string) string {
fileBytes, _ := os.ReadFile(path)
return EncodeBase64(fileBytes)
}
// Convenience function to encode image in any format to base64
func Base64FromFiles(allPath ...string) (res []string, err error) {
for _, path := range allPath {
encoded, err := Base64FromFile(path)
if err != nil {
return []string{}, err
}
res = append(res, encoded)
}
return
}