88 lines
1.8 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-07-08 13:40:13 +00:00
"strings"
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
2017-06-16 11:48:11 +00:00
var netLog = avatarLog.NewChannel("NET")
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
2017-07-08 13:40:13 +00:00
// Extension ...
func (avatar *Avatar) Extension() string {
switch avatar.Format {
case "jpg", "jpeg":
return ".jpg"
case "png":
return ".png"
case "gif":
return ".gif"
default:
return ""
}
}
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
2017-07-08 13:40:13 +00:00
response, data, networkErrs := gorequest.New().Get(url).EndBytes()
// Network errors
if len(networkErrs) > 0 {
netLog.Error(user.Nick, url, networkErrs[0])
return nil
}
2017-06-13 15:06:30 +00:00
2017-07-08 13:40:13 +00:00
// Retry HTTP only version after 5 seconds if service unavailable
if response == nil || response.StatusCode == http.StatusServiceUnavailable {
2017-06-15 15:45:59 +00:00
time.Sleep(5 * time.Second)
2017-07-08 13:40:13 +00:00
response, data, networkErrs = gorequest.New().Get(strings.Replace(url, "https://", "http://", 1)).EndBytes()
2017-06-15 15:45:59 +00:00
}
2017-07-08 13:40:13 +00:00
// Network errors on 2nd try
if len(networkErrs) > 0 {
netLog.Error(user.Nick, url, networkErrs[0])
2017-06-15 14:56:06 +00:00
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 {
2017-06-16 11:48:11 +00:00
netLog.Error(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-16 11:48:11 +00:00
netLog.Error(user.Nick, url, decodeErr)
2017-06-13 15:06:30 +00:00
return nil
}
return &Avatar{
User: user,
Image: img,
Data: data,
Format: format,
}
}