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