github.com/ravendb/ravendb-go-client@v0.0.0-20240229102137-4474ee7aa0fa/get_attachment_operation.go (about) 1 package ravendb 2 3 import ( 4 "net/http" 5 "strconv" 6 ) 7 8 var ( 9 _ IOperation = &GetAttachmentOperation{} 10 ) 11 12 type GetAttachmentOperation struct { 13 Command *GetAttachmentCommand 14 15 _documentID string 16 _name string 17 _type AttachmentType 18 _changeVector *string 19 } 20 21 func NewGetAttachmentOperation(documentID string, name string, typ AttachmentType, contentType string, changeVector *string) *GetAttachmentOperation { 22 return &GetAttachmentOperation{ 23 _documentID: documentID, 24 _name: name, 25 _type: typ, 26 _changeVector: changeVector, 27 } 28 } 29 30 func (o *GetAttachmentOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) { 31 var err error 32 o.Command, err = NewGetAttachmentCommand(o._documentID, o._name, o._type, o._changeVector) 33 return o.Command, err 34 } 35 36 var _ RavenCommand = &GetAttachmentCommand{} 37 38 type GetAttachmentCommand struct { 39 RavenCommandBase 40 41 _documentID string 42 _name string 43 _type AttachmentType 44 _changeVector *string 45 46 Result *AttachmentResult 47 } 48 49 // TODO: should stream be io.ReadCloser? Who owns closing the attachment 50 func NewGetAttachmentCommand(documentID string, name string, typ AttachmentType, changeVector *string) (*GetAttachmentCommand, error) { 51 if stringIsBlank(documentID) { 52 return nil, newIllegalArgumentError("DocumentId cannot be null or empty") 53 } 54 55 if stringIsBlank(name) { 56 return nil, newIllegalArgumentError("Name cannot be null or empty") 57 } 58 59 if typ != AttachmentDocument && changeVector == nil { 60 return nil, newIllegalArgumentError("Change vector cannot be null for attachment type " + typ) 61 } 62 63 cmd := &GetAttachmentCommand{ 64 RavenCommandBase: NewRavenCommandBase(), 65 66 _documentID: documentID, 67 _name: name, 68 _type: typ, 69 _changeVector: changeVector, 70 } 71 cmd.IsReadRequest = true 72 return cmd, nil 73 } 74 75 func (c *GetAttachmentCommand) CreateRequest(node *ServerNode) (*http.Request, error) { 76 url := node.URL + "/databases/" + node.Database + "/attachments?id=" + urlUtilsEscapeDataString(c._documentID) + "&name=" + urlUtilsEscapeDataString(c._name) 77 78 if c._type == AttachmentRevision { 79 m := map[string]interface{}{ 80 "Type": "Revision", 81 "ChangeVector": c._changeVector, 82 } 83 d, err := jsonMarshal(m) 84 if err != nil { 85 return nil, err 86 } 87 return NewHttpPost(url, d) 88 } 89 90 return newHttpGet(url) 91 } 92 93 func (c *GetAttachmentCommand) processResponse(cache *httpCache, response *http.Response, url string) (responseDisposeHandling, error) { 94 contentType := response.Header.Get("Content-Type") 95 changeVector := gttpExtensionsGetEtagHeader(response) 96 hash := response.Header.Get("Attachment-Hash") 97 size := int64(0) 98 sizeHeader := response.Header.Get("Attachment-Size") 99 if sizeHeader != "" { 100 size, _ = strconv.ParseInt(sizeHeader, 10, 64) 101 } 102 103 attachmentDetails := &AttachmentDetails{ 104 AttachmentName: AttachmentName{ 105 Name: c._name, 106 ContentType: contentType, 107 Hash: hash, 108 Size: size, 109 }, 110 ChangeVector: changeVector, 111 DocumentID: c._documentID, 112 } 113 c.Result = newAttachmentResult(response, attachmentDetails) 114 return responseDisposeHandlingManually, nil 115 }