1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-12-26 05:56:42 +00:00
cchat-discord/internal/urlutils/urlutils.go

75 lines
1.4 KiB
Go
Raw Normal View History

package urlutils
import (
"net/url"
"path"
"strconv"
"strings"
2020-12-20 05:44:26 +00:00
"github.com/diamondburned/arikawa/v2/discord"
)
2021-03-13 08:21:12 +00:00
// AvatarSize is the size of the source image.
const AvatarSize = 128
// AvatarURL wraps the URL with URL queries for the avatar.
func AvatarURL(URL string) string {
2021-03-13 08:21:12 +00:00
return Sized(URL, AvatarSize)
}
// Sized wraps the URL with the size query.
func Sized(URL string, size int) string {
2020-09-08 04:44:09 +00:00
if URL == "" {
return ""
}
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))
}
func Name(URL string) string {
u, err := url.Parse(URL)
if err != nil {
return URL
}
return path.Base(u.Path)
}
func ExtIs(URL string, exts []string) bool {
var ext = Ext(URL)
for _, e := range exts {
if e == ext {
return true
}
}
return false
}
// AssetURL generates the image URL from the given asset image ID.
2020-08-15 22:37:44 +00:00
func AssetURL(appID discord.AppID, imageID string) string {
if strings.HasPrefix(imageID, "spotify:") {
return "https://i.scdn.co/image/" + strings.TrimPrefix(imageID, "spotify:")
}
return "https://cdn.discordapp.com/app-assets/" + appID.String() + "/" + imageID + ".png"
}