forked from jesselang/go-confluence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachment.go
executable file
·151 lines (124 loc) · 3.94 KB
/
attachment.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package confluence
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
)
// NOTE: The `_links.download` key is not documented in the Confluence REST API Reference. However,
// it is referenced here: https://community.atlassian.com/t5/Answers-Developer-Questions/confluence-pages-get-attached-files-via-REST-API/qaq-p/529873#M67529
type Attachment struct {
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Links struct {
Download string `json:"download,omitempty"`
} `json:"_links,omitempty"`
}
type AttachmentResults struct {
ResultPagination
Results []Attachment `json:"results"`
}
func (w *Wiki) getAttachmentByFilenameEndpoint(contentID string, filename string) (*url.URL, error) {
return url.ParseRequestURI(fmt.Sprintf("%s/content/%s/child/attachment?filename=%s", w.endPoint.String(), contentID, filename))
}
func (w *Wiki) createAttachmentEndpoint(contentID string) (*url.URL, error) {
return url.ParseRequestURI(fmt.Sprintf("%s/content/%s/child/attachment", w.endPoint.String(), contentID))
}
func (w *Wiki) updateAttachmentEndpoint(contentID string, attachmentID string) (*url.URL, error) {
return url.ParseRequestURI(fmt.Sprintf("%s/content/%s/child/attachment/%s/data", w.endPoint.String(), contentID, attachmentID))
}
func (w *Wiki) GetAttachment(contentID string, filename string) (*AttachmentResults, error) {
endpoint, err := w.getAttachmentByFilenameEndpoint(contentID, filename)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := w.sendRequest(req)
if err != nil {
return nil, err
}
var results AttachmentResults
err = json.Unmarshal(res, &results)
if err != nil {
return nil, err
}
return &results, nil
}
func (w *Wiki) GetAttachmentData(contentID string, filename string) ([]byte, error) {
result, err := w.GetAttachment(contentID, filename)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", w.root.String() + result.Results[0].Links.Download, nil)
if err != nil {
return nil, err
}
res, err := w.sendRequest(req)
if err != nil {
return nil, err
}
return res, nil
}
func (w *Wiki) CreateAttachment(contentID string, path string) (*AttachmentResults, error) {
endpoint, err := w.createAttachmentEndpoint(contentID)
if err != nil {
return nil, err
}
return w.createOrUpdateAttachment(endpoint, path)
}
func (w *Wiki) UpdateAttachment(contentID string, path string, attachmentID string) (*AttachmentResults, error) {
endpoint, err := w.updateAttachmentEndpoint(contentID, attachmentID)
if err != nil {
return nil, err
}
return w.createOrUpdateAttachment(endpoint, path)
}
func (w *Wiki) createOrUpdateAttachment(endpoint *url.URL, path string) (*AttachmentResults, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", endpoint.String(), body)
req.Header.Add("Content-Type", writer.FormDataContentType())
// From https://docs.atlassian.com/ConfluenceServer/rest/latest/#api/content/{id}/child/attachment-createAttachments
// In order to protect against XSRF attacks, because this method accepts multipart/form-data,
// it has XSRF protection on it. This means you must submit a header of
// X-Atlassian-Token: nocheck with the request, otherwise it will be blocked.
req.Header.Add("X-Atlassian-Token", "nocheck")
if err != nil {
return nil, err
}
res, err := w.sendRequest(req)
if err != nil {
return nil, err
}
var results AttachmentResults
err = json.Unmarshal(res, &results)
if err != nil {
return nil, err
}
return &results, nil
}