utils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. input := fmt.Sprintf("%s%s%s", username, email, telephone)
  82. hasher := sha1.New()
  83. hasher.Write([]byte(input))
  84. return hex.EncodeToString(hasher.Sum(nil))[:12] // 截取前8位作为唯一标识符
  85. }
  86. func ToString(ns sql.NullString) string {
  87. if ns.Valid {
  88. return ns.String
  89. }
  90. return ""
  91. }
  92. func ToInt64(ni sql.NullInt64) int64 {
  93. if ni.Valid {
  94. return ni.Int64
  95. }
  96. return 0
  97. }
  98. func ToTimeString(nt sql.NullTime) string {
  99. if nt.Valid {
  100. return nt.Time.Format(time.RFC3339)
  101. }
  102. return ""
  103. }
  104. // StringToNullString 将字符串转换为 sql.NullString
  105. func StringToNullString(s string) sql.NullString {
  106. if s == "" {
  107. return sql.NullString{
  108. String: "",
  109. Valid: false,
  110. }
  111. }
  112. return sql.NullString{
  113. String: s,
  114. Valid: true,
  115. }
  116. }
  117. // IntToNullInt64 将 int64 转换为 sql.NullInt64
  118. func IntToNullInt64(i int64) sql.NullInt64 {
  119. return sql.NullInt64{
  120. Int64: i,
  121. Valid: true,
  122. }
  123. }