utils.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "fmt"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "github.com/lib/pq"
  10. )
  11. var (
  12. // This replaces all special characters
  13. tagRegexp = regexp.MustCompile(`[^a-z0-9\-\s]`)
  14. tagRegexpSpaces = regexp.MustCompile(`[\s]+`)
  15. )
  16. // validateMIME is a helper function to validate uploaded file's MIME type
  17. // against the slice of MIME types is given.
  18. func validateMIME(typ string, mimes []string) (ok bool) {
  19. if len(mimes) > 0 {
  20. var (
  21. ok = false
  22. )
  23. for _, m := range mimes {
  24. if typ == m {
  25. ok = true
  26. break
  27. }
  28. }
  29. if !ok {
  30. return false
  31. }
  32. }
  33. return true
  34. }
  35. // generateFileName appends the incoming file's name with a small random hash.
  36. func generateFileName(fName string) string {
  37. name := strings.TrimSpace(fName)
  38. if name == "" {
  39. name, _ = generateRandomString(10)
  40. }
  41. return name
  42. }
  43. // Given an error, pqErrMsg will try to return pq error details
  44. // if it's a pq error.
  45. func pqErrMsg(err error) string {
  46. if err, ok := err.(*pq.Error); ok {
  47. if err.Detail != "" {
  48. return fmt.Sprintf("%s. %s", err, err.Detail)
  49. }
  50. }
  51. return err.Error()
  52. }
  53. // normalizeTags takes a list of string tags and normalizes them by
  54. // lowercasing and removing all special characters except for dashes.
  55. func normalizeTags(tags []string) []string {
  56. var (
  57. out []string
  58. space = []byte(" ")
  59. dash = []byte("-")
  60. )
  61. for _, t := range tags {
  62. rep := bytes.TrimSpace(tagRegexp.ReplaceAll(bytes.ToLower([]byte(t)), space))
  63. rep = tagRegexpSpaces.ReplaceAll(rep, dash)
  64. if len(rep) > 0 {
  65. out = append(out, string(rep))
  66. }
  67. }
  68. return out
  69. }
  70. // makeMsgTpl takes a page title, heading, and message and returns
  71. // a msgTpl that can be rendered as a HTML view. This is used for
  72. // rendering arbitrary HTML views with error and success messages.
  73. func makeMsgTpl(pageTitle, heading, msg string) msgTpl {
  74. if heading == "" {
  75. heading = pageTitle
  76. }
  77. err := msgTpl{}
  78. err.Title = pageTitle
  79. err.MessageTitle = heading
  80. err.Message = msg
  81. return err
  82. }
  83. // parseStringIDs takes a slice of numeric string IDs and
  84. // parses each number into an int64 and returns a slice of the
  85. // resultant values.
  86. func parseStringIDs(s []string) ([]int64, error) {
  87. vals := make([]int64, 0, len(s))
  88. for _, v := range s {
  89. i, err := strconv.ParseInt(v, 10, 64)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if i < 1 {
  94. return nil, fmt.Errorf("%d is not a valid ID", i)
  95. }
  96. vals = append(vals, i)
  97. }
  98. return vals, nil
  99. }
  100. // generateRandomString generates a cryptographically random, alphanumeric string of length n.
  101. func generateRandomString(n int) (string, error) {
  102. const dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  103. var bytes = make([]byte, n)
  104. if _, err := rand.Read(bytes); err != nil {
  105. return "", err
  106. }
  107. for k, v := range bytes {
  108. bytes[k] = dictionary[v%byte(len(dictionary))]
  109. }
  110. return string(bytes), nil
  111. }
  112. // strHasLen checks if the given string has a length within min-max.
  113. func strHasLen(str string, min, max int) bool {
  114. return len(str) >= min && len(str) <= max
  115. }