/
attachment_content_impl.go
224 lines (174 loc) · 6.05 KB
/
attachment_content_impl.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package internal
import (
"bytes"
"context"
"fmt"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service"
"github.com/ctreminiom/go-atlassian/service/confluence"
"io"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
)
func NewContentAttachmentService(client service.Connector) *ContentAttachmentService {
return &ContentAttachmentService{
internalClient: &internalContentAttachmentImpl{c: client},
}
}
type ContentAttachmentService struct {
internalClient confluence.ContentAttachmentConnector
}
// Gets returns the attachments for a piece of content.
//
// By default, the following objects are expanded: metadata.
//
// GET /wiki/rest/api/content/{id}/child/attachment
//
// https://docs.go-atlassian.io/confluence-cloud/content/attachments#get-attachments
func (a *ContentAttachmentService) Gets(ctx context.Context, contentID string, startAt, maxResults int, options *model.GetContentAttachmentsOptionsScheme) (*model.ContentPageScheme, *model.ResponseScheme, error) {
return a.internalClient.Gets(ctx, contentID, startAt, maxResults, options)
}
// CreateOrUpdate adds an attachment to a piece of content.
//
// If the attachment already exists for the content,
//
// then the attachment is updated (i.e. a new version of the attachment is created).
//
// PUT /wiki/rest/api/content/{id}/child/attachment
//
// https://docs.go-atlassian.io/confluence-cloud/content/attachments#create-or-update-attachment
func (a *ContentAttachmentService) CreateOrUpdate(ctx context.Context, attachmentID, status, fileName string, file io.Reader) (*model.ContentPageScheme, *model.ResponseScheme, error) {
return a.internalClient.CreateOrUpdate(ctx, attachmentID, status, fileName, file)
}
// Create adds an attachment to a piece of content.
//
// This method only adds a new attachment.
//
// If you want to update an existing attachment, use Create or update attachments.
//
// POST /wiki/rest/api/content/{id}/child/attachment
//
// https://docs.go-atlassian.io/confluence-cloud/content/attachments#create-attachment
func (a *ContentAttachmentService) Create(ctx context.Context, attachmentID, status, fileName string, file io.Reader) (*model.ContentPageScheme, *model.ResponseScheme, error) {
return a.internalClient.Create(ctx, attachmentID, status, fileName, file)
}
type internalContentAttachmentImpl struct {
c service.Connector
}
func (i *internalContentAttachmentImpl) Gets(ctx context.Context, contentID string, startAt, maxResults int, options *model.GetContentAttachmentsOptionsScheme) (*model.ContentPageScheme, *model.ResponseScheme, error) {
if contentID == "" {
return nil, nil, model.ErrNoContentIDError
}
query := url.Values{}
query.Add("start", strconv.Itoa(startAt))
query.Add("limit", strconv.Itoa(maxResults))
if options != nil {
if len(options.Expand) != 0 {
query.Add("expand", strings.Join(options.Expand, ","))
}
if options.FileName != "" {
query.Add("filename", options.FileName)
}
if options.MediaType != "" {
query.Add("mediaType", options.MediaType)
}
}
endpoint := fmt.Sprintf("wiki/rest/api/content/%v/child/attachment?%v", contentID, query.Encode())
request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil)
if err != nil {
return nil, nil, err
}
page := new(model.ContentPageScheme)
response, err := i.c.Call(request, page)
if err != nil {
return nil, response, err
}
return page, response, nil
}
func (i *internalContentAttachmentImpl) CreateOrUpdate(ctx context.Context, attachmentID, status, fileName string, file io.Reader) (*model.ContentPageScheme, *model.ResponseScheme, error) {
if attachmentID == "" {
return nil, nil, model.ErrNoContentAttachmentIDError
}
if fileName == "" {
return nil, nil, model.ErrNoContentAttachmentNameError
}
if file == nil {
return nil, nil, model.ErrNoContentReaderError
}
var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("wiki/rest/api/content/%v/child/attachment", attachmentID))
if status != "" {
query := url.Values{}
query.Add("status", status)
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}
reader := &bytes.Buffer{}
writer := multipart.NewWriter(reader)
attachment, err := writer.CreateFormFile("file", fileName)
if err != nil {
return nil, nil, err
}
_, err = io.Copy(attachment, file)
if err != nil {
return nil, nil, err
}
if err = writer.WriteField("minorEdit", "true"); err != nil {
return nil, nil, err
}
writer.Close()
request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint.String(), writer.FormDataContentType(), reader)
if err != nil {
return nil, nil, err
}
page := new(model.ContentPageScheme)
response, err := i.c.Call(request, page)
if err != nil {
return nil, response, err
}
return page, response, nil
}
func (i *internalContentAttachmentImpl) Create(ctx context.Context, attachmentID, status, fileName string, file io.Reader) (*model.ContentPageScheme, *model.ResponseScheme, error) {
if attachmentID == "" {
return nil, nil, model.ErrNoContentAttachmentIDError
}
if fileName == "" {
return nil, nil, model.ErrNoContentAttachmentNameError
}
if file == nil {
return nil, nil, model.ErrNoContentReaderError
}
var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("wiki/rest/api/content/%v/child/attachment", attachmentID))
if status != "" {
query := url.Values{}
query.Add("status", status)
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}
reader := &bytes.Buffer{}
writer := multipart.NewWriter(reader)
attachment, err := writer.CreateFormFile("file", fileName)
if err != nil {
return nil, nil, err
}
_, err = io.Copy(attachment, file)
if err != nil {
return nil, nil, err
}
if err = writer.WriteField("minorEdit", "true"); err != nil {
return nil, nil, err
}
writer.Close()
request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), writer.FormDataContentType(), reader)
if err != nil {
return nil, nil, err
}
page := new(model.ContentPageScheme)
response, err := i.c.Call(request, page)
if err != nil {
return nil, response, err
}
return page, response, nil
}