diff --git a/fromURL.go b/fromURL.go index 5160c5e..66293ce 100644 --- a/fromURL.go +++ b/fromURL.go @@ -15,28 +15,64 @@ type GenericInfo struct { Limit *RateLimit } +var directURLPatterns = []string{ + "://i.imgur.com/", + "://i.imgur.io/", +} + +var albumURLPatterns = []string{ + "://imgur.com/a/", + "://m.imgur.com/a/", + "://imgur.io/a/", + "://m.imgur.io/a/", +} + +var galleryURLPatterns = []string{ + "://imgur.com/gallery/", + "://m.imgur.com/gallery/", + "://imgur.io/gallery/", + "://m.imgur.io/gallery/", +} + +var imageURLPatterns = []string{ + "://imgur.com/", + "://m.imgur.com/", + "://imgur.io/", + "://m.imgur.io/", +} + +func matchesSlice(url string, validFormats []string) bool { + for _, format := range validFormats { + if strings.Contains(url, format) { + return true + } + } + + return false +} + // GetInfoFromURL tries to query imgur based on information identified in the URL. // returns image/album info, status code of the request, error func (client *Client) GetInfoFromURL(url string) (*GenericInfo, int, error) { url = strings.TrimSpace(url) // https://i.imgur.com/.jpg -> image - if strings.Contains(url, "://i.imgur.com/") { + if matchesSlice(url, directURLPatterns) { return client.directImageURL(url) } // https://imgur.com/a/ -> album - if strings.Contains(url, "://imgur.com/a/") || strings.Contains(url, "://m.imgur.com/a/") { + if matchesSlice(url, albumURLPatterns) { return client.albumURL(url) } // https://imgur.com/gallery/ -> gallery album - if strings.Contains(url, "://imgur.com/gallery/") || strings.Contains(url, "://m.imgur.com/gallery/") { + if matchesSlice(url, galleryURLPatterns) { return client.galleryURL(url) } // https://imgur.com/ -> image - if strings.Contains(url, "://imgur.com/") || strings.Contains(url, "://m.imgur.com/") { + if matchesSlice(url, imageURLPatterns) { return client.imageURL(url) }