github.com/ravendb/ravendb-go-client@v0.0.0-20240229102137-4474ee7aa0fa/put_attachment_operation.go (about) 1 package ravendb 2 3 import ( 4 "bytes" 5 "io" 6 "net/http" 7 ) 8 9 var ( 10 _ IOperation = &PutAttachmentOperation{} 11 ) 12 13 type PutAttachmentOperation struct { 14 Command *PutAttachmentCommand 15 16 _documentID string 17 _name string 18 _stream io.Reader 19 _contentType string 20 _changeVector *string 21 } 22 23 func NewPutAttachmentOperation(documentID string, name string, stream io.Reader, contentType string, changeVector *string) *PutAttachmentOperation { 24 return &PutAttachmentOperation{ 25 _documentID: documentID, 26 _name: name, 27 _stream: stream, 28 _contentType: contentType, 29 _changeVector: changeVector, 30 } 31 } 32 33 func (o *PutAttachmentOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) { 34 var err error 35 o.Command, err = NewPutAttachmentCommand(o._documentID, o._name, o._stream, o._contentType, o._changeVector) 36 return o.Command, err 37 } 38 39 var _ RavenCommand = &PutAttachmentCommand{} 40 41 type PutAttachmentCommand struct { 42 RavenCommandBase 43 44 _documentID string 45 _name string 46 _stream io.Reader 47 _contentType string 48 _changeVector *string 49 50 Result *AttachmentDetails 51 } 52 53 // TODO: should stream be io.ReadCloser? Who owns closing the attachment 54 func NewPutAttachmentCommand(documentID string, name string, stream io.Reader, contentType string, changeVector *string) (*PutAttachmentCommand, error) { 55 if stringIsBlank(documentID) { 56 return nil, newIllegalArgumentError("documentId cannot be null") 57 } 58 59 if stringIsBlank(name) { 60 return nil, newIllegalArgumentError("name cannot be null") 61 } 62 63 cmd := &PutAttachmentCommand{ 64 RavenCommandBase: NewRavenCommandBase(), 65 66 _documentID: documentID, 67 _name: name, 68 _stream: stream, 69 _contentType: contentType, 70 _changeVector: changeVector, 71 } 72 return cmd, nil 73 } 74 75 var noReader = true 76 77 func (c *PutAttachmentCommand) CreateRequest(node *ServerNode) (*http.Request, error) { 78 url := node.URL + "/databases/" + node.Database + "/attachments?id=" + urlUtilsEscapeDataString(c._documentID) + "&name=" + urlUtilsEscapeDataString(c._name) 79 80 if stringIsNotEmpty(c._contentType) { 81 url += "&contentType=" + urlUtilsEscapeDataString(c._contentType) 82 } 83 84 if noReader { 85 var buf bytes.Buffer 86 _, err := io.Copy(&buf, c._stream) 87 if err != nil { 88 return nil, err 89 } 90 req, err := newHttpPut(url, buf.Bytes()) 91 if err != nil { 92 return nil, err 93 } 94 req.Header.Del("Content-Type") 95 addChangeVectorIfNotNull(c._changeVector, req) 96 return req, nil 97 } 98 99 req, err := newHttpPutReader(url, c._stream) 100 if err != nil { 101 return nil, err 102 } 103 addChangeVectorIfNotNull(c._changeVector, req) 104 return req, nil 105 106 } 107 108 func (c *PutAttachmentCommand) SetResponse(response []byte, fromCache bool) error { 109 return jsonUnmarshal(response, &c.Result) 110 }