blob: 9575e0fcef8d932fcefa11287987bf8d2836f7e9 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
4 "bytes"
5 "encoding/json"
Jon Perritteb575642014-04-24 15:16:31 -05006 "io/ioutil"
Jon Perritt816d2a02014-03-11 20:49:46 -05007 "strings"
8)
9
Jon Perrittc19adea2014-04-15 16:56:01 -050010type Object map[string]interface{}
Jon Perritt816d2a02014-03-11 20:49:46 -050011
12type ListOpts struct {
13 Container string
14 Full bool
15 Params map[string]string
16}
17
18type DownloadOpts struct {
19 Container string
20 Name string
21 Headers map[string]string
22 Params map[string]string
23}
24
25type 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
34type 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
43type DeleteOpts struct {
44 Container string
45 Name string
46 Params map[string]string
47}
48
49type GetOpts struct {
50 Container string
51 Name string
52 Headers map[string]string
53 Params map[string]string
54}
55
56type UpdateOpts struct {
57 Container string
58 Name string
59 Metadata map[string]string
60 Headers map[string]string
61}
62
Jon Perritteb575642014-04-24 15:16:31 -050063// ExtractInfo is a function that takes a ListResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050064// and returns the objects' information.
Jon Perritteb575642014-04-24 15:16:31 -050065func ExtractInfo(lr ListResult) ([]Object, error) {
Jon Perritt816d2a02014-03-11 20:49:46 -050066 var oi []Object
Jon Perritteb575642014-04-24 15:16:31 -050067 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 Perritt816d2a02014-03-11 20:49:46 -050073 return oi, err
74}
75
Jon Perritteb575642014-04-24 15:16:31 -050076// ExtractNames is a function that takes a ListResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050077// and returns the objects' names.
Jon Perritteb575642014-04-24 15:16:31 -050078func 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 Perritt816d2a02014-03-11 20:49:46 -050087 ons = ons[:len(ons)-1]
Jon Perritteb575642014-04-24 15:16:31 -050088 return ons, nil
Jon Perritt816d2a02014-03-11 20:49:46 -050089}
90
Jon Perritteb575642014-04-24 15:16:31 -050091// ExtractContent is a function that takes a DownloadResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050092// and returns the object's content.
Jon Perritteb575642014-04-24 15:16:31 -050093func 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 Perritt816d2a02014-03-11 20:49:46 -050098}
99
Jon Perritteb575642014-04-24 15:16:31 -0500100// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -0500101// and returns the custom metadata associated with the object.
Jon Perritteb575642014-04-24 15:16:31 -0500102func ExtractMetadata(gr GetResult) map[string]string {
Jon Perritt816d2a02014-03-11 20:49:46 -0500103 metadata := make(map[string]string)
Jon Perritteb575642014-04-24 15:16:31 -0500104 for k, v := range gr.Header {
Jon Perritt816d2a02014-03-11 20:49:46 -0500105 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}