-
Notifications
You must be signed in to change notification settings - Fork 7
/
utilities.go
118 lines (101 loc) · 2.82 KB
/
utilities.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// utilities.go
// For utility/helper functions to support from the main package
package utils
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"os"
"path/filepath"
)
func Base64EncodeCertificate(certPath string) (string, error) {
// Read the certificate file
data, err := os.ReadFile(certPath)
if err != nil {
return "", fmt.Errorf("failed to read certificate file: %v", err)
}
// Base64 encode the file's content
encoded := base64.StdEncoding.EncodeToString(data)
return encoded, nil
}
// GetImageContentType determines the content type based on file extension
func GetImageContentType(filePath string) string {
ext := filepath.Ext(filePath)
switch ext {
case ".png":
return "image/png"
case ".jpg", ".jpeg":
return "image/jpeg"
default:
return "application/octet-stream"
}
}
// Base64Encode encodes the provided data into a base64 string and provides details about the encoding process.
func Base64Encode(data []byte) (string, error) {
// Check if the provided data is empty
if len(data) == 0 {
return "", fmt.Errorf("no data provided for encoding")
}
// Encode the data to base64
encoded := base64.StdEncoding.EncodeToString(data)
// Check if the encoding process was successful
if encoded == "" {
return "", fmt.Errorf("failed to encode the data")
}
// Return the encoded data
return encoded, nil
}
// UnmarshalJSONData unmarshals binary data into the given output structure.
func UnmarshalJSONData(data []byte, out interface{}) error {
return json.Unmarshal(data, out)
}
// DumpRequestToFile dumps the given request to a specified file.
func DumpRequestToFile(req *http.Request, filename string) error {
// Dump the request details
dump, err := httputil.DumpRequestOut(req, true)
if err != nil {
return fmt.Errorf("error dumping request: %v", err)
}
// Open a file for writing
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("cannot create file: %v", err)
}
defer file.Close()
// Write the dumped request to the file
_, err = file.WriteString(string(dump))
if err != nil {
return fmt.Errorf("cannot write to file: %v", err)
}
return nil
}
/*
// Print request headers for troubleshooting
func PrintRequestHeaders(req *http.Request) {
fmt.Println("Request Headers:")
for name, values := range req.Header {
// Each value is a slice of strings since headers can be repeated.
for _, value := range values {
fmt.Printf("%s: %s\n", name, value)
}
}
}
*/
// PrettyPrintStruct prints the structure in a pretty format
func PrettyPrintStruct(v interface{}) string {
b, err := json.Marshal(v)
if err != nil {
return err.Error()
}
var out map[string]interface{}
if err := json.Unmarshal(b, &out); err != nil {
return err.Error()
}
pretty, err := json.MarshalIndent(out, "", " ")
if err != nil {
return err.Error()
}
return string(pretty)
}