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 | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 7 | "strings" |
| 8 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud" |
| 10 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | // Object is a structure that holds information related to a storage object. |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 14 | type Object struct { |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 15 | // Bytes is the total number of bytes that comprise the object. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 16 | Bytes int64 `json:"bytes"` |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 17 | |
| 18 | // ContentType is the content type of the object. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 19 | ContentType string `json:"content_type"` |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 20 | |
| 21 | // Hash represents the MD5 checksum value of the object's content. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 22 | Hash string `json:"hash"` |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 23 | |
| 24 | // LastModified is the RFC3339Milli time the object was last modified, represented |
| 25 | // as a string. For any given object (obj), this value may be parsed to a time.Time: |
| 26 | // lastModified, err := time.Parse(gophercloud.RFC3339Milli, obj.LastModified) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 27 | LastModified string `json:"last_modified"` |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 28 | |
| 29 | // Name is the unique name for the object. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 30 | Name string `json:"name"` |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 31 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 32 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 33 | // ObjectPage is a single page of objects that is returned from a call to the |
| 34 | // List function. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 35 | type ObjectPage struct { |
| 36 | pagination.MarkerPageBase |
| 37 | } |
| 38 | |
| 39 | // IsEmpty returns true if a ListResult contains no object names. |
| 40 | func (r ObjectPage) IsEmpty() (bool, error) { |
| 41 | names, err := ExtractNames(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 42 | return len(names) == 0, err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // LastMarker returns the last object name in a ListResult. |
| 46 | func (r ObjectPage) LastMarker() (string, error) { |
| 47 | names, err := ExtractNames(r) |
| 48 | if err != nil { |
| 49 | return "", err |
| 50 | } |
| 51 | if len(names) == 0 { |
| 52 | return "", nil |
| 53 | } |
| 54 | return names[len(names)-1], nil |
| 55 | } |
| 56 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 57 | // ExtractInfo is a function that takes a page of objects and returns their full information. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 58 | func ExtractInfo(r pagination.Page) ([]Object, error) { |
| 59 | var s []Object |
| 60 | err := (r.(ObjectPage)).ExtractInto(&s) |
| 61 | return s, err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // ExtractNames is a function that takes a page of objects and returns only their names. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 65 | func ExtractNames(r pagination.Page) ([]string, error) { |
| 66 | casted := r.(ObjectPage) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 67 | ct := casted.Header.Get("Content-Type") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 68 | switch { |
| 69 | case strings.HasPrefix(ct, "application/json"): |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 70 | parsed, err := ExtractInfo(r) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | |
| 75 | names := make([]string, 0, len(parsed)) |
| 76 | for _, object := range parsed { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 77 | names = append(names, object.Name) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 78 | } |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 79 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 80 | return names, nil |
| 81 | case strings.HasPrefix(ct, "text/plain"): |
| 82 | names := make([]string, 0, 50) |
| 83 | |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 84 | body := string(r.(ObjectPage).Body.([]uint8)) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 85 | for _, name := range strings.Split(body, "\n") { |
| 86 | if len(name) > 0 { |
| 87 | names = append(names, name) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return names, nil |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 92 | case strings.HasPrefix(ct, "text/html"): |
| 93 | return []string{}, nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 94 | default: |
| 95 | return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct) |
| 96 | } |
| 97 | } |
| 98 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 99 | // DownloadHeader represents the headers returned in the response from a Download request. |
| 100 | type DownloadHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 101 | AcceptRanges string `json:"Accept-Ranges"` |
| 102 | ContentDisposition string `json:"Content-Disposition"` |
| 103 | ContentEncoding string `json:"Content-Encoding"` |
| 104 | ContentLength string `json:"Content-Length"` |
| 105 | ContentType string `json:"Content-Type"` |
| 106 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 107 | DeleteAt gophercloud.JSONUnix `json:"X-Delete-At"` |
| 108 | ETag string `json:"Etag"` |
| 109 | LastModified gophercloud.JSONRFC1123 `json:"Last-Modified"` |
| 110 | ObjectManifest string `json:"X-Object-Manifest"` |
| 111 | StaticLargeObject bool `json:"X-Static-Large-Object"` |
| 112 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 115 | // DownloadResult is a *http.Response that is returned from a call to the Download function. |
| 116 | type DownloadResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 117 | gophercloud.HeaderResult |
Jamie Hannaford | ee11555 | 2014-10-27 16:11:05 +0100 | [diff] [blame] | 118 | Body io.ReadCloser |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 121 | // Extract will return a struct of headers returned from a call to Download. To obtain |
| 122 | // a map of headers, call the ExtractHeader method on the DownloadResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 123 | func (r DownloadResult) Extract() (*DownloadHeader, error) { |
| 124 | var s *DownloadHeader |
| 125 | err := r.ExtractInto(&s) |
| 126 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 129 | // ExtractContent is a function that takes a DownloadResult's io.Reader body |
| 130 | // and reads all available data into a slice of bytes. Please be aware that due |
| 131 | // the nature of io.Reader is forward-only - meaning that it can only be read |
| 132 | // once and not rewound. You can recreate a reader from the output of this |
| 133 | // function by using bytes.NewReader(downloadBytes) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 134 | func (r *DownloadResult) ExtractContent() ([]byte, error) { |
| 135 | if r.Err != nil { |
| 136 | return nil, r.Err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 137 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 138 | defer r.Body.Close() |
| 139 | body, err := ioutil.ReadAll(r.Body) |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 140 | if err != nil { |
| 141 | return nil, err |
| 142 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 143 | r.Body.Close() |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 144 | return body, nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 147 | // GetHeader represents the headers returned in the response from a Get request. |
| 148 | type GetHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 149 | ContentDisposition string `json:"Content-Disposition"` |
| 150 | ContentEncoding string `json:"Content-Encoding"` |
| 151 | ContentLength string `json:"Content-Length"` |
| 152 | ContentType string `json:"Content-Type"` |
| 153 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 154 | DeleteAt gophercloud.JSONUnix `json:"X-Delete-At"` |
| 155 | ETag string `json:"Etag"` |
| 156 | LastModified gophercloud.JSONRFC1123 `json:"Last-Modified"` |
| 157 | ObjectManifest string `json:"X-Object-Manifest"` |
| 158 | StaticLargeObject bool `json:"X-Static-Large-Object"` |
| 159 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 162 | // GetResult is a *http.Response that is returned from a call to the Get function. |
| 163 | type GetResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 164 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 167 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 168 | // a map of headers, call the ExtractHeader method on the GetResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 169 | func (r GetResult) Extract() (*GetHeader, error) { |
| 170 | var s *GetHeader |
| 171 | err := r.ExtractInto(&s) |
| 172 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 175 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 176 | // and returns the custom metadata associated with the object. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 177 | func (r GetResult) ExtractMetadata() (map[string]string, error) { |
| 178 | if r.Err != nil { |
| 179 | return nil, r.Err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 180 | } |
| 181 | metadata := make(map[string]string) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 182 | for k, v := range r.Header { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 183 | if strings.HasPrefix(k, "X-Object-Meta-") { |
| 184 | key := strings.TrimPrefix(k, "X-Object-Meta-") |
| 185 | metadata[key] = v[0] |
| 186 | } |
| 187 | } |
| 188 | return metadata, nil |
| 189 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 190 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 191 | // CreateHeader represents the headers returned in the response from a Create request. |
| 192 | type CreateHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 193 | ContentLength string `json:"Content-Length"` |
| 194 | ContentType string `json:"Content-Type"` |
| 195 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 196 | ETag string `json:"Etag"` |
| 197 | LastModified gophercloud.JSONRFC1123 `json:"Last-Modified"` |
| 198 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 201 | // CreateResult represents the result of a create operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 202 | type CreateResult struct { |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 203 | checksum string |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 204 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 207 | // Extract will return a struct of headers returned from a call to Create. To obtain |
| 208 | // a map of headers, call the ExtractHeader method on the CreateResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 209 | func (r CreateResult) Extract() (*CreateHeader, error) { |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 210 | //if r.Header.Get("ETag") != fmt.Sprintf("%x", localChecksum) { |
| 211 | // return nil, ErrWrongChecksum{} |
| 212 | //} |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 213 | var s *CreateHeader |
| 214 | err := r.ExtractInto(&s) |
| 215 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // UpdateHeader represents the headers returned in the response from a Update request. |
| 219 | type UpdateHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 220 | ContentLength string `json:"Content-Length"` |
| 221 | ContentType string `json:"Content-Type"` |
| 222 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 223 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 226 | // UpdateResult represents the result of an update operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 227 | type UpdateResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 228 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 229 | } |
| 230 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 231 | // Extract will return a struct of headers returned from a call to Update. To obtain |
| 232 | // a map of headers, call the ExtractHeader method on the UpdateResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 233 | func (r UpdateResult) Extract() (*UpdateHeader, error) { |
| 234 | var s *UpdateHeader |
| 235 | err := r.ExtractInto(&s) |
| 236 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // DeleteHeader represents the headers returned in the response from a Delete request. |
| 240 | type DeleteHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 241 | ContentLength string `json:"Content-Length"` |
| 242 | ContentType string `json:"Content-Type"` |
| 243 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 244 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 247 | // DeleteResult represents the result of a delete operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 248 | type DeleteResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 249 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 252 | // Extract will return a struct of headers returned from a call to Delete. To obtain |
| 253 | // a map of headers, call the ExtractHeader method on the DeleteResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 254 | func (r DeleteResult) Extract() (*DeleteHeader, error) { |
| 255 | var s *DeleteHeader |
| 256 | err := r.ExtractInto(&s) |
| 257 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // CopyHeader represents the headers returned in the response from a Copy request. |
| 261 | type CopyHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 262 | ContentLength int64 `json:"Content-Length"` |
| 263 | ContentType string `json:"Content-Type"` |
| 264 | CopiedFrom string `json:"X-Copied-From"` |
| 265 | CopiedFromLastModified gophercloud.JSONRFC1123 `json:"X-Copied-From-Last-Modified"` |
| 266 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 267 | ETag string `json:"Etag"` |
| 268 | LastModified gophercloud.JSONRFC1123 `json:"Last-Modified"` |
| 269 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 272 | // CopyResult represents the result of a copy operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 273 | type CopyResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 274 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 275 | } |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 276 | |
| 277 | // Extract will return a struct of headers returned from a call to Copy. To obtain |
| 278 | // a map of headers, call the ExtractHeader method on the CopyResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 279 | func (r CopyResult) Extract() (*CopyHeader, error) { |
| 280 | var s *CopyHeader |
| 281 | err := r.ExtractInto(&s) |
| 282 | return s, err |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 283 | } |