2020-07-06 00:18:40 +00:00
|
|
|
package urlutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-07-08 08:35:30 +00:00
|
|
|
// AvatarURL wraps the URL with URL queries for the avatar.
|
|
|
|
func AvatarURL(URL string) string {
|
|
|
|
return Sized(URL, 64)
|
|
|
|
}
|
|
|
|
|
2020-07-06 00:18:40 +00:00
|
|
|
// Sized wraps the URL with the size query.
|
|
|
|
func Sized(URL string, size int) string {
|
|
|
|
u, err := url.Parse(URL)
|
|
|
|
if err != nil {
|
|
|
|
return URL
|
|
|
|
}
|
|
|
|
|
|
|
|
q := u.Query()
|
|
|
|
q.Set("size", strconv.Itoa(size))
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ext returns the lowercased file extension of the URL.
|
|
|
|
func Ext(URL string) string {
|
|
|
|
u, err := url.Parse(URL)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToLower(path.Ext(u.Path))
|
|
|
|
}
|
|
|
|
|
2020-07-08 08:35:30 +00:00
|
|
|
func Name(URL string) string {
|
|
|
|
u, err := url.Parse(URL)
|
|
|
|
if err != nil {
|
|
|
|
return URL
|
|
|
|
}
|
|
|
|
return path.Base(u.Path)
|
|
|
|
}
|
|
|
|
|
2020-07-06 00:18:40 +00:00
|
|
|
func ExtIs(URL string, exts []string) bool {
|
|
|
|
var ext = Ext(URL)
|
|
|
|
|
|
|
|
for _, e := range exts {
|
|
|
|
if e == ext {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|