Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add url option when get public image url #7

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
)
Expand Down Expand Up @@ -110,10 +111,49 @@ func (c *Client) CreateSignedUrl(bucketId string, filePath string, expiresIn int
return response
}

func (c *Client) GetPublicUrl(bucketId string, filePath string) SignedUrlResponse {
func (c *Client) GetPublicUrl(bucketId string, filePath string, urlOptions ...UrlOptions) SignedUrlResponse {
var response SignedUrlResponse

response.SignedURL = c.clientTransport.baseUrl.String() + "/object/public/" + bucketId + "/" + filePath
urlStr := c.clientTransport.baseUrl.String() + "/object/public/" + bucketId + "/" + filePath
signedURL, err := url.Parse(urlStr)
if err != nil {
return response
}

signedURLQuery := signedURL.Query()
var options UrlOptions
if len(urlOptions) > 0 {
options = urlOptions[0]
}

if options.Transform.Height > 0 {
signedURLQuery.Add("height", strconv.Itoa(options.Transform.Height))
}
if options.Transform.Width > 0 {
signedURLQuery.Add("width", strconv.Itoa(options.Transform.Width))
}
// Default: origin
if options.Transform.Format != "" {
signedURLQuery.Add("format", options.Transform.Format)
} else {
signedURLQuery.Add("format", "origin")
}
// Default: 80
if options.Transform.Quality > 0 {
signedURLQuery.Add("quality", strconv.Itoa(options.Transform.Quality))
} else {
signedURLQuery.Add("quality", "80")
}
if options.Transform.Resize != "" && (options.Transform.Resize == "conver" || options.Transform.Resize == "contain" || options.Transform.Resize == "fill") {
signedURLQuery.Add("resize", options.Transform.Resize)
}
// Default on server is false
if options.Download == true {
signedURLQuery.Add("download", strconv.FormatBool(options.Download))
}

signedURL.RawQuery = signedURLQuery.Encode()
response.SignedURL = signedURL.String()

return response
}
Expand Down Expand Up @@ -232,3 +272,16 @@ type ListFileRequestBody struct {
SortByOptions SortBy `json:"sortBy"`
Prefix string `json:"prefix"`
}

type TransformOptions struct {
Width int `json:"width"`
Height int `json:"height"`
Resize string `json:"resize"`
Format string `json:"format"`
Quality int `json:"quality"`
}

type UrlOptions struct {
Transform TransformOptions `json:"transform"`
Download bool `json:"download"`
}