Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io/ioutil" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 9 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/pagination" |
| 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 | f3171c1 | 2014-09-30 17:39:31 -0500 | [diff] [blame^] | 15 | Bytes int `json:"bytes" mapstructure:"bytes"` |
| 16 | ContentType string `json:"content_type" mapstructure:"content_type"` |
| 17 | Hash string `json:"hash" mapstructure:"hash"` |
| 18 | LastModified string `json:"last_modified" mapstructure:"last_modified"` |
| 19 | Name string `json:"name" mapstructure:"name"` |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 20 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 21 | |
| 22 | // ListResult is a single page of objects that is returned from a call to the List function. |
| 23 | type ObjectPage struct { |
| 24 | pagination.MarkerPageBase |
| 25 | } |
| 26 | |
| 27 | // IsEmpty returns true if a ListResult contains no object names. |
| 28 | func (r ObjectPage) IsEmpty() (bool, error) { |
| 29 | names, err := ExtractNames(r) |
| 30 | if err != nil { |
| 31 | return true, err |
| 32 | } |
| 33 | return len(names) == 0, nil |
| 34 | } |
| 35 | |
| 36 | // LastMarker returns the last object name in a ListResult. |
| 37 | func (r ObjectPage) LastMarker() (string, error) { |
| 38 | names, err := ExtractNames(r) |
| 39 | if err != nil { |
| 40 | return "", err |
| 41 | } |
| 42 | if len(names) == 0 { |
| 43 | return "", nil |
| 44 | } |
| 45 | return names[len(names)-1], nil |
| 46 | } |
| 47 | |
| 48 | // DownloadResult is a *http.Response that is returned from a call to the Download function. |
| 49 | type DownloadResult struct { |
| 50 | Resp *http.Response |
| 51 | Err error |
| 52 | } |
| 53 | |
| 54 | // GetResult is a *http.Response that is returned from a call to the Get function. |
| 55 | type GetResult struct { |
| 56 | Resp *http.Response |
| 57 | Err error |
| 58 | } |
| 59 | |
| 60 | // ExtractInfo is a function that takes a page of objects and returns their full information. |
| 61 | func ExtractInfo(page pagination.Page) ([]Object, error) { |
| 62 | untyped := page.(ObjectPage).Body.([]interface{}) |
| 63 | results := make([]Object, len(untyped)) |
| 64 | for index, each := range untyped { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 65 | object := each.(map[string]interface{}) |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 66 | err := mapstructure.Decode(object, &results[index]) |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 67 | if err != nil { |
| 68 | return results, err |
| 69 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 70 | } |
| 71 | return results, nil |
| 72 | } |
| 73 | |
| 74 | // ExtractNames is a function that takes a page of objects and returns only their names. |
| 75 | func ExtractNames(page pagination.Page) ([]string, error) { |
| 76 | casted := page.(ObjectPage) |
| 77 | ct := casted.Header.Get("Content-Type") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 78 | switch { |
| 79 | case strings.HasPrefix(ct, "application/json"): |
| 80 | parsed, err := ExtractInfo(page) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | |
| 85 | names := make([]string, 0, len(parsed)) |
| 86 | for _, object := range parsed { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 87 | names = append(names, object.Name) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 88 | } |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 89 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 90 | return names, nil |
| 91 | case strings.HasPrefix(ct, "text/plain"): |
| 92 | names := make([]string, 0, 50) |
| 93 | |
| 94 | body := string(page.(ObjectPage).Body.([]uint8)) |
| 95 | for _, name := range strings.Split(body, "\n") { |
| 96 | if len(name) > 0 { |
| 97 | names = append(names, name) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return names, nil |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 102 | case strings.HasPrefix(ct, "text/html"): |
| 103 | return []string{}, nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 104 | default: |
| 105 | return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // ExtractContent is a function that takes a DownloadResult (of type *http.Response) |
| 110 | // and returns the object's content. |
| 111 | func (dr DownloadResult) ExtractContent() ([]byte, error) { |
| 112 | if dr.Err != nil { |
| 113 | return nil, nil |
| 114 | } |
| 115 | var body []byte |
| 116 | defer dr.Resp.Body.Close() |
| 117 | body, err := ioutil.ReadAll(dr.Resp.Body) |
| 118 | if err != nil { |
| 119 | return body, fmt.Errorf("Error trying to read DownloadResult body: %v", err) |
| 120 | } |
| 121 | return body, nil |
| 122 | } |
| 123 | |
| 124 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 125 | // and returns the custom metadata associated with the object. |
| 126 | func (gr GetResult) ExtractMetadata() (map[string]string, error) { |
| 127 | if gr.Err != nil { |
| 128 | return nil, gr.Err |
| 129 | } |
| 130 | metadata := make(map[string]string) |
| 131 | for k, v := range gr.Resp.Header { |
| 132 | if strings.HasPrefix(k, "X-Object-Meta-") { |
| 133 | key := strings.TrimPrefix(k, "X-Object-Meta-") |
| 134 | metadata[key] = v[0] |
| 135 | } |
| 136 | } |
| 137 | return metadata, nil |
| 138 | } |