-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattachments.go
89 lines (72 loc) · 2.42 KB
/
attachments.go
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
package dvls
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
type EntryAttachment struct {
ID string `json:"id,omitempty"`
IDString string `json:"idString"`
EntryID string `json:"connectionID"`
EntryIDString string `json:"connectionIDString"`
Description string `json:"description"`
FileName string `json:"filename"`
IsPrivate bool `json:"isPrivate"`
Size int `json:"size"`
Title string `json:"title"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (e *EntryAttachment) UnmarshalJSON(d []byte) error {
type rawEntryAttachment EntryAttachment
raw := struct {
Data rawEntryAttachment `json:"data"`
}{}
err := json.Unmarshal(d, &raw)
if err != nil {
return err
}
*e = EntryAttachment(raw.Data)
return nil
}
const attachmentEndpoint = "/api/attachment"
func (c *Client) newAttachmentRequest(attachment EntryAttachment) (string, error) {
reqUrl, err := url.JoinPath(c.baseUri, attachmentEndpoint, "save?=&private=false&useSensitiveMode=true")
if err != nil {
return "", fmt.Errorf("failed to build attachment url. error: %w", err)
}
reqUrl, err = url.QueryUnescape(reqUrl)
if err != nil {
return "", fmt.Errorf("failed to unescape query url. error: %w", err)
}
entryJson, err := json.Marshal(attachment)
if err != nil {
return "", fmt.Errorf("failed to marshal body. error: %w", err)
}
resp, err := c.Request(reqUrl, http.MethodPost, bytes.NewBuffer(entryJson))
if err != nil {
return "", fmt.Errorf("error while submitting entry attachment request. error: %w", err)
} else if err = resp.CheckRespSaveResult(); err != nil {
return "", err
}
err = json.Unmarshal(resp.Response, &attachment)
if err != nil {
return "", fmt.Errorf("failed to unmarshal response body. error: %w", err)
}
return attachment.ID, nil
}
func (c *Client) uploadAttachment(fileBytes []byte, attachmentId string) error {
reqUrl, err := url.JoinPath(c.baseUri, attachmentEndpoint, attachmentId, "document")
if err != nil {
return fmt.Errorf("failed to build attachment url. error: %w", err)
}
contentType := http.DetectContentType(fileBytes)
resp, err := c.Request(reqUrl, http.MethodPost, bytes.NewBuffer(fileBytes), RequestOptions{ContentType: contentType})
if err != nil {
return fmt.Errorf("error while uploading entry attachment. error: %w", err)
} else if err = resp.CheckRespSaveResult(); err != nil {
return err
}
return nil
}