实现兼容gbk编码格式

pull/30/head
陶士涵 5 years ago
parent d00525e887
commit 7795a04129

@ -12,8 +12,8 @@ import (
"sync" "sync"
) )
const PAGE_SIZE = 20 const PAGE_SIZE = 20
func main() { func main() {
log.Println("listen on 8080...") log.Println("listen on 8080...")
http.HandleFunc("/", index) http.HandleFunc("/", index)
@ -64,7 +64,6 @@ func list(w http.ResponseWriter, r *http.Request) {
currentPage = 1 currentPage = 1
} }
auth := getCookie(r, "auth") auth := getCookie(r, "auth")
authStrings := strings.Split(auth, "|") authStrings := strings.Split(auth, "|")
@ -158,6 +157,7 @@ func view(w http.ResponseWriter, r *http.Request) {
wg.Wait() wg.Wait()
tmpl.RenderView(w, render) tmpl.RenderView(w, render)
} }
//登陆界面 //登陆界面
func login(w http.ResponseWriter, r *http.Request) { func login(w http.ResponseWriter, r *http.Request) {
email := r.PostFormValue("email") email := r.PostFormValue("email")
@ -182,6 +182,7 @@ func login(w http.ResponseWriter, r *http.Request) {
tmpl.RenderLogin(w, errStr) tmpl.RenderLogin(w, errStr)
} }
} }
//加密cookie //加密cookie
//func authCookie(){ //func authCookie(){
// //

@ -43,7 +43,7 @@ func RenderView(w http.ResponseWriter,render interface{}){
<table class="table table-hover"> <table class="table table-hover">
<tbody> <tbody>
<tr> <tr>
<th scope="row">:</th> <th scope="row" width="100">:</th>
<td>{{.Date}}</td> <td>{{.Date}}</td>
</tr> </tr>
<tr> <tr>

@ -14,6 +14,7 @@ import (
"strconv" "strconv"
"strings" "strings"
) )
//验证邮箱密码 //验证邮箱密码
func CheckEmailPassword(server string, email string, password string) bool { func CheckEmailPassword(server string, email string, password string) bool {
if !strings.Contains(server, ":") { if !strings.Contains(server, ":") {
@ -36,6 +37,7 @@ func CheckEmailPassword(server string, email string, password string) bool {
} }
return true return true
} }
//获取连接 //获取连接
func connect(server string, email string, password string) *client.Client { func connect(server string, email string, password string) *client.Client {
var c *client.Client var c *client.Client
@ -61,6 +63,7 @@ func connect(server string, email string, password string)*client.Client{
} }
return c return c
} }
//获取邮件夹 //获取邮件夹
func GetFolders(server string, email string, password string, folder string) map[string]int { func GetFolders(server string, email string, password string, folder string) map[string]int {
var c *client.Client var c *client.Client
@ -188,17 +191,18 @@ func GetMessage(server string, email string, password string,folder string,id ui
// Print some info about the message // Print some info about the message
header := mr.Header header := mr.Header
if date, err := header.Date(); err == nil { log.Println(header)
date, _ := header.Date()
log.Println("Date:", date) log.Println("Date:", date)
mailitem.Date = date.String() mailitem.Date = date.String()
}
var f string var f string
dec := GetDecoder() dec := GetDecoder()
if from, err := header.AddressList("From"); err == nil { if from, err := header.AddressList("From"); err == nil {
log.Println("From:", from) log.Println("From:", from)
for _, address := range from { for _, address := range from {
fromStr:=strings.ToLower(address.String()) fromStr := address.String()
temp, _ := dec.DecodeHeader(fromStr) temp, _ := dec.DecodeHeader(fromStr)
f += " " + temp f += " " + temp
} }
@ -208,22 +212,20 @@ func GetMessage(server string, email string, password string,folder string,id ui
if to, err := header.AddressList("To"); err == nil { if to, err := header.AddressList("To"); err == nil {
log.Println("To:", to) log.Println("To:", to)
for _, address := range to { for _, address := range to {
toStr:=strings.ToLower(address.String()) toStr := address.String()
temp, _ := dec.DecodeHeader(toStr) temp, _ := dec.DecodeHeader(toStr)
t += " " + temp t += " " + temp
} }
} }
mailitem.To = t mailitem.To = t
if subject, err := header.Subject(); err == nil { subject, _ := header.Subject()
subject=strings.ToLower(subject)
s, err := dec.Decode(subject) s, err := dec.Decode(subject)
if err != nil { if err != nil {
s, _ = dec.DecodeHeader(subject) s, _ = dec.DecodeHeader(subject)
} }
log.Println("Subject:", s) log.Println("Subject:", s)
mailitem.Subject = s mailitem.Subject = s
}
// Process each message's part // Process each message's part
for { for {
p, err := mr.NextPart() p, err := mr.NextPart()
@ -243,7 +245,9 @@ func GetMessage(server string, email string, password string,folder string,id ui
filename, _ := h.Filename() filename, _ := h.Filename()
log.Println("Got attachment: ", filename) log.Println("Got attachment: ", filename)
} }
mailitem.Body=Encoding(mailitem.Body) ct,_,_:=header.ContentType()
log.Println(ct)
mailitem.Body = Encoding(mailitem.Body,ct)
} }
return mailitem return mailitem
} }
@ -298,12 +302,14 @@ func GetDecoder()*mime.WordDecoder{
} }
return dec return dec
} }
// 任意编码转特定编码 // 任意编码转特定编码
func ConvertToStr(src string, srcCode string, tagCode string) string { func ConvertToStr(src string, srcCode string, tagCode string) string {
srcCoder := mahonia.NewDecoder(srcCode) result := mahonia.NewDecoder(srcCode).ConvertString(src)
srcResult := srcCoder.ConvertString(src) //srcCoder := mahonia.NewDecoder(srcCode)
tagCoder := mahonia.NewDecoder(tagCode) //srcResult := srcCoder.ConvertString(src)
_, cdata, _ := tagCoder.Translate([]byte(srcResult), true) //tagCoder := mahonia.NewDecoder(tagCode)
result := string(cdata) //_, cdata, _ := tagCoder.Translate([]byte(srcResult), true)
//result := string(cdata)
return result return result
} }

