-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
160 lines (140 loc) · 3.86 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bytes"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
_ "embed"
"github.com/DusanKasan/parsemail"
"github.com/gin-gonic/gin"
"github.com/restsend/carrot"
"github.com/restsend/gormpher"
"gorm.io/gorm"
)
type Summary struct {
TotalCount int `json:"totalCount"`
TotalSize int `json:"totalSize"`
}
func RegisterHandlers(r *gin.RouterGroup, be *Backend) {
routes := r.Use(func(ctx *gin.Context) {
ctx.Set("backend", be)
ctx.Next()
})
routes.StaticFS("/raw", http.Dir(be.MailDir))
routes.POST("/summary", handleSummary)
routes.POST("/config", handleGetConfig)
routes.POST("/config/edit", handleEditConfig)
routes.GET("/render/:id", handleRender)
gormpher.RegisterObject(routes, &gormpher.WebObject{
Model: Mail{},
Name: "mail",
Editables: []string{"Opened", "OpenAt"},
Filterables: []string{"Opened", "OpenAt", "Score"},
Orderables: []string{"CreatedAt", "OpenAt", "Score"},
Searchables: []string{"Subject", "From", "To"},
GetDB: func(ctx *gin.Context, isCreate bool) *gorm.DB {
return be.db
},
AllowMethods: gormpher.GET | gormpher.DELETE | gormpher.EDIT | gormpher.QUERY,
})
}
func getBackend(c *gin.Context) *Backend {
obj, _ := c.Get("backend")
return obj.(*Backend)
}
func handleSummary(c *gin.Context) {
be := getBackend(c)
summary := Summary{}
filepath.Walk(be.MailDir, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if strings.HasSuffix(info.Name(), ".eml") {
summary.TotalCount++
}
// All file
summary.TotalSize += int(info.Size())
return nil
})
c.JSON(http.StatusOK, summary)
}
func handleGetConfig(c *gin.Context) {
be := getBackend(c)
vals := map[string]any{}
vals["WRITE_TIMEOUT"] = carrot.GetValue(be.db, key_WRITE_TIMEOUT)
vals["READ_TIMEOUT"] = carrot.GetValue(be.db, key_READ_TIMEOUT)
vals["DOMAIN"] = carrot.GetValue(be.db, key_DOMAIN)
vals["MAX_MESSAGE_BYTES"] = carrot.GetValue(be.db, key_MAX_MESSAGE_BYTES)
vals["MAX_RECIPIENTS"] = carrot.GetValue(be.db, key_MAX_RECIPIENTS)
vals["AUTH_STRICT"] = carrot.GetValue(be.db, key_AUTH_STRICT)
vals["MAIL_DIR"] = carrot.GetValue(be.db, key_MAIL_DIR)
c.JSON(http.StatusOK, vals)
}
func handleEditConfig(c *gin.Context) {
be := getBackend(c)
var vals map[string]string
err := c.BindJSON(&vals)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}
allows := map[string]bool{
"WRITE_TIMEOUT": true,
"READ_TIMEOUT": true,
"DOMAIN": true,
"MAX_MESSAGE_BYTES": true,
"MAX_RECIPIENTS": true,
"AUTH_STRICT": true,
"MAIL_DIR": true,
}
for k, v := range vals {
if _, ok := allows[k]; ok {
carrot.SetValue(be.db, k, v)
}
}
c.JSON(http.StatusOK, true)
}
func handleRender(c *gin.Context) {
be := getBackend(c)
mailid := c.Param("id")
var mail Mail
result := be.db.Take(&mail, "id", mailid)
if result.Error != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"err": result.Error.Error(),
})
return
}
name := path.Join(be.MailDir, mailid+".eml")
data, err := os.ReadFile(name)
if err != nil {
log.Println("open eml fail", name, err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"err": result.Error.Error(),
})
return
}
msg, err := parsemail.Parse(bytes.NewReader(data)) // returns Email struct and error
if err != nil {
log.Println("parse eml fail", name, err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"err": result.Error.Error(),
})
}
if msg.HTMLBody == "" {
c.Data(http.StatusOK, "text/plain", []byte(msg.TextBody))
return
}
htmlBody := msg.HTMLBody
for _, efile := range msg.EmbeddedFiles {
key := "cid:" + efile.CID
url := fmt.Sprintf("/api/raw/%s-%s", mail.ID, efile.CID)
htmlBody = strings.ReplaceAll(htmlBody, key, url)
}
c.Data(http.StatusOK, "text/html", []byte(htmlBody))
}