|
@@ -2,6 +2,7 @@ package mail
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
+ "eta/eta_crawler/models/email"
|
|
|
"eta/eta_crawler/utils"
|
|
|
"fmt"
|
|
|
"io"
|
|
@@ -241,6 +242,7 @@ func readEveryMsg(msg *imap.Message) (emailMessage MailMessage, ok bool, err err
|
|
|
|
|
|
// 过滤
|
|
|
|
|
|
+ logList := make([]*email.EmailListenLog, 0)
|
|
|
for {
|
|
|
p, tmpErr := mr.NextPart()
|
|
|
if tmpErr == io.EOF {
|
|
@@ -276,7 +278,7 @@ func readEveryMsg(msg *imap.Message) (emailMessage MailMessage, ok bool, err err
|
|
|
|
|
|
// 确定文件后缀
|
|
|
fileSuffix := determineFileSuffix(bodyBytes)
|
|
|
- fileName := fmt.Sprintf("%s%s.%s", utils.MtjhFilePath, cid[1:len(cid)-1], fileSuffix)
|
|
|
+ fileName := fmt.Sprintf("%s%s.%s", utils.CoalFilePath, cid[1:len(cid)-1], fileSuffix)
|
|
|
|
|
|
err = SaveToFile(bodyBytes, fileName)
|
|
|
if err != nil {
|
|
@@ -288,15 +290,28 @@ func readEveryMsg(msg *imap.Message) (emailMessage MailMessage, ok bool, err err
|
|
|
case *mail.AttachmentHeader:
|
|
|
// 这是一个附件
|
|
|
filename, _ := h.Filename()
|
|
|
+ fmt.Printf("读取到到附件: %s \n", filename)
|
|
|
+ utils.FileLog.Info("读取到附件: %s ", filename)
|
|
|
//log.Printf("得到附件: %v,content-type:%s \n", filename, p.Header.Get("Content-Type"))
|
|
|
- saveName := fmt.Sprint(msg.SeqNum, utils.MD5(filename), time.Now().Format(utils.FormatDateTimeUnSpace), time.Now().Nanosecond(), path.Ext(filename))
|
|
|
- filePath := fmt.Sprintf("%s%s%s%s", utils.MtjhFilePath, `file`, string(os.PathSeparator), saveName)
|
|
|
+ if !IsMatchExt(filename) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ filePath := fmt.Sprintf("%s%s%s%s", utils.CoalFilePath, `file`, string(os.PathSeparator), filename)
|
|
|
err = SaveToFile(bodyBytes, filePath)
|
|
|
if err != nil {
|
|
|
err = errors.New(fmt.Sprintf("保存文件时出现错误:%v \n", err))
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
+ fmt.Printf("保存到文件: %s \n", filePath)
|
|
|
+ utils.FileLog.Info("保存到文件: %s ", filePath)
|
|
|
+ logList = append(logList, &email.EmailListenLog{
|
|
|
+ Title: emailMessage.Title,
|
|
|
+ Author: emailMessage.From,
|
|
|
+ Email: emailMessage.FromAddress,
|
|
|
+ EmailMessageUid: emailMessage.Uid,
|
|
|
+ FileName: filename,
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ })
|
|
|
// 这是附件资源
|
|
|
if contentDisposition := p.Header.Get("Content-Disposition"); contentDisposition != "" {
|
|
|
if strings.HasPrefix(contentDisposition, "attachment") {
|
|
@@ -306,6 +321,7 @@ func readEveryMsg(msg *imap.Message) (emailMessage MailMessage, ok bool, err err
|
|
|
// 这是内嵌资源
|
|
|
emailMessage.Resources[cid] = filePath
|
|
|
}
|
|
|
+
|
|
|
//else {
|
|
|
// mailMessage.Attachment[filename] = filePath
|
|
|
//}
|
|
@@ -318,7 +334,10 @@ func readEveryMsg(msg *imap.Message) (emailMessage MailMessage, ok bool, err err
|
|
|
if emailMessage.Content == `` {
|
|
|
emailMessage.Content = textStr
|
|
|
}
|
|
|
-
|
|
|
+ err = email.BatchAddEmailListenLog(logList)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("邮件日志保存失败:%v \n", err)
|
|
|
+ }
|
|
|
//log.Println("一封邮件读取完毕")
|
|
|
//log.Printf("------------------------- \n\n")
|
|
|
|
|
@@ -367,3 +386,16 @@ func ContainsWholeWord(s string, word string) bool {
|
|
|
re := regexp.MustCompile(pattern)
|
|
|
return re.MatchString(s)
|
|
|
}
|
|
|
+
|
|
|
+func IsMatchExt(filename string) (ok bool) {
|
|
|
+ exts := utils.CoalEmailFileExt
|
|
|
+ extArr := strings.Split(exts, "|")
|
|
|
+ for _, ext := range extArr {
|
|
|
+ ex := strings.ToLower(path.Ext(filename))
|
|
|
+ if ext == ex {
|
|
|
+ ok = true
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|