forked from supabase-community/storage-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucket.go
More file actions
173 lines (147 loc) · 4.46 KB
/
bucket.go
File metadata and controls
173 lines (147 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package storage_go
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
func (c *Client) ListBuckets() ([]Bucket, BucketResponseError) {
res, err := c.session.Get(c.clientTransport.baseUrl.String() + "/bucket")
if err != nil {
panic(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(res.Body)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
var data []Bucket
err = json.Unmarshal(body, &data)
var respError BucketResponseError
err = json.Unmarshal(body, &respError)
return data, respError
}
func (c *Client) GetBucket(id string) (Bucket, BucketResponseError) {
res, err := c.session.Get(c.clientTransport.baseUrl.String() + "/bucket/" + id)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
var data Bucket
var error_ BucketResponseError
err = json.Unmarshal(body, &data)
err = json.Unmarshal(body, &error_)
return data, error_
}
func (c *Client) CreateBucket(id string, options BucketOptions) (Bucket, BucketResponseError) {
bodyData := map[string]interface{}{
"id": id,
"name": id,
"public": options.Public,
}
// We only set the file size limit if it's not empty
if len(options.FileSizeLimit) > 0 {
bodyData["file_size_limit"] = options.FileSizeLimit
}
// We only set the allowed mime types if it's not empty
if len(options.AllowedMimeTypes) > 0 {
bodyData["allowed_mime_types"] = options.AllowedMimeTypes
}
jsonBody, _ := json.Marshal(bodyData)
res, err := c.session.Post(c.clientTransport.baseUrl.String()+"/bucket",
"application/json",
bytes.NewBuffer(jsonBody))
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
var data Bucket
var error_ BucketResponseError
err = json.Unmarshal(body, &data)
data.Public = options.Public
err = json.Unmarshal(body, &error_)
return data, error_
}
func (c *Client) UpdateBucket(id string, options BucketOptions) (MessageResponse, BucketResponseError) {
bodyData := map[string]interface{}{
"id": id,
"name": id,
"public": options.Public,
}
// We only set the file size limit if it's not empty
if len(options.FileSizeLimit) > 0 {
bodyData["file_size_limit"] = options.FileSizeLimit
}
// We only set the allowed mime types if it's not empty
if len(options.AllowedMimeTypes) > 0 {
bodyData["allowed_mime_types"] = options.AllowedMimeTypes
}
jsonBody, _ := json.Marshal(bodyData)
request, err := http.NewRequest(http.MethodPut, c.clientTransport.baseUrl.String()+"/bucket/"+id, bytes.NewBuffer(jsonBody))
res, err := c.session.Do(request)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
var data MessageResponse
var error_ BucketResponseError
err = json.Unmarshal(body, &data)
err = json.Unmarshal(body, &error_)
return data, error_
}
func (c *Client) EmptyBucket(id string) (MessageResponse, BucketResponseError) {
jsonBody, _ := json.Marshal(map[string]interface{}{})
res, err := c.session.Post(c.clientTransport.baseUrl.String()+"/bucket/"+id+"/empty", "application/json", bytes.NewBuffer(jsonBody))
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
var data MessageResponse
var error_ BucketResponseError
err = json.Unmarshal(body, &data)
err = json.Unmarshal(body, &error_)
return data, error_
}
func (c *Client) DeleteBucket(id string) (MessageResponse, BucketResponseError) {
jsonBody, _ := json.Marshal(map[string]interface{}{})
request, err := http.NewRequest(http.MethodDelete, c.clientTransport.baseUrl.String()+"/bucket/"+id, bytes.NewBuffer(jsonBody))
res, err := c.session.Do(request)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
var data MessageResponse
var error_ BucketResponseError
err = json.Unmarshal(body, &data)
err = json.Unmarshal(body, &error_)
return data, error_
}
type MessageResponse struct {
Message string `json:"message"`
}
type BucketResponseError struct {
Error string `json:"error"`
Message string `json:"message"`
StatusCode uint16 `json:"statusCode"`
}
type Bucket struct {
Id string `json:"id"`
Name string `json:"name"`
Owner string `json:"owner"`
Public bool `json:"public"`
FileSizeLimit string `json:"file_size_limit"`
AllowedMimeTypes []string `json:"allowed_mine_types"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type BucketOptions struct {
Public bool
FileSizeLimit string
AllowedMimeTypes []string
}