实现兼容gbk编码格式

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

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

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

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

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

Loading…
Cancel
Save