Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 5 | "net/http" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 6 | "strings" |
| 7 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 10 | |
| 11 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | // Object is a structure that holds information related to a storage object. |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 15 | type Object struct { |
Jon Perritt | f3171c1 | 2014-09-30 17:39:31 -0500 | [diff] [blame] | 16 | Bytes int `json:"bytes" mapstructure:"bytes"` |
| 17 | ContentType string `json:"content_type" mapstructure:"content_type"` |
| 18 | Hash string `json:"hash" mapstructure:"hash"` |
| 19 | LastModified string `json:"last_modified" mapstructure:"last_modified"` |
| 20 | Name string `json:"name" mapstructure:"name"` |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 21 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 22 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 23 | // ObjectPage is a single page of objects that is returned from a call to the |
| 24 | // List function. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 25 | type ObjectPage struct { |
| 26 | pagination.MarkerPageBase |
| 27 | } |
| 28 | |
| 29 | // IsEmpty returns true if a ListResult contains no object names. |
| 30 | func (r ObjectPage) IsEmpty() (bool, error) { |
| 31 | names, err := ExtractNames(r) |
| 32 | if err != nil { |
| 33 | return true, err |
| 34 | } |
| 35 | return len(names) == 0, nil |
| 36 | } |
| 37 | |
| 38 | // LastMarker returns the last object name in a ListResult. |
| 39 | func (r ObjectPage) LastMarker() (string, error) { |
| 40 | names, err := ExtractNames(r) |
| 41 | if err != nil { |
| 42 | return "", err |
| 43 | } |
| 44 | if len(names) == 0 { |
| 45 | return "", nil |
| 46 | } |
| 47 | return names[len(names)-1], nil |
| 48 | } |
| 49 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 50 | // ExtractInfo is a function that takes a page of objects and returns their full information. |
| 51 | func ExtractInfo(page pagination.Page) ([]Object, error) { |
| 52 | untyped := page.(ObjectPage).Body.([]interface{}) |
| 53 | results := make([]Object, len(untyped)) |
| 54 | for index, each := range untyped { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 55 | object := each.(map[string]interface{}) |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 56 | err := mapstructure.Decode(object, &results[index]) |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 57 | if err != nil { |
| 58 | return results, err |
| 59 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 60 | } |
| 61 | return results, nil |
| 62 | } |
| 63 | |
| 64 | // ExtractNames is a function that takes a page of objects and returns only their names. |
| 65 | func ExtractNames(page pagination.Page) ([]string, error) { |
| 66 | casted := page.(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"): |
| 70 | parsed, err := ExtractInfo(page) |
| 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 | |
| 84 | body := string(page.(ObjectPage).Body.([]uint8)) |
| 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 | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 99 | // DownloadResult is a *http.Response that is returned from a call to the Download function. |
| 100 | type DownloadResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 101 | gophercloud.Result |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 104 | // ExtractContent is a function that takes a DownloadResult (of type *http.Response) |
| 105 | // and returns the object's content. |
| 106 | func (dr DownloadResult) ExtractContent() ([]byte, error) { |
| 107 | if dr.Err != nil { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 108 | return nil, dr.Err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 109 | } |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 110 | return dr.Body.([]byte), nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 113 | // GetResult is a *http.Response that is returned from a call to the Get function. |
| 114 | type GetResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 115 | gophercloud.Result |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 118 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 119 | // and returns the custom metadata associated with the object. |
| 120 | func (gr GetResult) ExtractMetadata() (map[string]string, error) { |
| 121 | if gr.Err != nil { |
| 122 | return nil, gr.Err |
| 123 | } |
| 124 | metadata := make(map[string]string) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame^] | 125 | for k, v := range gr.Header { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 126 | if strings.HasPrefix(k, "X-Object-Meta-") { |
| 127 | key := strings.TrimPrefix(k, "X-Object-Meta-") |
| 128 | metadata[key] = v[0] |
| 129 | } |
| 130 | } |
| 131 | return metadata, nil |
| 132 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 133 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 134 | type headerResult struct { |
| 135 | gophercloud.Result |
| 136 | } |
| 137 | |
| 138 | // Extract returns the unmodified HTTP response headers from a Create, Update, or Delete call, as |
| 139 | // well as any errors that occurred during the call. |
| 140 | func (result headerResult) Extract() (http.Header, error) { |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame^] | 141 | return result.Header, result.Err |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 144 | // CreateResult represents the result of a create operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 145 | type CreateResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 146 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 149 | // UpdateResult represents the result of an update operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 150 | type UpdateResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 151 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 154 | // DeleteResult represents the result of a delete operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 155 | type DeleteResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 156 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 159 | // CopyResult represents the result of a copy operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 160 | type CopyResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 161 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 162 | } |