| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"mime/multipart" |
| 6 |
"net/http" |
| 7 |
|
| 8 |
"github.com/ProtonMail/gluon/rfc822" |
| 9 |
"github.com/gin-gonic/gin" |
| 10 |
"github.com/henrybear327/go-proton-api" |
| 11 |
) |
| 12 |
|
| 13 |
func (s *Server) handlePostMailAttachments() gin.HandlerFunc { |
| 14 |
return func(c *gin.Context) { |
| 15 |
form, err := c.MultipartForm() |
| 16 |
if err != nil { |
| 17 |
c.AbortWithStatus(http.StatusBadRequest) |
| 18 |
return |
| 19 |
} |
| 20 |
|
| 21 |
attachment, err := s.b.CreateAttachment( |
| 22 |
c.GetString("UserID"), |
| 23 |
form.Value["MessageID"][0], |
| 24 |
form.Value["Filename"][0], |
| 25 |
rfc822.MIMEType(form.Value["MIMEType"][0]), |
| 26 |
proton.Disposition(form.Value["Disposition"][0]), |
| 27 |
form.Value["ContentID"][0], |
| 28 |
mustReadFileHeader(form.File["KeyPackets"][0]), |
| 29 |
mustReadFileHeader(form.File["DataPacket"][0]), |
| 30 |
string(mustReadFileHeader(form.File["Signature"][0])), |
| 31 |
) |
| 32 |
if err != nil { |
| 33 |
_ = c.AbortWithError(http.StatusUnprocessableEntity, err) |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
c.JSON(http.StatusOK, gin.H{ |
| 38 |
"Attachment": attachment, |
| 39 |
}) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func (s *Server) handleGetMailAttachment() gin.HandlerFunc { |
| 44 |
return func(c *gin.Context) { |
| 45 |
attData, err := s.b.GetAttachment(c.Param("attachID")) |
| 46 |
if err != nil { |
| 47 |
_ = c.AbortWithError(http.StatusUnprocessableEntity, err) |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
c.Data(http.StatusOK, "application/octet-stream", attData) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func mustReadFileHeader(fh *multipart.FileHeader) []byte { |
| 56 |
f, err := fh.Open() |
| 57 |
if err != nil { |
| 58 |
panic(err) |
| 59 |
} |
| 60 |
|
| 61 |
data, err := io.ReadAll(f) |
| 62 |
if err != nil { |
| 63 |
panic(err) |
| 64 |
} |
| 65 |
|
| 66 |
return data |
| 67 |
} |