Updated avatar refresh

This commit is contained in:
Eduard Urbach 2017-07-18 07:23:48 +02:00
parent 1706ef9bc4
commit 5d0e7911d8
4 changed files with 59 additions and 125 deletions

View File

@ -1,14 +1,23 @@
package main
import (
"encoding/json"
"flag"
"io"
"log"
"net/http"
)
"os"
"path"
"strings"
func refreshAvatar(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("ok"))
}
"github.com/animenotifier/arn"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/animenotifier/avatar/lib"
)
var port = "8001"
@ -18,6 +27,41 @@ func init() {
}
func main() {
http.HandleFunc("/", refreshAvatar)
// Switch to main directory
exe, err := os.Executable()
if err != nil {
panic(err)
}
root := path.Dir(exe)
os.Chdir(path.Join(root, "../../"))
// Start server
http.HandleFunc("/", onRequest)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// onRequest handles requests and refreshes the requested avatar
func onRequest(w http.ResponseWriter, req *http.Request) {
userID := strings.TrimPrefix(req.URL.Path, "/")
user, err := arn.GetUser(userID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
// Refresh
lib.RefreshAvatar(user)
// Send JSON response
buffer, err := json.Marshal(user.Avatar)
if err != nil {
io.WriteString(w, err.Error())
}
w.Write(buffer)
}

View File

@ -1,13 +1,10 @@
package main
import (
"fmt"
"os"
"path"
"reflect"
"runtime"
"sync"
"time"
_ "image/gif"
_ "image/jpeg"
@ -15,19 +12,10 @@ import (
"github.com/aerogo/log"
"github.com/animenotifier/arn"
"github.com/animenotifier/avatar"
"github.com/animenotifier/avatar/outputs"
"github.com/animenotifier/avatar/sources"
"github.com/animenotifier/avatar/lib"
"github.com/fatih/color"
)
const (
webPQuality = 80
)
var avatarSources []avatar.Source
var avatarOutputs []avatar.Output
var avatarLog = log.New()
var wg sync.WaitGroup
// Main
@ -45,51 +33,8 @@ func main() {
os.Chdir(path.Join(root, "../../"))
// Log
avatarLog.AddOutput(log.File("logs/avatar.log"))
defer avatarLog.Flush()
// Define the avatar sources
avatarSources = []avatar.Source{
&sources.Gravatar{
Rating: "pg",
RequestLimiter: time.NewTicker(100 * time.Millisecond),
},
&sources.MyAnimeList{
RequestLimiter: time.NewTicker(250 * time.Millisecond),
},
&sources.FileSystem{
Directory: "images/avatars/large/",
},
}
// Define the avatar outputs
avatarOutputs = []avatar.Output{
// Original - Large
&outputs.OriginalFile{
Directory: "images/avatars/large/",
Size: arn.AvatarMaxSize,
},
// Original - Small
&outputs.OriginalFile{
Directory: "images/avatars/small/",
Size: arn.AvatarSmallSize,
},
// WebP - Large
&outputs.WebPFile{
Directory: "images/avatars/large/",
Size: arn.AvatarMaxSize,
Quality: webPQuality,
},
// WebP - Small
&outputs.WebPFile{
Directory: "images/avatars/small/",
Size: arn.AvatarSmallSize,
Quality: webPQuality,
},
}
lib.Log.AddOutput(log.File("logs/avatar.log"))
defer lib.Log.Flush()
if InvokeShellArgs() {
return
@ -97,7 +42,7 @@ func main() {
// Worker queue
usersQueue := make(chan *arn.User, runtime.NumCPU())
StartWorkers(usersQueue, Work)
StartWorkers(usersQueue, lib.RefreshAvatar)
allUsers, _ := arn.AllUsers()
@ -123,59 +68,3 @@ func StartWorkers(queue chan *arn.User, work func(*arn.User)) {
}()
}
}
// Work handles a single user.
func Work(user *arn.User) {
user.Avatar.Extension = ""
for _, source := range avatarSources {
avatar, err := source.GetAvatar(user)
if err != nil {
avatarLog.Error(err)
continue
}
if avatar == nil {
// fmt.Println(color.RedString("✘"), reflect.TypeOf(source).Elem().Name(), user.Nick)
continue
}
// Name of source
user.Avatar.Source = reflect.TypeOf(source).Elem().Name()
// Log
fmt.Println(color.GreenString("✔"), user.Avatar.Source, "|", user.Nick, "|", avatar)
// Avoid JPG quality loss (if it's on the file system, we don't need to write it again)
if user.Avatar.Source == "FileSystem" {
user.Avatar.Extension = avatar.Extension()
break
}
for _, writer := range avatarOutputs {
err := writer.SaveAvatar(avatar)
if err != nil {
color.Red(err.Error())
}
}
break
}
// Since this a very long running job, refresh user data before saving it.
avatarExt := user.Avatar.Extension
avatarSrc := user.Avatar.Source
user, err := arn.GetUser(user.ID)
if err != nil {
avatarLog.Error("Can't refresh user info:", user.ID, user.Nick)
return
}
// Save avatar data
user.Avatar.Extension = avatarExt
user.Avatar.Source = avatarSrc
user.Save()
}

View File

@ -4,6 +4,7 @@ import (
"flag"
"github.com/animenotifier/arn"
"github.com/animenotifier/avatar/lib"
)
// Shell parameters
@ -26,7 +27,7 @@ func InvokeShellArgs() bool {
panic(err)
}
Work(user)
lib.RefreshAvatar(user)
return true
}
@ -37,7 +38,7 @@ func InvokeShellArgs() bool {
panic(err)
}
Work(user)
lib.RefreshAvatar(user)
return true
}

View File

@ -118,18 +118,18 @@ component Settings(user *arn.User)
option(value="") Automatic
option(value="Gravatar") Gravatar
option(value="URL") Link
option(value="FileSystem") Upload
//- option(value="FileSystem") Upload
if user.Settings().Avatar.Source == "Gravatar" || (user.Settings().Avatar.Source == "" && user.Avatar.Source == "Gravatar")
.profile-image-container.avatar-preview
img.profile-image(src=user.Gravatar(), alt="Gravatar")
img.profile-image.mountable(src=user.Gravatar(), alt="Gravatar")
if user.Settings().Avatar.Source == "URL"
InputText("Avatar.SourceURL", user.Settings().Avatar.SourceURL, "Link", "Post the link to the image here")
if user.Settings().Avatar.SourceURL != ""
.profile-image-container.avatar-preview
img.profile-image(src=strings.Replace(user.Settings().Avatar.SourceURL, "http://", "https://", 1), alt="Avatar preview")
img.profile-image.mountable(src=strings.Replace(user.Settings().Avatar.SourceURL, "http://", "https://", 1), alt="Avatar preview")
//- .widget.mountable(data-api="/api/settings/" + user.ID)
//- h3.widget-title