Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 6 | "io/ioutil" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 7 | "strings" |
| 8 | ) |
| 9 | |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 10 | type Object map[string]interface{} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 11 | |
| 12 | type ListOpts struct { |
| 13 | Container string |
| 14 | Full bool |
| 15 | Params map[string]string |
| 16 | } |
| 17 | |
| 18 | type DownloadOpts struct { |
| 19 | Container string |
| 20 | Name string |
| 21 | Headers map[string]string |
| 22 | Params map[string]string |
| 23 | } |
| 24 | |
| 25 | type CreateOpts struct { |
| 26 | Container string |
| 27 | Name string |
| 28 | Content *bytes.Buffer |
| 29 | Metadata map[string]string |
| 30 | Headers map[string]string |
| 31 | Params map[string]string |
| 32 | } |
| 33 | |
| 34 | type CopyOpts struct { |
| 35 | Container string |
| 36 | Name string |
| 37 | NewContainer string |
| 38 | NewName string |
| 39 | Metadata map[string]string |
| 40 | Headers map[string]string |
| 41 | } |
| 42 | |
| 43 | type DeleteOpts struct { |
| 44 | Container string |
| 45 | Name string |
| 46 | Params map[string]string |
| 47 | } |
| 48 | |
| 49 | type GetOpts struct { |
| 50 | Container string |
| 51 | Name string |
| 52 | Headers map[string]string |
| 53 | Params map[string]string |
| 54 | } |
| 55 | |
| 56 | type UpdateOpts struct { |
| 57 | Container string |
| 58 | Name string |
| 59 | Metadata map[string]string |
| 60 | Headers map[string]string |
| 61 | } |
| 62 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 63 | // ExtractInfo is a function that takes a ListResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 64 | // and returns the objects' information. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 65 | func ExtractInfo(lr ListResult) ([]Object, error) { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 66 | var oi []Object |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 67 | defer lr.Body.Close() |
| 68 | body, err := ioutil.ReadAll(lr.Body) |
| 69 | if err != nil { |
| 70 | return oi, err |
| 71 | } |
| 72 | err = json.Unmarshal(body, &oi) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 73 | return oi, err |
| 74 | } |
| 75 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 76 | // ExtractNames is a function that takes a ListResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 77 | // and returns the objects' names. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 78 | func ExtractNames(lr ListResult) ([]string, error) { |
| 79 | var ons []string |
| 80 | defer lr.Body.Close() |
| 81 | body, err := ioutil.ReadAll(lr.Body) |
| 82 | if err != nil { |
| 83 | return ons, err |
| 84 | } |
| 85 | jr := string(body) |
| 86 | ons = strings.Split(jr, "\n") |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 87 | ons = ons[:len(ons)-1] |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 88 | return ons, nil |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 91 | // ExtractContent is a function that takes a DownloadResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 92 | // and returns the object's content. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 93 | func ExtractContent(dr DownloadResult) ([]byte, error) { |
| 94 | var body []byte |
| 95 | defer dr.Body.Close() |
| 96 | body, err := ioutil.ReadAll(dr.Body) |
| 97 | return body, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 100 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 101 | // and returns the custom metadata associated with the object. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 102 | func ExtractMetadata(gr GetResult) map[string]string { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 103 | metadata := make(map[string]string) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 104 | for k, v := range gr.Header { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 105 | if strings.HasPrefix(k, "X-Object-Meta-") { |
| 106 | key := strings.TrimPrefix(k, "X-Object-Meta-") |
| 107 | metadata[key] = v[0] |
| 108 | } |
| 109 | } |
| 110 | return metadata |
| 111 | } |