Improved avatar background job

This commit is contained in:
Eduard Urbach 2017-06-13 13:23:54 +02:00
parent 4145f86e84
commit bdb8d983d2
8 changed files with 139 additions and 26 deletions

10
jobs/avatars/Avatar.go Normal file
View File

@ -0,0 +1,10 @@
package main
import "image"
// Avatar represents a single image and the name of the format.
type Avatar struct {
Image image.Image
Data []byte
Format string
}

View File

@ -0,0 +1,10 @@
package main
import (
"github.com/animenotifier/arn"
)
// AvatarSource describes a source where we can find avatar images for a user.
type AvatarSource interface {
GetAvatar(*arn.User) *Avatar
}

View File

@ -0,0 +1,6 @@
package main
// AvatarWriter represents a system that saves an avatar locally (in database or as a file, e.g.)
type AvatarWriter interface {
SaveAvatar(*Avatar) error
}

1
jobs/avatars/FSWriter.go Normal file
View File

@ -0,0 +1 @@
package main

45
jobs/avatars/Gravatar.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"bytes"
"fmt"
"image"
"github.com/animenotifier/arn"
"github.com/parnurzeal/gorequest"
gravatar "github.com/ungerik/go-gravatar"
)
// Gravatar - https://gravatar.com/
type Gravatar struct{}
// GetAvatar returns the Gravatar image for a user (if available).
func (source *Gravatar) GetAvatar(user *arn.User) *Avatar {
// If the user has no Email registered we can't get a Gravatar.
if user.Email == "" {
return nil
}
// Build URL
gravatarURL := gravatar.Url(user.Email) + "?s=" + fmt.Sprint(arn.AvatarMaxSize) + "&d=404&r=pg"
// Download
response, data, networkErr := gorequest.New().Get(gravatarURL).EndBytes()
if networkErr != nil || response.StatusCode != 200 {
return nil
}
// Decode
img, format, decodeErr := image.Decode(bytes.NewReader(data))
if decodeErr != nil {
return nil
}
return &Avatar{
Image: img,
Data: data,
Format: format,
}
}

View File

@ -1,45 +1,86 @@
package main package main
import ( import (
"fmt"
"os" "os"
"runtime" "runtime"
"time" "time"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/fatih/color"
) )
const ( const (
avatarsDirectoryOriginal = "images/avatars/original/" networkRateLimit = 100 * time.Millisecond
avatarsDirectoryWebP = "images/avatars/webp/" avatarsDirectoryOriginal = "images/avatars/large/original/"
avatarsDirectoryWebP = "images/avatars/large/webp/"
) )
var avatarSources []AvatarSource
var avatarWriters []AvatarWriter
// Main
func main() { func main() {
// Switch to main directory
os.Chdir("../../") os.Chdir("../../")
// Define the avatar sources
avatarSources = []AvatarSource{
&Gravatar{},
}
// Stream of all users
users, _ := arn.AllUsers() users, _ := arn.AllUsers()
// Worker queue
usersQueue := make(chan *arn.User) usersQueue := make(chan *arn.User)
rateLimiter := time.NewTicker(100 * time.Millisecond) StartWorkers(usersQueue, networkRateLimit, Work)
defer rateLimiter.Stop()
for w := 0; w < runtime.NumCPU(); w++ {
go func(workerID int) {
for user := range usersQueue {
<-rateLimiter.C
if downloadAvatar(user) {
makeWebPAvatar(user)
user.Avatar = "/+" + user.Nick + "/avatar"
} else {
user.Avatar = ""
}
user.Save()
}
}(w)
}
// We'll send each user to one of the worker threads
for user := range users { for user := range users {
usersQueue <- user usersQueue <- user
} }
} }
// StartWorkers creates multiple workers to handle a user each.
func StartWorkers(queue chan *arn.User, rateLimit time.Duration, work func(*arn.User)) {
rateLimiter := time.NewTicker(rateLimit)
for w := 0; w < runtime.NumCPU(); w++ {
go func() {
for user := range queue {
<-rateLimiter.C
work(user)
}
}()
}
}
// Work handles a single user.
func Work(user *arn.User) {
for _, source := range avatarSources {
avatar := source.GetAvatar(user)
if avatar == nil {
fmt.Println(color.RedString("✘"), user.Nick)
continue
}
fmt.Println(color.GreenString("✔"), user.Nick, "|", avatar.Format, avatar.Image.Bounds().Dx(), avatar.Image.Bounds().Dy())
for _, writer := range avatarWriters {
writer.SaveAvatar(avatar)
}
return
}
// if downloadAvatar(user) {
// makeWebPAvatar(user)
// user.Avatar = "/+" + user.Nick + "/avatar"
// } else {
// user.Avatar = ""
// }
// user.Save()
}

View File

@ -46,6 +46,6 @@ func avatarToWebP(in string, out string, quality float32) error {
// Small avatar // Small avatar
smallImg := resize.Resize(arn.AvatarSmallSize, 0, img, resize.Lanczos3) smallImg := resize.Resize(arn.AvatarSmallSize, 0, img, resize.Lanczos3)
saveErr = arn.SaveWebP(smallImg, strings.Replace(out, "webp/", "webp-small/", 1), quality) saveErr = arn.SaveWebP(smallImg, strings.Replace(out, "large/", "small/", 1), quality)
return saveErr return saveErr
} }

View File

@ -89,7 +89,7 @@ func main() {
} }
if ctx.CanUseWebP() { if ctx.CanUseWebP() {
return ctx.File("images/avatars/webp/" + user.ID + ".webp") return ctx.File("images/avatars/large/webp/" + user.ID + ".webp")
} }
err = errors.New("Your browser doesn't support the WebP image format") err = errors.New("Your browser doesn't support the WebP image format")
@ -106,7 +106,7 @@ func main() {
} }
if ctx.CanUseWebP() { if ctx.CanUseWebP() {
return ctx.File("images/avatars/webp-small/" + user.ID + ".webp") return ctx.File("images/avatars/small/webp/" + user.ID + ".webp")
} }
err = errors.New("Your browser doesn't support the WebP image format") err = errors.New("Your browser doesn't support the WebP image format")