golang 的 rabbitmq 消费项目
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

109 linhas
2.1 KiB

  1. package utils
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. )
  8. const (
  9. Base64Std = iota
  10. Base64Url
  11. Base64RawStd
  12. Base64RawUrl
  13. )
  14. func Base64StdEncode(str interface{}) string {
  15. return Base64Encode(str, Base64Std)
  16. }
  17. func Base64StdDecode(str interface{}) string {
  18. return Base64Decode(str, Base64Std)
  19. }
  20. func Base64UrlEncode(str interface{}) string {
  21. return Base64Encode(str, Base64Url)
  22. }
  23. func Base64UrlDecode(str interface{}) string {
  24. return Base64Decode(str, Base64Url)
  25. }
  26. func Base64RawStdEncode(str interface{}) string {
  27. return Base64Encode(str, Base64RawStd)
  28. }
  29. func Base64RawStdDecode(str interface{}) string {
  30. return Base64Decode(str, Base64RawStd)
  31. }
  32. func Base64RawUrlEncode(str interface{}) string {
  33. return Base64Encode(str, Base64RawUrl)
  34. }
  35. func Base64RawUrlDecode(str interface{}) string {
  36. return Base64Decode(str, Base64RawUrl)
  37. }
  38. func Base64Encode(str interface{}, encode int) string {
  39. newEncode := base64Encode(encode)
  40. if newEncode == nil {
  41. return ""
  42. }
  43. switch v := str.(type) {
  44. case string:
  45. return newEncode.EncodeToString([]byte(v))
  46. case []byte:
  47. return newEncode.EncodeToString(v)
  48. }
  49. return newEncode.EncodeToString([]byte(fmt.Sprint(str)))
  50. }
  51. func Base64Decode(str interface{}, encode int) string {
  52. var err error
  53. var b []byte
  54. newEncode := base64Encode(encode)
  55. if newEncode == nil {
  56. return ""
  57. }
  58. switch v := str.(type) {
  59. case string:
  60. b, err = newEncode.DecodeString(v)
  61. case []byte:
  62. b, err = newEncode.DecodeString(string(v))
  63. default:
  64. return ""
  65. }
  66. if err != nil {
  67. return ""
  68. }
  69. return string(b)
  70. }
  71. func base64Encode(encode int) *base64.Encoding {
  72. switch encode {
  73. case Base64Std:
  74. return base64.StdEncoding
  75. case Base64Url:
  76. return base64.URLEncoding
  77. case Base64RawStd:
  78. return base64.RawStdEncoding
  79. case Base64RawUrl:
  80. return base64.RawURLEncoding
  81. default:
  82. return nil
  83. }
  84. }
  85. func GetBase64FromUrlForImage(url string) (string, error) {
  86. res, err := http.Get(url)
  87. if err != nil {
  88. return "", err
  89. }
  90. defer res.Body.Close()
  91. data, _ := ioutil.ReadAll(res.Body)
  92. imageBase64 := base64.StdEncoding.EncodeToString(data)
  93. return imageBase64, nil
  94. }