File: attachments.go

package info (click to toggle)
golang-github-henrybear327-go-proton-api 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: sh: 55; makefile: 26
file content (67 lines) | stat: -rw-r--r-- 1,467 bytes parent folder | download
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
package server

import (
	"io"
	"mime/multipart"
	"net/http"

	"github.com/ProtonMail/gluon/rfc822"
	"github.com/gin-gonic/gin"
	"github.com/henrybear327/go-proton-api"
)

func (s *Server) handlePostMailAttachments() gin.HandlerFunc {
	return func(c *gin.Context) {
		form, err := c.MultipartForm()
		if err != nil {
			c.AbortWithStatus(http.StatusBadRequest)
			return
		}

		attachment, err := s.b.CreateAttachment(
			c.GetString("UserID"),
			form.Value["MessageID"][0],
			form.Value["Filename"][0],
			rfc822.MIMEType(form.Value["MIMEType"][0]),
			proton.Disposition(form.Value["Disposition"][0]),
			form.Value["ContentID"][0],
			mustReadFileHeader(form.File["KeyPackets"][0]),
			mustReadFileHeader(form.File["DataPacket"][0]),
			string(mustReadFileHeader(form.File["Signature"][0])),
		)
		if err != nil {
			_ = c.AbortWithError(http.StatusUnprocessableEntity, err)
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"Attachment": attachment,
		})
	}
}

func (s *Server) handleGetMailAttachment() gin.HandlerFunc {
	return func(c *gin.Context) {
		attData, err := s.b.GetAttachment(c.Param("attachID"))
		if err != nil {
			_ = c.AbortWithError(http.StatusUnprocessableEntity, err)
			return
		}

		c.Data(http.StatusOK, "application/octet-stream", attData)
	}
}

func mustReadFileHeader(fh *multipart.FileHeader) []byte {
	f, err := fh.Open()
	if err != nil {
		panic(err)
	}

	data, err := io.ReadAll(f)
	if err != nil {
		panic(err)
	}

	return data
}