Skip to content

Commit 2e9d661

Browse files
authored
Merge pull request #6 from tranhoangvuit/feat/bucket-size-limit-and-mime-types
Feat: Custom file size limit and mime types at bucket level
2 parents f26d565 + 26d8d7d commit 2e9d661

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

bucket.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,20 @@ func (c *Client) GetBucket(id string) (Bucket, BucketResponseError) {
5151
}
5252

5353
func (c *Client) CreateBucket(id string, options BucketOptions) (Bucket, BucketResponseError) {
54-
jsonBody, _ := json.Marshal(map[string]interface{}{
54+
bodyData := map[string]interface{}{
5555
"id": id,
5656
"name": id,
5757
"public": options.Public,
58-
})
58+
}
59+
// We only set the file size limit if it's not empty
60+
if len(options.FileSizeLimit) > 0 {
61+
bodyData["file_size_limit"] = options.FileSizeLimit
62+
}
63+
// We only set the allowed mime types if it's not empty
64+
if len(options.AllowedMimeTypes) > 0 {
65+
bodyData["allowed_mime_types"] = options.AllowedMimeTypes
66+
}
67+
jsonBody, _ := json.Marshal(bodyData)
5968
res, err := c.session.Post(c.clientTransport.baseUrl.String()+"/bucket",
6069
"application/json",
6170
bytes.NewBuffer(jsonBody))
@@ -74,11 +83,20 @@ func (c *Client) CreateBucket(id string, options BucketOptions) (Bucket, BucketR
7483
}
7584

7685
func (c *Client) UpdateBucket(id string, options BucketOptions) (MessageResponse, BucketResponseError) {
77-
jsonBody, _ := json.Marshal(map[string]interface{}{
86+
bodyData := map[string]interface{}{
7887
"id": id,
7988
"name": id,
8089
"public": options.Public,
81-
})
90+
}
91+
// We only set the file size limit if it's not empty
92+
if len(options.FileSizeLimit) > 0 {
93+
bodyData["file_size_limit"] = options.FileSizeLimit
94+
}
95+
// We only set the allowed mime types if it's not empty
96+
if len(options.AllowedMimeTypes) > 0 {
97+
bodyData["allowed_mime_types"] = options.AllowedMimeTypes
98+
}
99+
jsonBody, _ := json.Marshal(bodyData)
82100
request, err := http.NewRequest(http.MethodPut, c.clientTransport.baseUrl.String()+"/bucket/"+id, bytes.NewBuffer(jsonBody))
83101
res, err := c.session.Do(request)
84102
if err != nil {
@@ -138,14 +156,18 @@ type BucketResponseError struct {
138156
}
139157

140158
type Bucket struct {
141-
Id string `json:"id"`
142-
Name string `json:"name"`
143-
Owner string `json:"owner"`
144-
Public bool `json:"public"`
145-
CreatedAt string `json:"created_at"`
146-
UpdatedAt string `json:"updated_at"`
159+
Id string `json:"id"`
160+
Name string `json:"name"`
161+
Owner string `json:"owner"`
162+
Public bool `json:"public"`
163+
FileSizeLimit string `json:"file_size_limit"`
164+
AllowedMimeTypes []string `json:"allowed_mine_types"`
165+
CreatedAt string `json:"created_at"`
166+
UpdatedAt string `json:"updated_at"`
147167
}
148168

149169
type BucketOptions struct {
150-
Public bool
170+
Public bool
171+
FileSizeLimit string
172+
AllowedMimeTypes []string
151173
}

0 commit comments

Comments
 (0)