This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
attachments.go
175 lines (132 loc) · 4.33 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
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
package bookstack
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
type Attachment struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Extension string `json:"extension,omitempty"`
UploadedTo int `json:"uploaded_to,omitempty"`
External bool `json:"external,omitempty"`
Order int `json:"order,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
CreatedBy int `json:"created_by,omitempty"`
UpdatedBy int `json:"updated_by,omitempty"`
}
type AttachmentDetailed struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Extension string `json:"extension,omitempty"`
UploadedTo int `json:"uploaded_to,omitempty"`
External bool `json:"external,omitempty"`
Order int `json:"order,omitempty"`
CreatedBy CreatedBy `json:"created_by,omitempty"`
UpdatedBy UpdatedBy `json:"updated_by,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Links Links `json:"links,omitempty"`
Content string `json:"content,omitempty"`
}
type Links struct {
HTML string `json:"html,omitempty"`
Markdown string `json:"markdown,omitempty"`
}
type AttachmentParams struct {
Name string `json:"name,omitempty"`
UploadedTo int `json:"uploaded_to,omitempty"`
File string `json:"file,omitempty"`
Link string `json:"link,omitempty"`
}
func (a AttachmentParams) Form() (string, io.Reader, error) {
if a.File != "" {
body := bytes.NewBuffer(nil)
writer := multipart.NewWriter(body)
defer writer.Close()
if a.Name != "" {
if err := writer.WriteField("name", a.Name); err != nil {
return "", nil, err
}
}
if a.Link != "" {
if err := writer.WriteField("link", a.Link); err != nil {
return "", nil, err
}
}
if a.UploadedTo != 0 {
if err := writer.WriteField("uploaded_to", strconv.Itoa(a.UploadedTo)); err != nil {
return "", nil, err
}
}
img, err := writer.CreateFormFile("file", filepath.Base(a.File))
if err != nil {
return "", nil, err
}
f, err := os.Open(a.File)
if err != nil {
return "", nil, err
}
defer f.Close()
if _, err := io.Copy(img, f); err != nil {
return "", nil, err
}
if err := writer.Close(); err != nil {
return "", nil, err
}
return writer.FormDataContentType(), bytes.NewReader(body.Bytes()), nil
}
r, err := json.Marshal(a)
if err != nil {
return "", nil, err
}
return appJSON, bytes.NewReader(r), nil
}
// ListAttachments will return the attachments that match the given params.
func (b *Bookstack) ListAttachments(ctx context.Context, params *QueryParams) ([]Attachment, error) {
resp, err := b.request(ctx, http.MethodGet, params.String("/attachments"), blank{})
if err != nil {
return nil, err
}
return ParseMultiple[[]Attachment](resp)
}
// GetAttachment will return a single attachment that matches id.
func (b *Bookstack) GetAttachment(ctx context.Context, id int) (AttachmentDetailed, error) {
resp, err := b.request(ctx, http.MethodGet, fmt.Sprintf("/attachments/%d", id), blank{})
if err != nil {
return AttachmentDetailed{}, err
}
return ParseSingle[AttachmentDetailed](resp)
}
// CreateAttachment will create a attachment according to the given params.
func (b *Bookstack) CreateAttachment(ctx context.Context, params AttachmentParams) (Attachment, error) {
resp, err := b.request(ctx, http.MethodPost, "/attachments", params)
if err != nil {
return Attachment{}, err
}
return ParseSingle[Attachment](resp)
}
// UpdateAttachment will update a attachment with the given params.
func (b *Bookstack) UpdateAttachment(ctx context.Context, id int, params AttachmentParams) (Attachment, error) {
resp, err := b.request(ctx, http.MethodPut, fmt.Sprintf("/attachments/%d", id), params)
if err != nil {
return Attachment{}, err
}
return ParseSingle[Attachment](resp)
}
// DeleteAttachment will delete a attachment with the given id.
func (b *Bookstack) DeleteAttachment(ctx context.Context, id int) (bool, error) {
if _, err := b.request(ctx, http.MethodDelete, fmt.Sprintf("/attachments/%d", id), blank{}); err != nil {
return false, err
}
return true, nil
}