utils.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "fmt"
  6. "path/filepath"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. )
  11. var (
  12. regexpSpaces = regexp.MustCompile(`[\s]+`)
  13. )
  14. // inArray checks if a string is present in a list of strings.
  15. func inArray(val string, vals []string) (ok bool) {
  16. for _, v := range vals {
  17. if v == val {
  18. return true
  19. }
  20. }
  21. return false
  22. }
  23. // makeFilename sanitizes a filename (user supplied upload filenames).
  24. func makeFilename(fName string) string {
  25. name := strings.TrimSpace(fName)
  26. if name == "" {
  27. name, _ = generateRandomString(10)
  28. }
  29. // replace whitespace with "-"
  30. name = regexpSpaces.ReplaceAllString(name, "-")
  31. return filepath.Base(name)
  32. }
  33. // makeMsgTpl takes a page title, heading, and message and returns
  34. // a msgTpl that can be rendered as an HTML view. This is used for
  35. // rendering arbitrary HTML views with error and success messages.
  36. func makeMsgTpl(pageTitle, heading, msg string) msgTpl {
  37. if heading == "" {
  38. heading = pageTitle
  39. }
  40. err := msgTpl{}
  41. err.Title = pageTitle
  42. err.MessageTitle = heading
  43. err.Message = msg
  44. return err
  45. }
  46. // parseStringIDs takes a slice of numeric string IDs and
  47. // parses each number into an int64 and returns a slice of the
  48. // resultant values.
  49. func parseStringIDs(s []string) ([]int, error) {
  50. vals := make([]int, 0, len(s))
  51. for _, v := range s {
  52. i, err := strconv.Atoi(v)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if i < 1 {
  57. return nil, fmt.Errorf("%d is not a valid ID", i)
  58. }
  59. vals = append(vals, i)
  60. }
  61. return vals, nil
  62. }
  63. // generateRandomString generates a cryptographically random, alphanumeric string of length n.
  64. func generateRandomString(n int) (string, error) {
  65. const dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  66. var bytes = make([]byte, n)
  67. if _, err := rand.Read(bytes); err != nil {
  68. return "", err
  69. }
  70. for k, v := range bytes {
  71. bytes[k] = dictionary[v%byte(len(dictionary))]
  72. }
  73. return string(bytes), nil
  74. }
  75. // strHasLen checks if the given string has a length within min-max.
  76. func strHasLen(str string, min, max int) bool {
  77. return len(str) >= min && len(str) <= max
  78. }
  79. // strSliceContains checks if a string is present in the string slice.
  80. func strSliceContains(str string, sl []string) bool {
  81. for _, s := range sl {
  82. if s == str {
  83. return true
  84. }
  85. }
  86. return false
  87. }
  88. func trimNullBytes(b []byte) string {
  89. return string(bytes.Trim(b, "\x00"))
  90. }