attachments.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package attachments
  2. import (
  3. "encoding/json"
  4. log "github.com/sirupsen/logrus"
  5. "pmail/db"
  6. "pmail/dto"
  7. "pmail/dto/parsemail"
  8. "pmail/models"
  9. "pmail/services/auth"
  10. )
  11. func GetAttachments(ctx *dto.Context, emailId int, cid string) (string, []byte) {
  12. // 获取邮件内容
  13. var email models.Email
  14. err := db.Instance.Get(&email, db.WithContext(ctx, "select * from email where id = ?"), emailId)
  15. if err != nil {
  16. log.WithContext(ctx).Errorf("SQL error:%+v", err)
  17. return "", nil
  18. }
  19. // 检查权限
  20. if !auth.HasAuth(ctx, &email) {
  21. return "", nil
  22. }
  23. var atts []parsemail.Attachment
  24. _ = json.Unmarshal([]byte(email.Attachments), &atts)
  25. for _, att := range atts {
  26. if att.ContentID == cid {
  27. return att.ContentType, att.Content
  28. }
  29. }
  30. return "", nil
  31. }
  32. func GetAttachmentsByIndex(ctx *dto.Context, emailId int, index int) (string, []byte) {
  33. // 获取邮件内容
  34. var email models.Email
  35. err := db.Instance.Get(&email, db.WithContext(ctx, "select * from email where id = ?"), emailId)
  36. if err != nil {
  37. log.WithContext(ctx).Errorf("SQL error:%+v", err)
  38. return "", nil
  39. }
  40. // 检查权限
  41. if !auth.HasAuth(ctx, &email) {
  42. return "", nil
  43. }
  44. var atts []parsemail.Attachment
  45. _ = json.Unmarshal([]byte(email.Attachments), &atts)
  46. if len(atts) > index {
  47. return atts[index].Filename, atts[index].Content
  48. }
  49. return "", nil
  50. }