| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
|
| 9 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 10 |
"github.com/go-resty/resty/v2" |
| 11 |
) |
| 12 |
|
| 13 |
func (c *Client) GetAttachment(ctx context.Context, attachmentID string) ([]byte, error) { |
| 14 |
var buffer bytes.Buffer |
| 15 |
if err := c.getAttachment(ctx, attachmentID, &buffer); err != nil { |
| 16 |
return nil, err |
| 17 |
} |
| 18 |
return buffer.Bytes(), nil |
| 19 |
} |
| 20 |
|
| 21 |
func (c *Client) GetAttachmentInto(ctx context.Context, attachmentID string, reader io.ReaderFrom) error { |
| 22 |
return c.getAttachment(ctx, attachmentID, reader) |
| 23 |
} |
| 24 |
|
| 25 |
func (c *Client) UploadAttachment(ctx context.Context, addrKR *crypto.KeyRing, req CreateAttachmentReq) (Attachment, error) { |
| 26 |
var res struct { |
| 27 |
Attachment Attachment |
| 28 |
} |
| 29 |
|
| 30 |
kr, err := addrKR.FirstKey() |
| 31 |
if err != nil { |
| 32 |
return res.Attachment, fmt.Errorf("failed to get first key: %w", err) |
| 33 |
} |
| 34 |
|
| 35 |
sig, err := kr.SignDetached(crypto.NewPlainMessage(req.Body)) |
| 36 |
if err != nil { |
| 37 |
return Attachment{}, fmt.Errorf("failed to sign attachment: %w", err) |
| 38 |
} |
| 39 |
|
| 40 |
enc, err := kr.EncryptAttachment(crypto.NewPlainMessage(req.Body), req.Filename) |
| 41 |
if err != nil { |
| 42 |
return Attachment{}, fmt.Errorf("failed to encrypt attachment: %w", err) |
| 43 |
} |
| 44 |
|
| 45 |
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 46 |
return r.SetResult(&res). |
| 47 |
SetMultipartFormData(map[string]string{ |
| 48 |
"MessageID": req.MessageID, |
| 49 |
"Filename": req.Filename, |
| 50 |
"MIMEType": string(req.MIMEType), |
| 51 |
"Disposition": string(req.Disposition), |
| 52 |
"ContentID": req.ContentID, |
| 53 |
}). |
| 54 |
SetMultipartFields( |
| 55 |
&resty.MultipartField{ |
| 56 |
Param: "KeyPackets", |
| 57 |
FileName: "blob", |
| 58 |
ContentType: "application/octet-stream", |
| 59 |
Reader: bytes.NewReader(enc.KeyPacket), |
| 60 |
}, |
| 61 |
&resty.MultipartField{ |
| 62 |
Param: "DataPacket", |
| 63 |
FileName: "blob", |
| 64 |
ContentType: "application/octet-stream", |
| 65 |
Reader: bytes.NewReader(enc.DataPacket), |
| 66 |
}, |
| 67 |
&resty.MultipartField{ |
| 68 |
Param: "Signature", |
| 69 |
FileName: "blob", |
| 70 |
ContentType: "application/octet-stream", |
| 71 |
Reader: bytes.NewReader(sig.GetBinary()), |
| 72 |
}, |
| 73 |
). |
| 74 |
Post("/mail/v4/attachments") |
| 75 |
}); err != nil { |
| 76 |
return Attachment{}, err |
| 77 |
} |
| 78 |
|
| 79 |
return res.Attachment, nil |
| 80 |
} |
| 81 |
|
| 82 |
func (c *Client) getAttachment(ctx context.Context, attachmentID string, reader io.ReaderFrom) error { |
| 83 |
res, err := c.doRes(ctx, func(req *resty.Request) (*resty.Response, error) { |
| 84 |
res, err := req.SetDoNotParseResponse(true).Get("/mail/v4/attachments/" + attachmentID) |
| 85 |
return parseResponse(res, err) |
| 86 |
}) |
| 87 |
if err != nil { |
| 88 |
return fmt.Errorf("failed to request attachment: %w", err) |
| 89 |
} |
| 90 |
defer res.RawBody().Close() |
| 91 |
|
| 92 |
if _, err = reader.ReadFrom(res.RawBody()); err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
|
| 96 |
return nil |
| 97 |
} |