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