detail.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package detail
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. log "github.com/sirupsen/logrus"
  7. "pmail/dto"
  8. "pmail/dto/parsemail"
  9. "pmail/models"
  10. "pmail/mysql"
  11. "strings"
  12. )
  13. func GetEmailDetail(ctx *dto.Context, id int, markRead bool) (*models.Email, error) {
  14. // 获取邮件内容
  15. var email models.Email
  16. err := mysql.Instance.Get(&email, mysql.WithContext(ctx, "select * from email where id = ?"), id)
  17. if err != nil {
  18. log.WithContext(ctx).Errorf("SQL error:%+v", err)
  19. return nil, err
  20. }
  21. if markRead && email.IsRead == 0 {
  22. _, err = mysql.Instance.Exec(mysql.WithContext(ctx, "update email set is_read =1 where id =?"), email.Id)
  23. if err != nil {
  24. log.WithContext(ctx).Errorf("SQL error:%+v", err)
  25. }
  26. }
  27. // 将内容中的cid内容替换成url
  28. if email.Attachments != "" {
  29. var atts []parsemail.Attachment
  30. _ = json.Unmarshal([]byte(email.Attachments), &atts)
  31. for _, att := range atts {
  32. email.Html = sql.NullString{
  33. String: strings.ReplaceAll(email.Html.String, fmt.Sprintf("cid:%s", att.ContentID), fmt.Sprintf("/attachments/%d/%s", id, att.ContentID)),
  34. }
  35. }
  36. }
  37. return &email, nil
  38. }