util.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package util
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gabriel-vasile/mimetype"
  6. "math/rand"
  7. "os"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. )
  14. const (
  15. randomStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  16. )
  17. var (
  18. random = rand.New(rand.NewSource(time.Now().UnixNano()))
  19. randomMutex = sync.Mutex{}
  20. sizeStrRegex = regexp.MustCompile(`(?i)^(\d+)([gmkb])?$`)
  21. errInvalidPriority = errors.New("invalid priority")
  22. )
  23. // FileExists checks if a file exists, and returns true if it does
  24. func FileExists(filename string) bool {
  25. stat, _ := os.Stat(filename)
  26. return stat != nil
  27. }
  28. // InStringList returns true if needle is contained in haystack
  29. func InStringList(haystack []string, needle string) bool {
  30. for _, s := range haystack {
  31. if s == needle {
  32. return true
  33. }
  34. }
  35. return false
  36. }
  37. // InStringListAll returns true if all needles are contained in haystack
  38. func InStringListAll(haystack []string, needles []string) bool {
  39. matches := 0
  40. for _, s := range haystack {
  41. for _, needle := range needles {
  42. if s == needle {
  43. matches++
  44. }
  45. }
  46. }
  47. return matches == len(needles)
  48. }
  49. // InIntList returns true if needle is contained in haystack
  50. func InIntList(haystack []int, needle int) bool {
  51. for _, s := range haystack {
  52. if s == needle {
  53. return true
  54. }
  55. }
  56. return false
  57. }
  58. // SplitNoEmpty splits a string using strings.Split, but filters out empty strings
  59. func SplitNoEmpty(s string, sep string) []string {
  60. res := make([]string, 0)
  61. for _, r := range strings.Split(s, sep) {
  62. if r != "" {
  63. res = append(res, r)
  64. }
  65. }
  66. return res
  67. }
  68. // RandomString returns a random string with a given length
  69. func RandomString(length int) string {
  70. randomMutex.Lock() // Who would have thought that random.Intn() is not thread-safe?!
  71. defer randomMutex.Unlock()
  72. b := make([]byte, length)
  73. for i := range b {
  74. b[i] = randomStringCharset[random.Intn(len(randomStringCharset))]
  75. }
  76. return string(b)
  77. }
  78. // DurationToHuman converts a duration to a human readable format
  79. func DurationToHuman(d time.Duration) (str string) {
  80. if d == 0 {
  81. return "0"
  82. }
  83. d = d.Round(time.Second)
  84. days := d / time.Hour / 24
  85. if days > 0 {
  86. str += fmt.Sprintf("%dd", days)
  87. }
  88. d -= days * time.Hour * 24
  89. hours := d / time.Hour
  90. if hours > 0 {
  91. str += fmt.Sprintf("%dh", hours)
  92. }
  93. d -= hours * time.Hour
  94. minutes := d / time.Minute
  95. if minutes > 0 {
  96. str += fmt.Sprintf("%dm", minutes)
  97. }
  98. d -= minutes * time.Minute
  99. seconds := d / time.Second
  100. if seconds > 0 {
  101. str += fmt.Sprintf("%ds", seconds)
  102. }
  103. return
  104. }
  105. // ParsePriority parses a priority string into its equivalent integer value
  106. func ParsePriority(priority string) (int, error) {
  107. switch strings.TrimSpace(strings.ToLower(priority)) {
  108. case "":
  109. return 0, nil
  110. case "1", "min":
  111. return 1, nil
  112. case "2", "low":
  113. return 2, nil
  114. case "3", "default":
  115. return 3, nil
  116. case "4", "high":
  117. return 4, nil
  118. case "5", "max", "urgent":
  119. return 5, nil
  120. default:
  121. return 0, errInvalidPriority
  122. }
  123. }
  124. // PriorityString converts a priority number to a string
  125. func PriorityString(priority int) (string, error) {
  126. switch priority {
  127. case 0:
  128. return "default", nil
  129. case 1:
  130. return "min", nil
  131. case 2:
  132. return "low", nil
  133. case 3:
  134. return "default", nil
  135. case 4:
  136. return "high", nil
  137. case 5:
  138. return "max", nil
  139. default:
  140. return "", errInvalidPriority
  141. }
  142. }
  143. // ExpandHome replaces "~" with the user's home directory
  144. func ExpandHome(path string) string {
  145. return os.ExpandEnv(strings.ReplaceAll(path, "~", "$HOME"))
  146. }
  147. // ShortTopicURL shortens the topic URL to be human-friendly, removing the http:// or https://
  148. func ShortTopicURL(s string) string {
  149. return strings.TrimPrefix(strings.TrimPrefix(s, "https://"), "http://")
  150. }
  151. // DetectContentType probes the byte array b and returns mime type and file extension.
  152. // The filename is only used to override certain special cases.
  153. func DetectContentType(b []byte, filename string) (mimeType string, ext string) {
  154. if strings.HasSuffix(strings.ToLower(filename), ".apk") {
  155. return "application/vnd.android.package-archive", ".apk"
  156. }
  157. m := mimetype.Detect(b)
  158. mimeType, ext = m.String(), m.Extension()
  159. if ext == "" {
  160. ext = ".bin"
  161. }
  162. return
  163. }
  164. // ParseSize parses a size string like 2K or 2M into bytes. If no unit is found, e.g. 123, bytes is assumed.
  165. func ParseSize(s string) (int64, error) {
  166. matches := sizeStrRegex.FindStringSubmatch(s)
  167. if matches == nil {
  168. return -1, fmt.Errorf("invalid size %s", s)
  169. }
  170. value, err := strconv.Atoi(matches[1])
  171. if err != nil {
  172. return -1, fmt.Errorf("cannot convert number %s", matches[1])
  173. }
  174. switch strings.ToUpper(matches[2]) {
  175. case "G":
  176. return int64(value) * 1024 * 1024 * 1024, nil
  177. case "M":
  178. return int64(value) * 1024 * 1024, nil
  179. case "K":
  180. return int64(value) * 1024, nil
  181. default:
  182. return int64(value), nil
  183. }
  184. }