util.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package service
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "math"
  10. "math/rand"
  11. "os"
  12. "time"
  13. )
  14. const timeFormat = "2006-01-02 15:04:05"
  15. func nowFormat() string {
  16. return time.Now().Format(timeFormat)
  17. }
  18. func GenToken(l int) string {
  19. s := "0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM"
  20. tkn := bytes.Buffer{}
  21. for i := 0; i < l; i++ {
  22. idx := rand.Int() % len(s)
  23. tkn.WriteByte(s[idx])
  24. }
  25. return tkn.String()
  26. }
  27. func makeFilePart(name, part string) string {
  28. return fmt.Sprintf("%s.part%s", name, part)
  29. }
  30. // 文件 md5 值
  31. func fileMD5(filename string) (string, error) {
  32. if info, err := os.Stat(filename); err != nil {
  33. return "", err
  34. } else if info.IsDir() {
  35. return "", errors.New(fmt.Sprintf("%s is a dir", filename))
  36. }
  37. f, err := os.Open(filename)
  38. if err != nil {
  39. return "", err
  40. }
  41. defer f.Close()
  42. h := md5.New()
  43. if _, err = io.Copy(h, f); err != nil {
  44. return "", err
  45. }
  46. return hex.EncodeToString(h.Sum(nil)), nil
  47. }
  48. func Must(i interface{}, err error) interface{} {
  49. if err != nil {
  50. panic(err)
  51. }
  52. return i
  53. }
  54. func WriteFile(filename string, reader io.Reader) (written int64, err error) {
  55. newFile, err := os.Create(filename)
  56. if err != nil {
  57. return 0, err
  58. }
  59. defer newFile.Close()
  60. return io.Copy(newFile, reader)
  61. }
  62. func CopyFile(src, dest string) (written int64, err error) {
  63. srcF, err := os.Open(src)
  64. if err != nil {
  65. return 0, err
  66. }
  67. defer srcF.Close()
  68. return WriteFile(dest, srcF)
  69. }
  70. var (
  71. KB = uint64(math.Pow(2, 10))
  72. MB = uint64(math.Pow(2, 20))
  73. GB = uint64(math.Pow(2, 30))
  74. TB = uint64(math.Pow(2, 40))
  75. )
  76. func CelsiusToFahrenheit(c int) int {
  77. return c*9/5 + 32
  78. }
  79. func BytesToKB(b uint64) float64 {
  80. return float64(b) / float64(KB)
  81. }
  82. func BytesToMB(b uint64) float64 {
  83. return float64(b) / float64(MB)
  84. }
  85. func BytesToGB(b uint64) float64 {
  86. return float64(b) / float64(GB)
  87. }
  88. func BytesToTB(b uint64) float64 {
  89. return float64(b) / float64(TB)
  90. }
  91. func ConvertBytes(b uint64) (float64, string) {
  92. switch {
  93. case b < KB:
  94. return float64(b), "B"
  95. case b < MB:
  96. return BytesToKB(b), "KB"
  97. case b < GB:
  98. return BytesToMB(b), "MB"
  99. case b < TB:
  100. return BytesToGB(b), "GB"
  101. default:
  102. return BytesToTB(b), "TB"
  103. }
  104. }
  105. func ConvertBytesString(b uint64) string {
  106. cf, s := ConvertBytes(b)
  107. return fmt.Sprintf("%.1f%s", cf, s)
  108. }
  109. func init() {
  110. rand.Seed(time.Now().Unix())
  111. }