2024-01-21 15:21:42 +00:00
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-04-05 17:02:35 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/random"
|
2024-01-21 15:21:42 +00:00
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func OpenBrowser(url string) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
err = exec.Command("xdg-open", url).Start()
|
|
|
|
case "windows":
|
|
|
|
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
|
|
|
|
case "darwin":
|
|
|
|
err = exec.Command("open", url).Start()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetIp() (ip string) {
|
|
|
|
ips, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range ips {
|
|
|
|
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
|
|
|
|
if ipNet.IP.To4() != nil {
|
|
|
|
ip = ipNet.IP.String()
|
|
|
|
if strings.HasPrefix(ip, "10") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(ip, "172") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(ip, "192.168") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ip = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var sizeKB = 1024
|
|
|
|
var sizeMB = sizeKB * 1024
|
|
|
|
var sizeGB = sizeMB * 1024
|
|
|
|
|
|
|
|
func Bytes2Size(num int64) string {
|
|
|
|
numStr := ""
|
|
|
|
unit := "B"
|
|
|
|
if num/int64(sizeGB) > 1 {
|
|
|
|
numStr = fmt.Sprintf("%.2f", float64(num)/float64(sizeGB))
|
|
|
|
unit = "GB"
|
|
|
|
} else if num/int64(sizeMB) > 1 {
|
|
|
|
numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeMB)))
|
|
|
|
unit = "MB"
|
|
|
|
} else if num/int64(sizeKB) > 1 {
|
|
|
|
numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeKB)))
|
|
|
|
unit = "KB"
|
|
|
|
} else {
|
|
|
|
numStr = fmt.Sprintf("%d", num)
|
|
|
|
}
|
|
|
|
return numStr + " " + unit
|
|
|
|
}
|
|
|
|
|
|
|
|
func Interface2String(inter interface{}) string {
|
2024-02-12 13:35:40 +00:00
|
|
|
switch inter := inter.(type) {
|
2024-01-21 15:21:42 +00:00
|
|
|
case string:
|
2024-02-12 13:35:40 +00:00
|
|
|
return inter
|
2024-01-21 15:21:42 +00:00
|
|
|
case int:
|
2024-02-12 13:35:40 +00:00
|
|
|
return fmt.Sprintf("%d", inter)
|
2024-01-21 15:21:42 +00:00
|
|
|
case float64:
|
2024-02-12 13:35:40 +00:00
|
|
|
return fmt.Sprintf("%f", inter)
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|
|
|
|
return "Not Implemented"
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnescapeHTML(x string) interface{} {
|
|
|
|
return template.HTML(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IntMax(a int, b int) int {
|
|
|
|
if a >= b {
|
|
|
|
return a
|
|
|
|
} else {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 17:02:47 +00:00
|
|
|
func GenRequestID() string {
|
2024-04-05 17:02:35 +00:00
|
|
|
return GetTimeString() + random.GetRandomNumberString(8)
|
2024-03-13 17:02:47 +00:00
|
|
|
}
|
|
|
|
|
2024-01-21 15:21:42 +00:00
|
|
|
func Max(a int, b int) int {
|
|
|
|
if a >= b {
|
|
|
|
return a
|
|
|
|
} else {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AssignOrDefault(value string, defaultValue string) string {
|
|
|
|
if len(value) != 0 {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func MessageWithRequestId(message string, id string) string {
|
|
|
|
return fmt.Sprintf("%s (request id: %s)", message, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func String2Int(str string) int {
|
|
|
|
num, err := strconv.Atoi(str)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return num
|
|
|
|
}
|