File: attachment.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 (97 lines) | stat: -rw-r--r-- 2,715 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
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
package proton

import (
	"bytes"
	"context"
	"fmt"
	"io"

	"github.com/ProtonMail/gopenpgp/v2/crypto"
	"github.com/go-resty/resty/v2"
)

func (c *Client) GetAttachment(ctx context.Context, attachmentID string) ([]byte, error) {
	var buffer bytes.Buffer
	if err := c.getAttachment(ctx, attachmentID, &buffer); err != nil {
		return nil, err
	}
	return buffer.Bytes(), nil
}

func (c *Client) GetAttachmentInto(ctx context.Context, attachmentID string, reader io.ReaderFrom) error {
	return c.getAttachment(ctx, attachmentID, reader)
}

func (c *Client) UploadAttachment(ctx context.Context, addrKR *crypto.KeyRing, req CreateAttachmentReq) (Attachment, error) {
	var res struct {
		Attachment Attachment
	}

	kr, err := addrKR.FirstKey()
	if err != nil {
		return res.Attachment, fmt.Errorf("failed to get first key: %w", err)
	}

	sig, err := kr.SignDetached(crypto.NewPlainMessage(req.Body))
	if err != nil {
		return Attachment{}, fmt.Errorf("failed to sign attachment: %w", err)
	}

	enc, err := kr.EncryptAttachment(crypto.NewPlainMessage(req.Body), req.Filename)
	if err != nil {
		return Attachment{}, fmt.Errorf("failed to encrypt attachment: %w", err)
	}

	if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
		return r.SetResult(&res).
			SetMultipartFormData(map[string]string{
				"MessageID":   req.MessageID,
				"Filename":    req.Filename,
				"MIMEType":    string(req.MIMEType),
				"Disposition": string(req.Disposition),
				"ContentID":   req.ContentID,
			}).
			SetMultipartFields(
				&resty.MultipartField{
					Param:       "KeyPackets",
					FileName:    "blob",
					ContentType: "application/octet-stream",
					Reader:      bytes.NewReader(enc.KeyPacket),
				},
				&resty.MultipartField{
					Param:       "DataPacket",
					FileName:    "blob",
					ContentType: "application/octet-stream",
					Reader:      bytes.NewReader(enc.DataPacket),
				},
				&resty.MultipartField{
					Param:       "Signature",
					FileName:    "blob",
					ContentType: "application/octet-stream",
					Reader:      bytes.NewReader(sig.GetBinary()),
				},
			).
			Post("/mail/v4/attachments")
	}); err != nil {
		return Attachment{}, err
	}

	return res.Attachment, nil
}

func (c *Client) getAttachment(ctx context.Context, attachmentID string, reader io.ReaderFrom) error {
	res, err := c.doRes(ctx, func(req *resty.Request) (*resty.Response, error) {
		res, err := req.SetDoNotParseResponse(true).Get("/mail/v4/attachments/" + attachmentID)
		return parseResponse(res, err)
	})
	if err != nil {
		return fmt.Errorf("failed to request attachment: %w", err)
	}
	defer res.RawBody().Close()

	if _, err = reader.ReadFrom(res.RawBody()); err != nil {
		return err
	}

	return nil
}