From c910e82a0aaa0886d06a6c6cb6e9568a6504a6a6 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 8 Jul 2017 17:31:04 +0200 Subject: [PATCH] Improved avatar generator --- jobs/avatars/FileSystem.go | 48 ++++++++++++++++++++++++++++++++++++++ jobs/avatars/avatars.go | 13 +++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 jobs/avatars/FileSystem.go diff --git a/jobs/avatars/FileSystem.go b/jobs/avatars/FileSystem.go new file mode 100644 index 00000000..b97b1363 --- /dev/null +++ b/jobs/avatars/FileSystem.go @@ -0,0 +1,48 @@ +package main + +import ( + "bytes" + "image" + "io/ioutil" + + "github.com/animenotifier/arn" +) + +var fileSystemLog = avatarLog.NewChannel("SSD") + +// FileSystem loads avatar from the local filesystem. +type FileSystem struct { + Directory string +} + +// GetAvatar returns the local image for the user. +func (source *FileSystem) GetAvatar(user *arn.User) *Avatar { + fullPath := arn.FindFileWithExtension(user.ID, source.Directory, arn.OriginalImageExtensions) + + if fullPath == "" { + fileSystemLog.Error(user.Nick, "Not found on file system") + return nil + } + + data, err := ioutil.ReadFile(fullPath) + + if err != nil { + fileSystemLog.Error(user.Nick, err) + return nil + } + + // Decode + img, format, decodeErr := image.Decode(bytes.NewReader(data)) + + if decodeErr != nil { + fileSystemLog.Error(user.Nick, decodeErr) + return nil + } + + return &Avatar{ + User: user, + Image: img, + Data: data, + Format: format, + } +} diff --git a/jobs/avatars/avatars.go b/jobs/avatars/avatars.go index c22c9377..277106e7 100644 --- a/jobs/avatars/avatars.go +++ b/jobs/avatars/avatars.go @@ -54,6 +54,9 @@ func main() { &MyAnimeList{ RequestLimiter: time.NewTicker(250 * time.Millisecond), }, + &FileSystem{ + Directory: "images/avatars/large/", + }, } // Define the avatar outputs @@ -118,7 +121,6 @@ func StartWorkers(queue chan *arn.User, work func(*arn.User)) { // Work handles a single user. func Work(user *arn.User) { - fmt.Println(user.ID, "|", user.Nick) user.AvatarExtension = "" for _, source := range avatarSources { @@ -129,6 +131,13 @@ func Work(user *arn.User) { continue } + sourceType := reflect.TypeOf(source).Elem().Name() + + // Avoid quality loss (if it's on the file system, we don't need to write it again) + if sourceType == "FileSystem" { + continue + } + for _, writer := range avatarOutputs { err := writer.SaveAvatar(avatar) @@ -137,7 +146,7 @@ func Work(user *arn.User) { } } - fmt.Println(color.GreenString("✔"), reflect.TypeOf(source).Elem().Name(), "|", user.Nick, "|", avatar) + fmt.Println(color.GreenString("✔"), sourceType, "|", user.Nick, "|", avatar) break }