util.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package util
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "mime"
  7. "os"
  8. "strings"
  9. "sync"
  10. "time"
  11. )
  12. const (
  13. randomStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  14. )
  15. var (
  16. random = rand.New(rand.NewSource(time.Now().UnixNano()))
  17. randomMutex = sync.Mutex{}
  18. errInvalidPriority = errors.New("invalid priority")
  19. )
  20. // FileExists checks if a file exists, and returns true if it does
  21. func FileExists(filename string) bool {
  22. stat, _ := os.Stat(filename)
  23. return stat != nil
  24. }
  25. // InStringList returns true if needle is contained in haystack
  26. func InStringList(haystack []string, needle string) bool {
  27. for _, s := range haystack {
  28. if s == needle {
  29. return true
  30. }
  31. }
  32. return false
  33. }
  34. // InStringListAll returns true if all needles are contained in haystack
  35. func InStringListAll(haystack []string, needles []string) bool {
  36. matches := 0
  37. for _, s := range haystack {
  38. for _, needle := range needles {
  39. if s == needle {
  40. matches++
  41. }
  42. }
  43. }
  44. return matches == len(needles)
  45. }
  46. // InIntList returns true if needle is contained in haystack
  47. func InIntList(haystack []int, needle int) bool {
  48. for _, s := range haystack {
  49. if s == needle {
  50. return true
  51. }
  52. }
  53. return false
  54. }
  55. // SplitNoEmpty splits a string using strings.Split, but filters out empty strings
  56. func SplitNoEmpty(s string, sep string) []string {
  57. res := make([]string, 0)
  58. for _, r := range strings.Split(s, sep) {
  59. if r != "" {
  60. res = append(res, r)
  61. }
  62. }
  63. return res
  64. }
  65. // RandomString returns a random string with a given length
  66. func RandomString(length int) string {
  67. randomMutex.Lock() // Who would have thought that random.Intn() is not thread-safe?!
  68. defer randomMutex.Unlock()
  69. b := make([]byte, length)
  70. for i := range b {
  71. b[i] = randomStringCharset[random.Intn(len(randomStringCharset))]
  72. }
  73. return string(b)
  74. }
  75. // DurationToHuman converts a duration to a human readable format
  76. func DurationToHuman(d time.Duration) (str string) {
  77. if d == 0 {
  78. return "0"
  79. }
  80. d = d.Round(time.Second)
  81. days := d / time.Hour / 24
  82. if days > 0 {
  83. str += fmt.Sprintf("%dd", days)
  84. }
  85. d -= days * time.Hour * 24
  86. hours := d / time.Hour
  87. if hours > 0 {
  88. str += fmt.Sprintf("%dh", hours)
  89. }
  90. d -= hours * time.Hour
  91. minutes := d / time.Minute
  92. if minutes > 0 {
  93. str += fmt.Sprintf("%dm", minutes)
  94. }
  95. d -= minutes * time.Minute
  96. seconds := d / time.Second
  97. if seconds > 0 {
  98. str += fmt.Sprintf("%ds", seconds)
  99. }
  100. return
  101. }
  102. // ParsePriority parses a priority string into its equivalent integer value
  103. func ParsePriority(priority string) (int, error) {
  104. switch strings.TrimSpace(strings.ToLower(priority)) {
  105. case "":
  106. return 0, nil
  107. case "1", "min":
  108. return 1, nil
  109. case "2", "low":
  110. return 2, nil
  111. case "3", "default":
  112. return 3, nil
  113. case "4", "high":
  114. return 4, nil
  115. case "5", "max", "urgent":
  116. return 5, nil
  117. default:
  118. return 0, errInvalidPriority
  119. }
  120. }
  121. // PriorityString converts a priority number to a string
  122. func PriorityString(priority int) (string, error) {
  123. switch priority {
  124. case 0:
  125. return "default", nil
  126. case 1:
  127. return "min", nil
  128. case 2:
  129. return "low", nil
  130. case 3:
  131. return "default", nil
  132. case 4:
  133. return "high", nil
  134. case 5:
  135. return "max", nil
  136. default:
  137. return "", errInvalidPriority
  138. }
  139. }
  140. // ExpandHome replaces "~" with the user's home directory
  141. func ExpandHome(path string) string {
  142. return os.ExpandEnv(strings.ReplaceAll(path, "~", "$HOME"))
  143. }
  144. // ShortTopicURL shortens the topic URL to be human-friendly, removing the http:// or https://
  145. func ShortTopicURL(s string) string {
  146. return strings.TrimPrefix(strings.TrimPrefix(s, "https://"), "http://")
  147. }
  148. // ExtensionByType is a wrapper around mime.ExtensionByType with a few sensible corrections
  149. func ExtensionByType(contentType string) string {
  150. switch contentType {
  151. case "image/jpeg":
  152. return ".jpg"
  153. default:
  154. exts, err := mime.ExtensionsByType(contentType)
  155. if err == nil && len(exts) > 0 {
  156. return exts[0]
  157. }
  158. return ".bin"
  159. }
  160. }