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