-
Notifications
You must be signed in to change notification settings - Fork 2
/
attachments.go
135 lines (107 loc) · 3.09 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
package main
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"os"
"path/filepath"
)
type attachment struct {
ID int `json:"id"`
Name string `json:"name"`
Extension string `json:"extension"`
UploadedTo int `json:"uploaded_to"`
}
func (client bookStackClient) UploadAttachment(pageID int, name string, path string) (*attachment, error) {
fd, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open file: %w", err)
}
defer fd.Close()
// TODO check for existing attachment by hash
fileName := filepath.Base(fd.Name())
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
filepart, err := mw.CreateFormFile("file", fd.Name())
if err != nil {
return nil, fmt.Errorf("create form file: %w", err)
}
_, err = io.Copy(filepart, fd)
if err != nil {
return nil, fmt.Errorf("copy file data: %w", err)
}
uploadedTo, err := mw.CreateFormField("uploaded_to")
if err != nil {
return nil, fmt.Errorf("create field uploaded_to: %w", err)
}
fmt.Fprintf(uploadedTo, "%d", pageID)
nameField, err := mw.CreateFormField("name")
if err != nil {
return nil, fmt.Errorf("create field name: %w", err)
}
fmt.Fprintf(nameField, "%s", fileName)
err = mw.Close()
if err != nil {
return nil, fmt.Errorf("close multipart writer: %w", err)
}
resp, err := client.R().
SetBody(&buf).
SetHeader("Content-Type", mw.FormDataContentType()).
SetResult(attachment{}).
Post("/api/attachments")
if err != nil || resp.StatusCode() > 399 {
return nil, fmt.Errorf("http POST: %v: %w", resp, err)
}
return resp.Result().(*attachment), nil
}
type image struct {
ID int `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
UploadedTo int `json:"uploaded_to"`
}
func (client bookStackClient) UploadImage(pageID int, name string, path string) (*image, error) {
fd, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open file: %w", err)
}
defer fd.Close()
// TODO check for existing attachment by hash
fileName := filepath.Base(fd.Name())
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
filepart, err := mw.CreateFormFile("image", fd.Name())
if err != nil {
return nil, fmt.Errorf("create form file: %w", err)
}
_, err = io.Copy(filepart, fd)
if err != nil {
return nil, fmt.Errorf("copy file data: %w", err)
}
uploadedTo, err := mw.CreateFormField("uploaded_to")
if err != nil {
return nil, fmt.Errorf("create field uploaded_to: %w", err)
}
fmt.Fprintf(uploadedTo, "%d", pageID)
nameField, err := mw.CreateFormField("name")
if err != nil {
return nil, fmt.Errorf("create field name: %w", err)
}
typeField, _ := mw.CreateFormField("type")
fmt.Fprintf(nameField, "%s", fileName)
fmt.Fprintf(typeField, "%s", "gallery")
err = mw.Close()
if err != nil {
return nil, fmt.Errorf("close multipart writer: %w", err)
}
resp, err := client.R().
SetBody(&buf).
SetHeader("Content-Type", mw.FormDataContentType()).
SetResult(image{}).
Post("/api/image-gallery")
if err != nil || resp.StatusCode() > 399 {
return nil, fmt.Errorf("http POST: %v: %w", resp, err)
}
return resp.Result().(*image), nil
}