feat: support empty fileUrl in GetUploadFileUrl()

This commit is contained in:
Yang Luo 2023-12-30 18:44:29 +08:00
parent 27adeb4620
commit 44f4de1440
2 changed files with 18 additions and 6 deletions

View File

@ -72,6 +72,10 @@ func GetTruncatedPath(provider *Provider, fullFilePath string, limit int) string
} }
func GetUploadFileUrl(provider *Provider, fullFilePath string, hasTimestamp bool) (string, string) { func GetUploadFileUrl(provider *Provider, fullFilePath string, hasTimestamp bool) (string, string) {
if provider.Domain != "" && !strings.HasPrefix(provider.Domain, "http://") && !strings.HasPrefix(provider.Domain, "https://") {
provider.Domain = fmt.Sprintf("https://%s", provider.Domain)
}
escapedPath := util.UrlJoin(provider.PathPrefix, fullFilePath) escapedPath := util.UrlJoin(provider.PathPrefix, fullFilePath)
objectKey := util.UrlJoin(util.GetUrlPath(provider.Domain), escapedPath) objectKey := util.UrlJoin(util.GetUrlPath(provider.Domain), escapedPath)
@ -79,9 +83,6 @@ func GetUploadFileUrl(provider *Provider, fullFilePath string, hasTimestamp bool
if provider.Type != "Local File System" { if provider.Type != "Local File System" {
// provider.Domain = "https://cdn.casbin.com/casdoor/" // provider.Domain = "https://cdn.casbin.com/casdoor/"
host = util.GetUrlHost(provider.Domain) host = util.GetUrlHost(provider.Domain)
if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") {
host = fmt.Sprintf("https://%s", host)
}
} else { } else {
// provider.Domain = "http://localhost:8000" or "https://door.casdoor.com" // provider.Domain = "http://localhost:8000" or "https://door.casdoor.com"
host = util.UrlJoin(provider.Domain, "/files") host = util.UrlJoin(provider.Domain, "/files")
@ -90,9 +91,12 @@ func GetUploadFileUrl(provider *Provider, fullFilePath string, hasTimestamp bool
host = util.UrlJoin(host, provider.Bucket) host = util.UrlJoin(host, provider.Bucket)
} }
fileUrl := util.UrlJoin(host, escapePath(objectKey)) fileUrl := ""
if host != "" {
fileUrl = util.UrlJoin(host, escapePath(objectKey))
}
if hasTimestamp { if fileUrl != "" && hasTimestamp {
fileUrl = fmt.Sprintf("%s?t=%s", fileUrl, util.GetCurrentUnixTime()) fileUrl = fmt.Sprintf("%s?t=%s", fileUrl, util.GetCurrentUnixTime())
} }

View File

@ -72,7 +72,15 @@ func GetUrlPath(urlString string) string {
} }
func GetUrlHost(urlString string) string { func GetUrlHost(urlString string) string {
u, _ := url.Parse(urlString) if urlString == "" {
return ""
}
u, err := url.Parse(urlString)
if err != nil {
return err.Error()
}
return fmt.Sprintf("%s://%s", u.Scheme, u.Host) return fmt.Sprintf("%s://%s", u.Scheme, u.Host)
} }