-
Notifications
You must be signed in to change notification settings - Fork 206
/
attachment.go
144 lines (135 loc) · 3.46 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
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package documize
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"github.com/documize/community/core/api/entity"
)
const emptyBraces = "{}"
// GetAttachmentData get the data of a file attachement.
func (c *Client) GetAttachmentData(att *entity.Attachment) error {
url := fmt.Sprintf("%s/api/public/attachments/%s/%s/%s",
c.BaseURL, att.OrgID, att.Job, att.FileID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
resp, err := c.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() // ignore error
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if isError(string(b)) {
return errors.New(trimErrors(string(b)))
}
att.Data = b
return nil
}
// DeleteAttachment removes a file attachment.
func (c *Client) DeleteAttachment(att *entity.Attachment) error {
url := fmt.Sprintf("%s/api/documents/%s/attachments/%s",
c.BaseURL, att.DocumentID, att.RefID)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
resp, err := c.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() // ignore error
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if string(b) != emptyBraces {
return errors.New(trimErrors(string(b)))
}
return nil
}
// GetAttachments gets a slice of the attachments for a document ID.
func (c *Client) GetAttachments(documentID string) (entAtts []entity.Attachment, err error) {
url := fmt.Sprintf("%s/api/documents/%s/attachments",
c.BaseURL, documentID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
resp, err := c.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close() // ignore error
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &entAtts)
if err != nil {
return nil, err
}
return entAtts, nil
}
// AddAttachment adds a new attachement to a document
func (c *Client) AddAttachment(documentID, filename string, data []byte) error {
url := fmt.Sprintf("%s/api/documents/%s/attachments",
c.BaseURL, documentID)
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
writer, err := w.CreateFormFile("attachment", filename)
if err != nil {
return err
}
n, err := writer.Write(data)
if err != nil {
return err
}
if n != len(data) {
return errors.New("incorrect length written")
}
err = w.Close()
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(buf.Bytes()))
if err != nil {
return err
}
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
req.Header.Set("Content-Type",
"multipart/form-data; boundary="+w.Boundary())
resp, err := c.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() // ignore error
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if string(b) != emptyBraces {
return errors.New(trimErrors(string(b)))
}
return nil
}