rui_orig/utils.go

108 lines
2.5 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
2023-01-25 01:41:56 +03:00
"encoding/base64"
2021-09-07 17:36:50 +03:00
"net"
2023-01-25 01:41:56 +03:00
"path/filepath"
2021-09-07 17:36:50 +03:00
"strconv"
"strings"
2024-06-26 19:01:00 +03:00
"sync"
2021-09-07 17:36:50 +03:00
)
2024-06-26 19:01:00 +03:00
const stringBuilderCap = 4096
var stringBuilderPool = sync.Pool{
New: func() any {
result := new(strings.Builder)
result.Grow(stringBuilderCap)
return result
},
}
2021-09-07 17:36:50 +03:00
func allocStringBuilder() *strings.Builder {
2024-06-26 19:01:00 +03:00
if builder := stringBuilderPool.Get(); builder != nil {
return builder.(*strings.Builder)
2021-09-07 17:36:50 +03:00
}
result := new(strings.Builder)
2024-06-26 19:01:00 +03:00
result.Grow(stringBuilderCap)
2021-09-07 17:36:50 +03:00
return result
}
func freeStringBuilder(builder *strings.Builder) {
2024-06-26 19:01:00 +03:00
if builder != nil && builder.Cap() == stringBuilderCap {
builder.Reset()
stringBuilderPool.Put(builder)
2021-09-07 17:36:50 +03:00
}
}
// GetLocalIP return IP address of the machine interface
2021-09-07 17:36:50 +03:00
func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return "localhost"
}
2021-11-04 14:59:25 +03:00
func dataIntProperty(data DataObject, tag string) (int, bool) {
2021-09-07 17:36:50 +03:00
if value, ok := data.PropertyValue(tag); ok {
if n, err := strconv.Atoi(value); err == nil {
2021-11-04 14:59:25 +03:00
return n, true
2021-09-07 17:36:50 +03:00
}
}
2021-11-04 14:59:25 +03:00
return 0, false
2021-09-07 17:36:50 +03:00
}
func dataBoolProperty(data DataObject, tag string) bool {
if value, ok := data.PropertyValue(tag); ok && value == "1" {
return true
}
return false
}
func dataFloatProperty(data DataObject, tag string) float64 {
if value, ok := data.PropertyValue(tag); ok {
if n, err := strconv.ParseFloat(value, 64); err == nil {
return n
}
}
return 0
}
2023-01-25 01:41:56 +03:00
// InlineImageFromResource reads image from resources and converts it to an inline image.
// Supported png, jpeg, gif, and svg files
func InlineImageFromResource(filename string) (string, bool) {
if image, ok := resources.images[filename]; ok && image.fs != nil {
dataType := map[string]string{
".svg": "data:image/svg+xml",
".png": "data:image/png",
".jpg": "data:image/jpg",
".jpeg": "data:image/jpg",
".gif": "data:image/gif",
}
ext := strings.ToLower(filepath.Ext(filename))
if prefix, ok := dataType[ext]; ok {
if data, err := image.fs.ReadFile(image.path); err == nil {
return prefix + ";base64," + base64.StdEncoding.EncodeToString(data), true
} else {
DebugLog(err.Error())
}
} else {
DebugLogF(`InlineImageFromResource("%s") error: Unsupported file`, filename)
}
} else {
DebugLogF(`The resource image "%s" not found`, filename)
}
return "", false
}