first commit

This commit is contained in:
jinnrry committed 2023-07-30 17:59:25 +08:00
commit 494940afc9
87 files changed
+9589

No files matched your search

@@ -0,0 +1,60 @@
package attachments
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"pmail/dto"
"pmail/dto/parsemail"
"pmail/models"
"pmail/mysql"
"pmail/services/auth"
)
func GetAttachments(ctx *dto.Context, emailId int, cid string) (string, []byte) {
// 获取邮件内容
var email models.Email
err := mysql.Instance.Get(&email, mysql.WithContext(ctx, "select * from email where id = ?"), emailId)
if err != nil {
log.WithContext(ctx).Errorf("SQL error:%+v", err)
return "", nil
}
// 检查权限
if !auth.HasAuth(ctx, &email) {
return "", nil
}
var atts []parsemail.Attachment
_ = json.Unmarshal([]byte(email.Attachments), &atts)
for _, att := range atts {
if att.ContentID == cid {
return att.ContentType, att.Content
}
}
return "", nil
}
func GetAttachmentsByIndex(ctx *dto.Context, emailId int, index int) (string, []byte) {
// 获取邮件内容
var email models.Email
err := mysql.Instance.Get(&email, mysql.WithContext(ctx, "select * from email where id = ?"), emailId)
if err != nil {
log.WithContext(ctx).Errorf("SQL error:%+v", err)
return "", nil
}
// 检查权限
if !auth.HasAuth(ctx, &email) {
return "", nil
}
var atts []parsemail.Attachment
_ = json.Unmarshal([]byte(email.Attachments), &atts)
if len(atts) > index {
return atts[index].Filename, atts[index].Content
}
return "", nil
}
+33
View File
@@ -0,0 +1,33 @@
package auth
import (
log "github.com/sirupsen/logrus"
"pmail/dto"
"pmail/models"
"pmail/mysql"
"strings"
)
// HasAuth 检查当前用户是否有某个邮件的auth
func HasAuth(ctx *dto.Context, email *models.Email) bool {
// 获取当前用户的auth
var auth []models.UserAuth
err := mysql.Instance.Select(&auth, mysql.WithContext(ctx, "select * from user_auth where user_id = ?"), ctx.UserInfo.ID)
if err != nil {
log.WithContext(ctx).Errorf("SQL error:%+v", err)
return false
}
var hasAuth bool
for _, userAuth := range auth {
if userAuth.EmailAccount == "*" {
hasAuth = true
break
} else if strings.Contains(email.Bcc, ctx.UserInfo.Account) || strings.Contains(email.Cc, ctx.UserInfo.Account) || strings.Contains(email.To, ctx.UserInfo.Account) {
hasAuth = true
break
}
}
return hasAuth
}
+43
View File
@@ -0,0 +1,43 @@
package detail
import (
"database/sql"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"pmail/dto"
"pmail/dto/parsemail"
"pmail/models"
"pmail/mysql"
"strings"
)
func GetEmailDetail(ctx *dto.Context, id int, markRead bool) (*models.Email, error) {
// 获取邮件内容
var email models.Email
err := mysql.Instance.Get(&email, mysql.WithContext(ctx, "select * from email where id = ?"), id)
if err != nil {
log.WithContext(ctx).Errorf("SQL error:%+v", err)
return nil, err
}
if markRead && email.IsRead == 0 {
_, err = mysql.Instance.Exec(mysql.WithContext(ctx, "update email set is_read =1 where id =?"), email.Id)
if err != nil {
log.WithContext(ctx).Errorf("SQL error:%+v", err)
}
}
// 将内容中的cid内容替换成url
if email.Attachments != "" {
var atts []parsemail.Attachment
_ = json.Unmarshal([]byte(email.Attachments), &atts)
for _, att := range atts {
email.Html = sql.NullString{
String: strings.ReplaceAll(email.Html.String, fmt.Sprintf("cid:%s", att.ContentID), fmt.Sprintf("/attachments/%d/%s", id, att.ContentID)),
}
}
}
return &email, nil
}
+60
View File
@@ -0,0 +1,60 @@
package list
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"pmail/dto"
"pmail/models"
"pmail/mysql"
)
func GetEmailList(ctx *dto.Context, tag string, keyword string, offset, limit int) (emailList []*models.Email, total int) {
querySQL, queryParams := genSQL(ctx, false, tag, keyword, offset, limit)
counterSQL, counterParams := genSQL(ctx, true, tag, keyword, offset, limit)
err := mysql.Instance.Select(&emailList, querySQL, queryParams...)
if err != nil {
log.Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
}
err = mysql.Instance.Get(&total, counterSQL, counterParams...)
if err != nil {
log.Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
}
return
}
func genSQL(ctx *dto.Context, counter bool, tag, keyword string, offset, limit int) (string, []any) {
sql := "select * from email where 1=1 "
if counter {
sql = "select count(1) from email where 1=1 "
}
sqlParams := []any{}
var tagInfo dto.SearchTag
_ = json.Unmarshal([]byte(tag), &tagInfo)
if tagInfo.Type != -1 {
sql += " and type =? "
sqlParams = append(sqlParams, tagInfo.Type)
}
if tagInfo.Status != -1 {
sql += " and status =? "
sqlParams = append(sqlParams, tagInfo.Status)
}
if keyword != "" {
sql += " and (subject like ? or text like ? )"
sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
}
sql += " limit ? offset ?"
sqlParams = append(sqlParams, limit, offset)
return sql, sqlParams
}