utils.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package common
  2. import (
  3. "bytes"
  4. "context"
  5. crand "crypto/rand"
  6. "encoding/base64"
  7. "fmt"
  8. "github.com/pkg/errors"
  9. "html/template"
  10. "io"
  11. "log"
  12. "math/big"
  13. "math/rand"
  14. "net"
  15. "os"
  16. "os/exec"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. "time"
  21. "github.com/google/uuid"
  22. )
  23. func OpenBrowser(url string) {
  24. var err error
  25. switch runtime.GOOS {
  26. case "linux":
  27. err = exec.Command("xdg-open", url).Start()
  28. case "windows":
  29. err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
  30. case "darwin":
  31. err = exec.Command("open", url).Start()
  32. }
  33. if err != nil {
  34. log.Println(err)
  35. }
  36. }
  37. func GetIp() (ip string) {
  38. ips, err := net.InterfaceAddrs()
  39. if err != nil {
  40. log.Println(err)
  41. return ip
  42. }
  43. for _, a := range ips {
  44. if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  45. if ipNet.IP.To4() != nil {
  46. ip = ipNet.IP.String()
  47. if strings.HasPrefix(ip, "10") {
  48. return
  49. }
  50. if strings.HasPrefix(ip, "172") {
  51. return
  52. }
  53. if strings.HasPrefix(ip, "192.168") {
  54. return
  55. }
  56. ip = ""
  57. }
  58. }
  59. }
  60. return
  61. }
  62. var sizeKB = 1024
  63. var sizeMB = sizeKB * 1024
  64. var sizeGB = sizeMB * 1024
  65. func Bytes2Size(num int64) string {
  66. numStr := ""
  67. unit := "B"
  68. if num/int64(sizeGB) > 1 {
  69. numStr = fmt.Sprintf("%.2f", float64(num)/float64(sizeGB))
  70. unit = "GB"
  71. } else if num/int64(sizeMB) > 1 {
  72. numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeMB)))
  73. unit = "MB"
  74. } else if num/int64(sizeKB) > 1 {
  75. numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeKB)))
  76. unit = "KB"
  77. } else {
  78. numStr = fmt.Sprintf("%d", num)
  79. }
  80. return numStr + " " + unit
  81. }
  82. func Seconds2Time(num int) (time string) {
  83. if num/31104000 > 0 {
  84. time += strconv.Itoa(num/31104000) + " 年 "
  85. num %= 31104000
  86. }
  87. if num/2592000 > 0 {
  88. time += strconv.Itoa(num/2592000) + " 个月 "
  89. num %= 2592000
  90. }
  91. if num/86400 > 0 {
  92. time += strconv.Itoa(num/86400) + " 天 "
  93. num %= 86400
  94. }
  95. if num/3600 > 0 {
  96. time += strconv.Itoa(num/3600) + " 小时 "
  97. num %= 3600
  98. }
  99. if num/60 > 0 {
  100. time += strconv.Itoa(num/60) + " 分钟 "
  101. num %= 60
  102. }
  103. time += strconv.Itoa(num) + " 秒"
  104. return
  105. }
  106. func Interface2String(inter interface{}) string {
  107. switch inter.(type) {
  108. case string:
  109. return inter.(string)
  110. case int:
  111. return fmt.Sprintf("%d", inter.(int))
  112. case float64:
  113. return fmt.Sprintf("%f", inter.(float64))
  114. }
  115. return "Not Implemented"
  116. }
  117. func UnescapeHTML(x string) interface{} {
  118. return template.HTML(x)
  119. }
  120. func IntMax(a int, b int) int {
  121. if a >= b {
  122. return a
  123. } else {
  124. return b
  125. }
  126. }
  127. func IsIP(s string) bool {
  128. ip := net.ParseIP(s)
  129. return ip != nil
  130. }
  131. func GetUUID() string {
  132. code := uuid.New().String()
  133. code = strings.Replace(code, "-", "", -1)
  134. return code
  135. }
  136. const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  137. func init() {
  138. rand.New(rand.NewSource(time.Now().UnixNano()))
  139. }
  140. func GenerateRandomCharsKey(length int) (string, error) {
  141. b := make([]byte, length)
  142. maxI := big.NewInt(int64(len(keyChars)))
  143. for i := range b {
  144. n, err := crand.Int(crand.Reader, maxI)
  145. if err != nil {
  146. return "", err
  147. }
  148. b[i] = keyChars[n.Int64()]
  149. }
  150. return string(b), nil
  151. }
  152. func GenerateRandomKey(length int) (string, error) {
  153. bytes := make([]byte, length*3/4) // 对于48位的输出,这里应该是36
  154. if _, err := crand.Read(bytes); err != nil {
  155. return "", err
  156. }
  157. return base64.StdEncoding.EncodeToString(bytes), nil
  158. }
  159. func GenerateKey() (string, error) {
  160. //rand.Seed(time.Now().UnixNano())
  161. return GenerateRandomCharsKey(48)
  162. }
  163. func GetRandomInt(max int) int {
  164. //rand.Seed(time.Now().UnixNano())
  165. return rand.Intn(max)
  166. }
  167. func GetTimestamp() int64 {
  168. return time.Now().Unix()
  169. }
  170. func GetTimeString() string {
  171. now := time.Now()
  172. return fmt.Sprintf("%s%d", now.Format("20060102150405"), now.UnixNano()%1e9)
  173. }
  174. func Max(a int, b int) int {
  175. if a >= b {
  176. return a
  177. } else {
  178. return b
  179. }
  180. }
  181. func MessageWithRequestId(message string, id string) string {
  182. return fmt.Sprintf("%s (request id: %s)", message, id)
  183. }
  184. func RandomSleep() {
  185. // Sleep for 0-3000 ms
  186. time.Sleep(time.Duration(rand.Intn(3000)) * time.Millisecond)
  187. }
  188. // SaveTmpFile saves data to a temporary file. The filename would be apppended with a random string.
  189. func SaveTmpFile(filename string, data io.Reader) (string, error) {
  190. f, err := os.CreateTemp(os.TempDir(), filename)
  191. if err != nil {
  192. return "", errors.Wrapf(err, "failed to create temporary file %s", filename)
  193. }
  194. defer f.Close()
  195. _, err = io.Copy(f, data)
  196. if err != nil {
  197. return "", errors.Wrapf(err, "failed to copy data to temporary file %s", filename)
  198. }
  199. return f.Name(), nil
  200. }
  201. // GetAudioDuration returns the duration of an audio file in seconds.
  202. func GetAudioDuration(ctx context.Context, filename string) (float64, error) {
  203. // ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {{input}}
  204. c := exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", filename)
  205. output, err := c.Output()
  206. if err != nil {
  207. return 0, errors.Wrap(err, "failed to get audio duration")
  208. }
  209. return strconv.ParseFloat(string(bytes.TrimSpace(output)), 64)
  210. }