2023-04-22 20:39:27 +08:00
|
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-01-17 18:12:05 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
|
"context"
|
2024-09-24 20:19:18 +08:00
|
|
|
|
crand "crypto/rand"
|
2024-09-25 16:30:33 +08:00
|
|
|
|
"encoding/base64"
|
2025-02-25 22:01:05 +08:00
|
|
|
|
"encoding/json"
|
2023-04-22 20:39:27 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"html/template"
|
2025-01-17 18:12:05 +08:00
|
|
|
|
"io"
|
2023-04-22 20:39:27 +08:00
|
|
|
|
"log"
|
2024-09-24 20:19:18 +08:00
|
|
|
|
"math/big"
|
2023-06-03 10:53:25 +08:00
|
|
|
|
"math/rand"
|
2023-04-22 20:39:27 +08:00
|
|
|
|
"net"
|
2025-01-17 18:12:05 +08:00
|
|
|
|
"os"
|
2023-04-22 20:39:27 +08:00
|
|
|
|
"os/exec"
|
|
|
|
|
|
"runtime"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
2023-04-23 12:43:10 +08:00
|
|
|
|
"time"
|
2024-12-24 14:48:11 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2025-04-25 18:17:46 +08:00
|
|
|
|
"github.com/pkg/errors"
|
2023-04-22 20:39:27 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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 Seconds2Time(num int) (time string) {
|
|
|
|
|
|
if num/31104000 > 0 {
|
|
|
|
|
|
time += strconv.Itoa(num/31104000) + " 年 "
|
|
|
|
|
|
num %= 31104000
|
|
|
|
|
|
}
|
|
|
|
|
|
if num/2592000 > 0 {
|
|
|
|
|
|
time += strconv.Itoa(num/2592000) + " 个月 "
|
|
|
|
|
|
num %= 2592000
|
|
|
|
|
|
}
|
|
|
|
|
|
if num/86400 > 0 {
|
|
|
|
|
|
time += strconv.Itoa(num/86400) + " 天 "
|
|
|
|
|
|
num %= 86400
|
|
|
|
|
|
}
|
|
|
|
|
|
if num/3600 > 0 {
|
|
|
|
|
|
time += strconv.Itoa(num/3600) + " 小时 "
|
|
|
|
|
|
num %= 3600
|
|
|
|
|
|
}
|
|
|
|
|
|
if num/60 > 0 {
|
|
|
|
|
|
time += strconv.Itoa(num/60) + " 分钟 "
|
|
|
|
|
|
num %= 60
|
|
|
|
|
|
}
|
|
|
|
|
|
time += strconv.Itoa(num) + " 秒"
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Interface2String(inter interface{}) string {
|
|
|
|
|
|
switch inter.(type) {
|
|
|
|
|
|
case string:
|
|
|
|
|
|
return inter.(string)
|
|
|
|
|
|
case int:
|
|
|
|
|
|
return fmt.Sprintf("%d", inter.(int))
|
|
|
|
|
|
case float64:
|
|
|
|
|
|
return fmt.Sprintf("%f", inter.(float64))
|
|
|
|
|
|
}
|
|
|
|
|
|
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-09-17 20:49:51 +08:00
|
|
|
|
func IsIP(s string) bool {
|
|
|
|
|
|
ip := net.ParseIP(s)
|
|
|
|
|
|
return ip != nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-22 20:39:27 +08:00
|
|
|
|
func GetUUID() string {
|
|
|
|
|
|
code := uuid.New().String()
|
|
|
|
|
|
code = strings.Replace(code, "-", "", -1)
|
|
|
|
|
|
return code
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-03 10:53:25 +08:00
|
|
|
|
const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
2024-09-25 16:30:33 +08:00
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano()))
|
2023-06-03 10:53:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-25 16:30:33 +08:00
|
|
|
|
func GenerateRandomCharsKey(length int) (string, error) {
|
2024-09-24 20:19:18 +08:00
|
|
|
|
b := make([]byte, length)
|
|
|
|
|
|
maxI := big.NewInt(int64(len(keyChars)))
|
|
|
|
|
|
|
|
|
|
|
|
for i := range b {
|
|
|
|
|
|
n, err := crand.Int(crand.Reader, maxI)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
2023-06-03 10:53:25 +08:00
|
|
|
|
}
|
2024-09-24 20:19:18 +08:00
|
|
|
|
b[i] = keyChars[n.Int64()]
|
2023-06-03 10:53:25 +08:00
|
|
|
|
}
|
2024-09-24 20:19:18 +08:00
|
|
|
|
|
|
|
|
|
|
return string(b), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-25 16:30:33 +08:00
|
|
|
|
func GenerateRandomKey(length int) (string, error) {
|
|
|
|
|
|
bytes := make([]byte, length*3/4) // 对于48位的输出,这里应该是36
|
|
|
|
|
|
if _, err := crand.Read(bytes); err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
return base64.StdEncoding.EncodeToString(bytes), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-24 20:19:18 +08:00
|
|
|
|
func GenerateKey() (string, error) {
|
|
|
|
|
|
//rand.Seed(time.Now().UnixNano())
|
2024-09-25 16:30:33 +08:00
|
|
|
|
return GenerateRandomCharsKey(48)
|
2023-06-03 10:53:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-27 19:00:47 +08:00
|
|
|
|
func GetRandomInt(max int) int {
|
|
|
|
|
|
//rand.Seed(time.Now().UnixNano())
|
|
|
|
|
|
return rand.Intn(max)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-23 12:43:10 +08:00
|
|
|
|
func GetTimestamp() int64 {
|
|
|
|
|
|
return time.Now().Unix()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-17 15:39:46 +08:00
|
|
|
|
func GetTimeString() string {
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
return fmt.Sprintf("%s%d", now.Format("20060102150405"), now.UnixNano()%1e9)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-22 20:39:27 +08:00
|
|
|
|
func Max(a int, b int) int {
|
|
|
|
|
|
if a >= b {
|
|
|
|
|
|
return a
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return b
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-08-12 10:05:25 +08:00
|
|
|
|
|
2024-07-27 17:55:36 +08:00
|
|
|
|
func MessageWithRequestId(message string, id string) string {
|
|
|
|
|
|
return fmt.Sprintf("%s (request id: %s)", message, id)
|
2023-09-17 15:39:46 +08:00
|
|
|
|
}
|
2023-10-22 18:38:29 +08:00
|
|
|
|
|
2024-04-04 19:18:00 +08:00
|
|
|
|
func RandomSleep() {
|
|
|
|
|
|
// Sleep for 0-3000 ms
|
|
|
|
|
|
time.Sleep(time.Duration(rand.Intn(3000)) * time.Millisecond)
|
|
|
|
|
|
}
|
2025-01-17 18:12:05 +08:00
|
|
|
|
|
2025-02-25 22:01:05 +08:00
|
|
|
|
func GetPointer[T any](v T) *T {
|
|
|
|
|
|
return &v
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Any2Type[T any](data any) (T, error) {
|
|
|
|
|
|
var zero T
|
|
|
|
|
|
bytes, err := json.Marshal(data)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return zero, err
|
|
|
|
|
|
}
|
|
|
|
|
|
var res T
|
|
|
|
|
|
err = json.Unmarshal(bytes, &res)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return zero, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return res, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-17 18:12:05 +08:00
|
|
|
|
// SaveTmpFile saves data to a temporary file. The filename would be apppended with a random string.
|
|
|
|
|
|
func SaveTmpFile(filename string, data io.Reader) (string, error) {
|
|
|
|
|
|
f, err := os.CreateTemp(os.TempDir(), filename)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", errors.Wrapf(err, "failed to create temporary file %s", filename)
|
|
|
|
|
|
}
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(f, data)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", errors.Wrapf(err, "failed to copy data to temporary file %s", filename)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return f.Name(), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetAudioDuration returns the duration of an audio file in seconds.
|
|
|
|
|
|
func GetAudioDuration(ctx context.Context, filename string) (float64, error) {
|
|
|
|
|
|
// ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {{input}}
|
|
|
|
|
|
c := exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", filename)
|
|
|
|
|
|
output, err := c.Output()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, errors.Wrap(err, "failed to get audio duration")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return strconv.ParseFloat(string(bytes.TrimSpace(output)), 64)
|
|
|
|
|
|
}
|