Skip to content

Commit

Permalink
Merge pull request #6 from tranhoangvuit/feat/bucket-size-limit-and-m…
Browse files Browse the repository at this point in the history
…ime-types

Feat: Custom file size limit and mime types at bucket level
  • Loading branch information
NikhilCodes authored Jun 29, 2023
2 parents f26d565 + 26d8d7d commit 2e9d661
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ func (c *Client) GetBucket(id string) (Bucket, BucketResponseError) {
}

func (c *Client) CreateBucket(id string, options BucketOptions) (Bucket, BucketResponseError) {
jsonBody, _ := json.Marshal(map[string]interface{}{
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))
Expand All @@ -74,11 +83,20 @@ func (c *Client) CreateBucket(id string, options BucketOptions) (Bucket, BucketR
}

func (c *Client) UpdateBucket(id string, options BucketOptions) (MessageResponse, BucketResponseError) {
jsonBody, _ := json.Marshal(map[string]interface{}{
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 {
Expand Down Expand Up @@ -138,14 +156,18 @@ type BucketResponseError struct {
}

type Bucket struct {
Id string `json:"id"`
Name string `json:"name"`
Owner string `json:"owner"`
Public bool `json:"public"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
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
Public bool
FileSizeLimit string
AllowedMimeTypes []string
}

0 comments on commit 2e9d661

Please sign in to comment.