65 lines
1.4 KiB
Go
Raw Normal View History

2017-06-13 11:23:54 +00:00
package main
2017-06-13 12:47:17 +00:00
import (
2017-06-13 15:06:30 +00:00
"bytes"
"fmt"
2017-06-13 12:47:17 +00:00
"image"
2017-06-15 14:56:06 +00:00
"net/http"
2017-06-15 15:45:59 +00:00
"time"
2017-06-13 12:47:17 +00:00
"github.com/animenotifier/arn"
2017-06-13 15:06:30 +00:00
"github.com/parnurzeal/gorequest"
2017-06-13 12:47:17 +00:00
)
2017-06-13 11:23:54 +00:00
// Avatar represents a single image and the name of the format.
type Avatar struct {
2017-06-13 12:47:17 +00:00
User *arn.User
2017-06-13 11:23:54 +00:00
Image image.Image
Data []byte
Format string
}
2017-06-13 15:06:30 +00:00
// String returns a text representation of the format, width and height.
func (avatar *Avatar) String() string {
return fmt.Sprint(avatar.Format, " | ", avatar.Image.Bounds().Dx(), "x", avatar.Image.Bounds().Dy())
}
// AvatarFromURL downloads and decodes the image from an URL and creates an Avatar.
func AvatarFromURL(url string, user *arn.User) *Avatar {
// Download
response, data, networkErr := gorequest.New().Get(url).EndBytes()
2017-06-15 15:45:59 +00:00
// Retry after 5 seconds if service unavailable
if response.StatusCode == http.StatusServiceUnavailable {
time.Sleep(5 * time.Second)
response, data, networkErr = gorequest.New().Get(url).EndBytes()
}
// Network errors
2017-06-15 14:56:06 +00:00
if networkErr != nil {
avatarLog.Error("NET", user.Nick, url, networkErr)
return nil
}
2017-06-15 15:45:59 +00:00
// Bad status codes
2017-06-15 14:56:06 +00:00
if response.StatusCode != http.StatusOK {
avatarLog.Error("NET", user.Nick, url, response.StatusCode)
2017-06-13 15:06:30 +00:00
return nil
}
// Decode
img, format, decodeErr := image.Decode(bytes.NewReader(data))
if decodeErr != nil {
2017-06-15 14:56:06 +00:00
avatarLog.Error("IMG", user.Nick, url, decodeErr)
2017-06-13 15:06:30 +00:00
return nil
}
return &Avatar{
User: user,
Image: img,
Data: data,
Format: format,
}
}