@ -3,6 +3,8 @@ package tools
import ( import (
"golang.org/x/net/html/charset" "golang.org/x/net/html/charset"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform" "golang.org/x/text/transform"
"io/ioutil" "io/ioutil"
"log" "log"
@ -17,14 +19,23 @@ func Reverse(s string) string {
} }
return string(r) return string(r)
} }
//转换编码 //转换编码
func Encoding(html string)string { func Encoding(html string,ct string) string {
e,_,_ :=charset.DetermineEncoding([]byte(html),"") e,name:=DetermineEncoding(html)
if name!="utf-8"{
html=ConvertToStr(html,"gbk","utf-8")
e=unicode.UTF8
}
r := strings.NewReader(html) r := strings.NewReader(html)
log.Println(r);
utf8Reader := transform.NewReader(r, e.NewDecoder()) utf8Reader := transform.NewReader(r, e.NewDecoder())
//将其他编码的reader转换为常用的utf8reader //将其他编码的reader转换为常用的utf8reader
all, _ := ioutil.ReadAll(utf8Reader) all, _ := ioutil.ReadAll(utf8Reader)
log.Println(string(all))
return string(all) return string(all)
} }
func DetermineEncoding(html string) (encoding.Encoding,string) {
e, name, _ := charset.DetermineEncoding([]byte(html), "")
return e,name
}

@ -10,7 +10,6 @@ type IndexData struct {
Fid string Fid string
NextPage, PrePage string NextPage, PrePage string
NumPages template.HTML NumPages template.HTML
} }
type ViewData struct { type ViewData struct {
Folders map[string]int Folders map[string]int

Loading…
Cancel
Save