Add mail attachment download support for 2dph integration.
Release Please / Release Please (push) Skipped
Release / GoReleaser (push) Skipped
Tests / Test (Go 1.25) (push) Skipped
Tests / Test (Go stable) (push) Skipped
Tests / Test (Go 1.25) (pull_request) Successful in 50s
Tests / Test (Go stable) (pull_request) Successful in 51s

This gives go-onlyoffice a cookie-backed attachment download path so 2dph can reuse the canonical OnlyOffice client instead of keeping a local mail adapter.
This commit is contained in:
2026-08-19 12:05:25 +01:00
parent a3a6ad3543
commit 1ef0670624
3 changed files with 99 additions and 1 deletions
+34
View File
@@ -7,7 +7,9 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/mail"
"net/http"
"net/url"
"strconv"
"strings"
@@ -96,6 +98,38 @@ func (c *Client) GetMailMessage(ctx context.Context, messageID string) (map[stri
return c.ResponseObject(ctx, "/api/2.0/mail/messages/"+url.PathEscape(id))
}
// DownloadMailAttachment fetches raw attachment bytes by mail attachment id via
// the mail addon's download.ashx handler. This path relies on the session
// cookie captured during authentication, so NewClient configures a cookie jar.
func (c *Client) DownloadMailAttachment(ctx context.Context, attachmentID string) ([]byte, error) {
id := strings.TrimSpace(attachmentID)
if id == "" {
return nil, fmt.Errorf("DownloadMailAttachment: attachment id is required")
}
auth, err := c.authHeader()
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL()+"/addons/mail/httphandlers/download.ashx?attachid="+url.QueryEscape(id), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", auth)
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("DownloadMailAttachment %s: %d %s", id, resp.StatusCode, truncate(string(raw), 400))
}
return raw, nil
}
// RemoveMailMessages deletes messages by id (PUT /api/2.0/mail/messages/remove).
// The API response "response" field may be a number or object; success is HTTP 2xx.
func (c *Client) RemoveMailMessages(ctx context.Context, ids ...int) (map[string]any, error) {