utils.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "os"
  7. "strings"
  8. "time"
  9. "github.com/google/uuid"
  10. )
  11. func Hmac(data string) string {
  12. h := md5.New()
  13. h.Write([]byte(data))
  14. return hex.EncodeToString(h.Sum(nil))
  15. }
  16. func IsStringEmpty(str string) bool {
  17. return strings.Trim(str, " ") == ""
  18. }
  19. func GetUUID() string {
  20. u := uuid.New()
  21. return strings.ReplaceAll(u.String(), "-", "")
  22. }
  23. func PathExists(path string) bool {
  24. _, err := os.Stat(path)
  25. if err == nil {
  26. return true
  27. }
  28. if os.IsNotExist(err) {
  29. return false
  30. }
  31. return false
  32. }
  33. func Base64ToImage(imageBase64 string) ([]byte, error) {
  34. image, err := base64.StdEncoding.DecodeString(imageBase64)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return image, nil
  39. }
  40. func GetDirFiles(dir string) ([]string, error) {
  41. dirList, err := os.ReadDir(dir)
  42. if err != nil {
  43. return nil, err
  44. }
  45. filesRet := make([]string, 0)
  46. for _, file := range dirList {
  47. if file.IsDir() {
  48. files, err := GetDirFiles(dir + string(os.PathSeparator) + file.Name())
  49. if err != nil {
  50. return nil, err
  51. }
  52. filesRet = append(filesRet, files...)
  53. } else {
  54. filesRet = append(filesRet, dir+string(os.PathSeparator)+file.Name())
  55. }
  56. }
  57. return filesRet, nil
  58. }
  59. func GetCurrentTimeStamp() int64 {
  60. return time.Now().UnixNano() / 1e6
  61. }
  62. // RemoveRepByMap slice去重
  63. func RemoveRepByMap(slc []string) []string {
  64. var result []string
  65. tempMap := map[string]byte{}
  66. for _, e := range slc {
  67. l := len(tempMap)
  68. tempMap[e] = 0
  69. if len(tempMap) != l {
  70. result = append(result, e)
  71. }
  72. }
  73. return result
  74. }