Fixed the Emotes feature

This commit is contained in:
Zorglube 2020-10-20 20:57:05 +02:00
parent c8fa4746ce
commit 6b7e6cd083
3 changed files with 43 additions and 11 deletions

View File

@ -27,3 +27,17 @@ func RunPath() string {
} }
return filepath.Dir(ex) return filepath.Dir(ex)
} }
func Substr(input string, start int, length int) string {
asRunes := []rune(input)
if start >= len(asRunes) {
return ""
}
if start+length > len(asRunes) {
length = len(asRunes) - start
}
return string(asRunes[start : start+length])
}

View File

@ -70,7 +70,7 @@ func processEmoteDir(path string) (common.EmotesMap, error) {
} }
for _, d := range subd { for _, d := range subd {
if d.IsDir() { if d.IsDir() {
//emotes = append(emotes, findEmotes(filepath.Join(path, dir, d.Name()))...) // emotes = append(emotes, findEmotes(filepath.Join(path, dir, d.Name()))...)
p := filepath.Join(path, dir, d.Name()) p := filepath.Join(path, dir, d.Name())
em, err = findEmotes(p, em) em, err = findEmotes(p, em)
if err != nil { if err != nil {
@ -80,33 +80,50 @@ func processEmoteDir(path string) (common.EmotesMap, error) {
} }
} }
fmt.Printf("processEmoteDir: %d\n", len(em)) common.LogInfof("processEmoteDir: %d\n", len(em))
return em, nil return em, nil
} }
func findEmotes(dir string, em common.EmotesMap) (common.EmotesMap, error) { func substr(input string, start int, length int) string {
//em := NewEmotesMap() asRunes := []rune(input)
//fmt.Printf("finding emotes in %q\n", dir) if start >= len(asRunes) {
return ""
}
if start+length > len(asRunes) {
length = len(asRunes) - start
}
return string(asRunes[start : start+length])
}
func findEmotes(dir string, em common.EmotesMap) (common.EmotesMap, error) {
var runPathLength = len(common.RunPath() + "/static/")
common.LogDebugf("finding emotes in %q\n", dir)
emotePNGs, err := filepath.Glob(filepath.Join(dir, "*.png")) emotePNGs, err := filepath.Glob(filepath.Join(dir, "*.png"))
if err != nil { if err != nil {
return em, fmt.Errorf("unable to glob emote directory: %s\n", err) return em, fmt.Errorf("unable to glob emote directory: %s\n", err)
} }
fmt.Printf("%d emotePNGs\n", len(emotePNGs)) common.LogInfof("Found %d emotePNGs\n", len(emotePNGs))
emoteGIFs, err := filepath.Glob(filepath.Join(dir, "*.gif")) emoteGIFs, err := filepath.Glob(filepath.Join(dir, "*.gif"))
if err != nil { if err != nil {
return em, errors.Wrap(err, "unable to glob emote directory:") return em, errors.Wrap(err, "unable to glob emote directory:")
} }
fmt.Printf("%d emoteGIFs\n", len(emoteGIFs)) common.LogInfof("Found %d emoteGIFs\n", len(emoteGIFs))
for _, file := range emotePNGs { for _, file := range emotePNGs {
em = em.Add(file) png := strings.ReplaceAll(common.Substr(file, runPathLength, len(file)), "\\", "/")
//emotes = append(emotes, common.Emote{FullPath: dir, Code: file}) //common.LogDebugf("Emote PNG: %s", png)
em = em.Add(png)
} }
for _, file := range emoteGIFs { for _, file := range emoteGIFs {
em = em.Add(file) gif := strings.ReplaceAll(common.Substr(file, runPathLength, len(file)), "\\", "/")
//common.LogDebugf("Emote GIF: %s", gif)
em = em.Add(gif)
} }
return em, nil return em, nil
@ -122,7 +139,7 @@ func getEmotes(names []string) error {
return errors.Wrapf(err, "could not get emote data for \"%s\"", user.ID) return errors.Wrapf(err, "could not get emote data for \"%s\"", user.ID)
} }
emoteUserDir := filepath.Join(emoteDir, "twitch", user.Login) emoteUserDir := filepath.Join(common.RunPath()+emoteDir, "twitch", user.Login)
if _, err := os.Stat(emoteUserDir); os.IsNotExist(err) { if _, err := os.Stat(emoteUserDir); os.IsNotExist(err) {
os.MkdirAll(emoteUserDir, os.ModePerm) os.MkdirAll(emoteUserDir, os.ModePerm)
} }

View File

@ -283,6 +283,7 @@ func handleEmoteTemplate(w http.ResponseWriter, r *http.Request) {
Emotes: common.Emotes, Emotes: common.Emotes,
} }
common.LogDebugf("Emotes Data: %s", data)
err := common.ExecuteServerTemplate(w, "emotes", data) err := common.ExecuteServerTemplate(w, "emotes", data)
if err != nil { if err != nil {
common.LogErrorf("Error executing file, %v", err) common.LogErrorf("Error executing file, %v", err)