-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.go
164 lines (134 loc) · 3.33 KB
/
utils.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package overflow
import (
"bufio"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"math"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/araddon/dateparse"
"github.com/onflow/cadence"
)
// go string to cadence string panic if error
func cadenceString(input string) cadence.String {
value, err := cadence.NewString(input)
if err != nil {
panic(err)
}
return value
}
func parseTime(timeString string, location string) (string, error) {
loc, err := time.LoadLocation(location)
if err != nil {
return "", err
}
time.Local = loc
t, err := dateparse.ParseLocal(timeString)
if err != nil {
return "", err
}
return fmt.Sprintf("%d.0", t.Unix()), nil
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func writeProgressToFile(fileName string, blockHeight int64) error {
err := os.WriteFile(fileName, []byte(fmt.Sprintf("%d", blockHeight)), 0644)
if err != nil {
return fmt.Errorf("could not create initial progress file %v", err)
}
return nil
}
func readProgressFromFile(fileName string) (int64, error) {
dat, err := os.ReadFile(fileName)
if err != nil {
return 0, fmt.Errorf("ProgressFile is not valid %v", err)
}
stringValue := strings.TrimSpace(string(dat))
return strconv.ParseInt(stringValue, 10, 64)
}
func splitByWidthMake(str string, size int) []string {
strLength := len(str)
splitedLength := int(math.Ceil(float64(strLength) / float64(size)))
splited := make([]string, splitedLength)
var start, stop int
for i := 0; i < splitedLength; i += 1 {
start = i * size
stop = start + size
if stop > strLength {
stop = strLength
}
splited[i] = str[start:stop]
}
return splited
}
func fileAsImageData(path string) (string, error) {
f, _ := os.Open(path)
defer f.Close()
// Read entire JPG into byte slice.
reader := bufio.NewReader(f)
content, err := io.ReadAll(reader)
if err != nil {
return "", fmt.Errorf("could not read imageFile %s, %w", path, err)
}
return contentAsImageDataUrl(content), nil
}
func contentAsImageDataUrl(content []byte) string {
contentType := http.DetectContentType(content)
// Encode as base64.
encoded := base64.StdEncoding.EncodeToString(content)
return "data:" + contentType + ";base64, " + encoded
}
func fileAsBase64(path string) (string, error) {
f, _ := os.Open(path)
defer f.Close()
// Read entire JPG into byte slice.
reader := bufio.NewReader(f)
content, err := io.ReadAll(reader)
if err != nil {
return "", fmt.Errorf("could not read file %s, %w", path, err)
}
// Encode as base64.
encoded := base64.StdEncoding.EncodeToString(content)
return encoded, nil
}
func getUrl(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func randomString(length int) string {
rand.Seed(time.Now().UnixNano())
b := make([]byte, length)
rand.Read(b)
return fmt.Sprintf("%x", b)[:length]
}
// HexToAddress converts a hex string to an Address.
func hexToAddress(h string) (*cadence.Address, error) {
trimmed := strings.TrimPrefix(h, "0x")
if len(trimmed)%2 == 1 {
trimmed = "0" + trimmed
}
b, err := hex.DecodeString(trimmed)
if err != nil {
return nil, err
}
address := cadence.BytesToAddress(b)
return &address, nil
}