-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailhandler.go
More file actions
77 lines (60 loc) · 1.46 KB
/
mailhandler.go
File metadata and controls
77 lines (60 loc) · 1.46 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
type mailHandler struct {
dropboxClient *DropboxClient
}
type MailAttachment struct {
Filename string `json:"filename"`
Name string `json:"name"`
Type string `json:"type"`
ContentID string `json:"content-id"`
}
// Returns a new instance of mailHandler
func NewMailHandler(dc *DropboxClient) *mailHandler {
return &mailHandler{
dropboxClient: dc,
}
}
func (m *mailHandler) HandleIncomingEmail(c *gin.Context) {
rawAttachments := c.PostForm("attachment-info")
var attachments map[string]MailAttachment
err := json.Unmarshal([]byte(rawAttachments), &attachments)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
erroredFiles := []string{}
for key, attachment := range attachments {
fd, _, err := c.Request.FormFile(key)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
err = m.dropboxClient.UploadDatedFile(fd, attachment.Filename)
if err != nil {
erroredFiles = append(erroredFiles, attachment.Filename)
fmt.Println(err)
}
}
if len(erroredFiles) > 0 {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Some files have not been processed",
"filesError": erroredFiles,
})
return
}
c.Status(204)
}