forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathput_attachment_operation.go
110 lines (89 loc) · 2.71 KB
/
put_attachment_operation.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
package ravendb
import (
"bytes"
"io"
"net/http"
)
var (
_ IOperation = &PutAttachmentOperation{}
)
type PutAttachmentOperation struct {
Command *PutAttachmentCommand
_documentID string
_name string
_stream io.Reader
_contentType string
_changeVector *string
}
func NewPutAttachmentOperation(documentID string, name string, stream io.Reader, contentType string, changeVector *string) *PutAttachmentOperation {
return &PutAttachmentOperation{
_documentID: documentID,
_name: name,
_stream: stream,
_contentType: contentType,
_changeVector: changeVector,
}
}
func (o *PutAttachmentOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) {
var err error
o.Command, err = NewPutAttachmentCommand(o._documentID, o._name, o._stream, o._contentType, o._changeVector)
return o.Command, err
}
var _ RavenCommand = &PutAttachmentCommand{}
type PutAttachmentCommand struct {
RavenCommandBase
_documentID string
_name string
_stream io.Reader
_contentType string
_changeVector *string
Result *AttachmentDetails
}
// TODO: should stream be io.ReadCloser? Who owns closing the attachment
func NewPutAttachmentCommand(documentID string, name string, stream io.Reader, contentType string, changeVector *string) (*PutAttachmentCommand, error) {
if stringIsBlank(documentID) {
return nil, newIllegalArgumentError("documentId cannot be null")
}
if stringIsBlank(name) {
return nil, newIllegalArgumentError("name cannot be null")
}
cmd := &PutAttachmentCommand{
RavenCommandBase: NewRavenCommandBase(),
_documentID: documentID,
_name: name,
_stream: stream,
_contentType: contentType,
_changeVector: changeVector,
}
return cmd, nil
}
var noReader = true
func (c *PutAttachmentCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
url := node.URL + "/databases/" + node.Database + "/attachments?id=" + urlUtilsEscapeDataString(c._documentID) + "&name=" + urlUtilsEscapeDataString(c._name)
if stringIsNotEmpty(c._contentType) {
url += "&contentType=" + urlUtilsEscapeDataString(c._contentType)
}
if noReader {
var buf bytes.Buffer
_, err := io.Copy(&buf, c._stream)
if err != nil {
return nil, err
}
req, err := newHttpPut(url, buf.Bytes())
if err != nil {
return nil, err
}
req.Header.Del("Content-Type")
addChangeVectorIfNotNull(c._changeVector, req)
return req, nil
}
req, err := newHttpPutReader(url, c._stream)
if err != nil {
return nil, err
}
addChangeVectorIfNotNull(c._changeVector, req)
return req, nil
}
func (c *PutAttachmentCommand) SetResponse(response []byte, fromCache bool) error {
return jsonUnmarshal(response, &c.Result)
}