update: template可以接受网络文件

This commit is contained in:
2024-03-19 17:01:53 +08:00
parent 8b3c590364
commit b05b7756a0
6 changed files with 54 additions and 37 deletions

23
internal/util/base64.go Normal file
View File

@ -0,0 +1,23 @@
package util
import (
"encoding/base64"
"strings"
)
func DecodeBase64(s string) (string, error) {
s = strings.TrimSpace(s)
// url safe
if strings.Contains(s, "-") || strings.Contains(s, "_") {
s = strings.Replace(s, "-", "+", -1)
s = strings.Replace(s, "_", "/", -1)
}
if len(s)%4 != 0 {
s += strings.Repeat("=", 4-len(s)%4)
}
decodeStr, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return "", err
}
return string(decodeStr), nil
}

25
internal/util/fetch.go Normal file
View File

@ -0,0 +1,25 @@
package util
import (
"io"
"net/http"
)
func Fetch(url string, maxRetryTimes int) (string, error) {
retryTime := 0
var err error
for retryTime < maxRetryTimes {
resp, err := http.Get(url)
if err != nil {
retryTime++
continue
}
data, err := io.ReadAll(resp.Body)
if err != nil {
retryTime++
continue
}
return string(data), err
}
return "", err
}