WebP bridge is working again

This commit is contained in:
2017-07-08 15:40:13 +02:00
parent 0a51b64e88
commit c729d9d3ba
10 changed files with 84 additions and 77 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"image"
"net/http"
"strings"
"time"
"github.com/animenotifier/arn"
@ -21,6 +22,20 @@ type Avatar struct {
Format string
}
// Extension ...
func (avatar *Avatar) Extension() string {
switch avatar.Format {
case "jpg", "jpeg":
return ".jpg"
case "png":
return ".png"
case "gif":
return ".gif"
default:
return ""
}
}
// 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())
@ -29,17 +44,23 @@ func (avatar *Avatar) String() string {
// 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()
// 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()
}
response, data, networkErrs := gorequest.New().Get(url).EndBytes()
// Network errors
if networkErr != nil {
netLog.Error(user.Nick, url, networkErr)
if len(networkErrs) > 0 {
netLog.Error(user.Nick, url, networkErrs[0])
return nil
}
// Retry HTTP only version after 5 seconds if service unavailable
if response == nil || response.StatusCode == http.StatusServiceUnavailable {
time.Sleep(5 * time.Second)
response, data, networkErrs = gorequest.New().Get(strings.Replace(url, "https://", "http://", 1)).EndBytes()
}
// Network errors on 2nd try
if len(networkErrs) > 0 {
netLog.Error(user.Nick, url, networkErrs[0])
return nil
}