Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 5 | "io" |
| 6 | "io/ioutil" |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame^] | 7 | "net/http" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 8 | "strings" |
| 9 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 12 | |
| 13 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // Object is a structure that holds information related to a storage object. |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 17 | type Object struct { |
Jon Perritt | f3171c1 | 2014-09-30 17:39:31 -0500 | [diff] [blame] | 18 | Bytes int `json:"bytes" mapstructure:"bytes"` |
| 19 | ContentType string `json:"content_type" mapstructure:"content_type"` |
| 20 | Hash string `json:"hash" mapstructure:"hash"` |
| 21 | LastModified string `json:"last_modified" mapstructure:"last_modified"` |
| 22 | Name string `json:"name" mapstructure:"name"` |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 23 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 24 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 25 | // ObjectPage is a single page of objects that is returned from a call to the |
| 26 | // List function. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 27 | type ObjectPage struct { |
| 28 | pagination.MarkerPageBase |
| 29 | } |
| 30 | |
| 31 | // IsEmpty returns true if a ListResult contains no object names. |
| 32 | func (r ObjectPage) IsEmpty() (bool, error) { |
| 33 | names, err := ExtractNames(r) |
| 34 | if err != nil { |
| 35 | return true, err |
| 36 | } |
| 37 | return len(names) == 0, nil |
| 38 | } |
| 39 | |
| 40 | // LastMarker returns the last object name in a ListResult. |
| 41 | func (r ObjectPage) LastMarker() (string, error) { |
| 42 | names, err := ExtractNames(r) |
| 43 | if err != nil { |
| 44 | return "", err |
| 45 | } |
| 46 | if len(names) == 0 { |
| 47 | return "", nil |
| 48 | } |
| 49 | return names[len(names)-1], nil |
| 50 | } |
| 51 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 52 | // ExtractInfo is a function that takes a page of objects and returns their full information. |
| 53 | func ExtractInfo(page pagination.Page) ([]Object, error) { |
| 54 | untyped := page.(ObjectPage).Body.([]interface{}) |
| 55 | results := make([]Object, len(untyped)) |
| 56 | for index, each := range untyped { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 57 | object := each.(map[string]interface{}) |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 58 | err := mapstructure.Decode(object, &results[index]) |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 59 | if err != nil { |
| 60 | return results, err |
| 61 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 62 | } |
| 63 | return results, nil |
| 64 | } |
| 65 | |
| 66 | // ExtractNames is a function that takes a page of objects and returns only their names. |
| 67 | func ExtractNames(page pagination.Page) ([]string, error) { |
| 68 | casted := page.(ObjectPage) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 69 | ct := casted.Header.Get("Content-Type") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 70 | switch { |
| 71 | case strings.HasPrefix(ct, "application/json"): |
| 72 | parsed, err := ExtractInfo(page) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | |
| 77 | names := make([]string, 0, len(parsed)) |
| 78 | for _, object := range parsed { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 79 | names = append(names, object.Name) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 80 | } |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 81 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 82 | return names, nil |
| 83 | case strings.HasPrefix(ct, "text/plain"): |
| 84 | names := make([]string, 0, 50) |
| 85 | |
| 86 | body := string(page.(ObjectPage).Body.([]uint8)) |
| 87 | for _, name := range strings.Split(body, "\n") { |
| 88 | if len(name) > 0 { |
| 89 | names = append(names, name) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return names, nil |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 94 | case strings.HasPrefix(ct, "text/html"): |
| 95 | return []string{}, nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 96 | default: |
| 97 | return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct) |
| 98 | } |
| 99 | } |
| 100 | |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame^] | 101 | type headerResult struct { |
Jon Perritt | 2b36fa3 | 2014-10-24 15:44:23 -0500 | [diff] [blame] | 102 | gophercloud.Result |
| 103 | } |
| 104 | |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame^] | 105 | func (hr headerResult) ExtractHeader() (http.Header, error) { |
| 106 | return hr.Header, hr.Err |
| 107 | } |
| 108 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 109 | // DownloadResult is a *http.Response that is returned from a call to the Download function. |
| 110 | type DownloadResult struct { |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame^] | 111 | headerResult |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 112 | Body io.Reader |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 115 | // ExtractContent is a function that takes a DownloadResult's io.Reader body |
| 116 | // and reads all available data into a slice of bytes. Please be aware that due |
| 117 | // the nature of io.Reader is forward-only - meaning that it can only be read |
| 118 | // once and not rewound. You can recreate a reader from the output of this |
| 119 | // function by using bytes.NewReader(downloadBytes) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 120 | func (dr DownloadResult) ExtractContent() ([]byte, error) { |
| 121 | if dr.Err != nil { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 122 | return nil, dr.Err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 123 | } |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 124 | body, err := ioutil.ReadAll(dr.Body) |
| 125 | if err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | return body, nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 131 | // GetResult is a *http.Response that is returned from a call to the Get function. |
| 132 | type GetResult struct { |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame^] | 133 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 136 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 137 | // and returns the custom metadata associated with the object. |
| 138 | func (gr GetResult) ExtractMetadata() (map[string]string, error) { |
| 139 | if gr.Err != nil { |
| 140 | return nil, gr.Err |
| 141 | } |
| 142 | metadata := make(map[string]string) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 143 | for k, v := range gr.Header { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 144 | if strings.HasPrefix(k, "X-Object-Meta-") { |
| 145 | key := strings.TrimPrefix(k, "X-Object-Meta-") |
| 146 | metadata[key] = v[0] |
| 147 | } |
| 148 | } |
| 149 | return metadata, nil |
| 150 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 151 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 152 | // CreateResult represents the result of a create operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 153 | type CreateResult struct { |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame^] | 154 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 155 | } |
| 156 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 157 | // UpdateResult represents the result of an update operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 158 | type UpdateResult struct { |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame^] | 159 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 162 | // DeleteResult represents the result of a delete operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 163 | type DeleteResult struct { |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame^] | 164 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 167 | // CopyResult represents the result of a copy operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 168 | type CopyResult struct { |
Jon Perritt | 10a7ec1 | 2014-10-27 11:29:33 -0500 | [diff] [blame^] | 169 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 170 | } |