charset_reader.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package mail
  2. import (
  3. "fmt"
  4. "golang.org/x/text/encoding/charmap"
  5. "golang.org/x/text/encoding/japanese"
  6. "golang.org/x/text/encoding/korean"
  7. "golang.org/x/text/encoding/simplifiedchinese"
  8. "golang.org/x/text/transform"
  9. "io"
  10. "strings"
  11. )
  12. var charsetMap = map[string]transform.Transformer{
  13. "gb2312": simplifiedchinese.GBK.NewDecoder(),
  14. "gbk": simplifiedchinese.GBK.NewDecoder(),
  15. "ibm037": charmap.CodePage037.NewDecoder(),
  16. "ibm437": charmap.CodePage437.NewDecoder(),
  17. "ibm850": charmap.CodePage850.NewDecoder(),
  18. "ibm852": charmap.CodePage852.NewDecoder(),
  19. "ibm855": charmap.CodePage855.NewDecoder(),
  20. "ibm858": charmap.CodePage858.NewDecoder(),
  21. "ibm860": charmap.CodePage860.NewDecoder(),
  22. "ibm862": charmap.CodePage862.NewDecoder(),
  23. "ibm863": charmap.CodePage863.NewDecoder(),
  24. "ibm865": charmap.CodePage865.NewDecoder(),
  25. "ibm866": charmap.CodePage866.NewDecoder(),
  26. "ibm1047": charmap.CodePage1047.NewDecoder(),
  27. "ibm1140": charmap.CodePage1140.NewDecoder(),
  28. "iso-8859-1": charmap.ISO8859_1.NewDecoder(),
  29. "iso-8859-2": charmap.ISO8859_2.NewDecoder(),
  30. "iso-8859-3": charmap.ISO8859_3.NewDecoder(),
  31. "iso-8859-4": charmap.ISO8859_4.NewDecoder(),
  32. "iso-8859-5": charmap.ISO8859_5.NewDecoder(),
  33. "iso-8859-6": charmap.ISO8859_6.NewDecoder(),
  34. "iso-8859-7": charmap.ISO8859_7.NewDecoder(),
  35. "iso-8859-8": charmap.ISO8859_8.NewDecoder(),
  36. "iso-8859-9": charmap.ISO8859_9.NewDecoder(),
  37. "iso-8859-10": charmap.ISO8859_10.NewDecoder(),
  38. "iso-8859-13": charmap.ISO8859_13.NewDecoder(),
  39. "iso-8859-14": charmap.ISO8859_14.NewDecoder(),
  40. "iso-8859-15": charmap.ISO8859_15.NewDecoder(),
  41. "iso-8859-16": charmap.ISO8859_16.NewDecoder(),
  42. "koi8-r": charmap.KOI8R.NewDecoder(),
  43. "koi8-u": charmap.KOI8U.NewDecoder(),
  44. "macintosh": charmap.Macintosh.NewDecoder(),
  45. "x-mac-cyrillic": charmap.MacintoshCyrillic.NewDecoder(),
  46. "windows-874": charmap.Windows874.NewDecoder(),
  47. "windows-1250": charmap.Windows1250.NewDecoder(),
  48. "windows-1251": charmap.Windows1251.NewDecoder(),
  49. "windows-1252": charmap.Windows1252.NewDecoder(),
  50. "windows-1253": charmap.Windows1253.NewDecoder(),
  51. "windows-1254": charmap.Windows1254.NewDecoder(),
  52. "windows-1255": charmap.Windows1255.NewDecoder(),
  53. "windows-1257": charmap.Windows1257.NewDecoder(),
  54. "windows-1258": charmap.Windows1258.NewDecoder(),
  55. "x-user-defined": charmap.XUserDefined.NewDecoder(),
  56. "euc-jp": japanese.EUCJP.NewDecoder(),
  57. "iso-2022-jp": japanese.ISO2022JP.NewDecoder(),
  58. "shift_jis": japanese.ShiftJIS.NewDecoder(),
  59. "ks_c_5601-1987": korean.EUCKR.NewDecoder(),
  60. "euc-kr": korean.EUCKR.NewDecoder(),
  61. }
  62. // 定义一个自定义的 CharsetReader 函数,它能够处理 gb2312 和 gbk 字符集
  63. func myCharsetReader(charset string, input io.Reader) (io.Reader, error) {
  64. charset = strings.ToLower(charset)
  65. newDecoder, ok := charsetMap[charset]
  66. if ok {
  67. reader := transform.NewReader(input, newDecoder)
  68. return reader, nil
  69. }
  70. if charset == `utf-8` {
  71. return input, nil
  72. }
  73. switch strings.ToLower(charset) {
  74. case "gb2312", "gbk":
  75. reader := transform.NewReader(input, simplifiedchinese.GBK.NewDecoder())
  76. return reader, nil
  77. case "utf-8":
  78. return input, nil
  79. case "iso-8859-1":
  80. reader := transform.NewReader(input, charmap.ISO8859_1.NewDecoder())
  81. return reader, nil
  82. case "windows-1252":
  83. reader := transform.NewReader(input, charmap.Windows1252.NewDecoder())
  84. return reader, nil
  85. default:
  86. }
  87. return input, fmt.Errorf("unsupported charset: %s", charset)
  88. }