utils.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "crypto/sha1"
  5. "database/sql"
  6. "encoding/hex"
  7. "fmt"
  8. "io"
  9. "mime/multipart"
  10. "os"
  11. "regexp"
  12. "time"
  13. )
  14. const (
  15. Person = iota + 1 // 个人版
  16. std //标准版
  17. ent //企业版
  18. cluster // 分布式
  19. lite = 6 //敏捷版本
  20. )
  21. func SwitchLicenseType(license string) int {
  22. if regexp.MustCompile("(?i)标准").MatchString(license) {
  23. return std
  24. }
  25. if regexp.MustCompile("(?i)分布式").MatchString(license) {
  26. return cluster
  27. }
  28. if regexp.MustCompile("(?i)企业").MatchString(license) {
  29. return ent
  30. }
  31. if regexp.MustCompile("(?i)敏捷").MatchString(license) {
  32. return lite
  33. }
  34. if regexp.MustCompile("(?i)个人").MatchString(license) {
  35. return Person
  36. }
  37. return 0
  38. }
  39. const maxFileNameLength = 255
  40. // GenerateFileName 生成新的文件名并确保不超过最大长度限制
  41. func GenerateFileName(name, timestamp, ext string) string {
  42. newFileName := name + "_" + timestamp + ext
  43. if len(newFileName) > maxFileNameLength {
  44. trimLength := len(newFileName) - maxFileNameLength
  45. if len(name) > trimLength {
  46. name = name[:len(name)-trimLength]
  47. } else {
  48. name = ""
  49. }
  50. newFileName = name + "_" + timestamp + ext
  51. }
  52. return newFileName
  53. }
  54. // CalculateFileMd5 计算文件的 MD5 值
  55. func CalculateFileMd5(filePath string) (string, error) {
  56. file, err := os.Open(filePath)
  57. if err != nil {
  58. return "", err
  59. }
  60. defer file.Close()
  61. hash := md5.New()
  62. if _, err := io.Copy(hash, file); err != nil {
  63. return "", err
  64. }
  65. return hex.EncodeToString(hash.Sum(nil)), nil
  66. }
  67. // calculateFileHeaderMd5 计算 *multipart.FileHeader 的 MD5 值
  68. func CalculateFileHeaderMd5(fileHeader *multipart.FileHeader) (string, error) {
  69. file, err := fileHeader.Open()
  70. if err != nil {
  71. return "", err
  72. }
  73. defer file.Close()
  74. hash := md5.New()
  75. if _, err := io.Copy(hash, file); err != nil {
  76. return "", err
  77. }
  78. return hex.EncodeToString(hash.Sum(nil)), nil
  79. }
  80. func GenerateShortIdentifier(username, email, telephone string) string {
  81. // 获取当前时间的时间戳
  82. timestamp := time.Now().UnixNano()
  83. // 将时间戳加入输入字符串
  84. input := fmt.Sprintf("%s%s%s%d", timestamp, email, telephone, username)
  85. // 生成哈希值
  86. hasher := sha1.New()
  87. hasher.Write([]byte(input))
  88. // 返回哈希的前12位作为唯一标识符
  89. return hex.EncodeToString(hasher.Sum(nil))[:12]
  90. }
  91. func ToString(ns sql.NullString) string {
  92. if ns.Valid {
  93. return ns.String
  94. }
  95. return ""
  96. }
  97. func ToInt64(ni sql.NullInt64) int64 {
  98. if ni.Valid {
  99. return ni.Int64
  100. }
  101. return 0
  102. }
  103. func ToTimeString(nt sql.NullTime) string {
  104. if nt.Valid {
  105. return nt.Time.Format(time.RFC3339)
  106. }
  107. return ""
  108. }
  109. // StringToNullString 将字符串转换为 sql.NullString
  110. func StringToNullString(s string) sql.NullString {
  111. if s == "" {
  112. return sql.NullString{
  113. String: "",
  114. Valid: false,
  115. }
  116. }
  117. return sql.NullString{
  118. String: s,
  119. Valid: true,
  120. }
  121. }
  122. // IntToNullInt64 将 int64 转换为 sql.NullInt64
  123. func IntToNullInt64(i int64) sql.NullInt64 {
  124. return sql.NullInt64{
  125. Int64: i,
  126. Valid: true,
  127. }
  128. }