diff --git a/common/utils.go b/common/utils.go index 18e68d7..f84900c 100644 --- a/common/utils.go +++ b/common/utils.go @@ -27,3 +27,17 @@ func RunPath() string { } 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]) +} diff --git a/emotes.go b/emotes.go index 6feeee4..8f681ba 100644 --- a/emotes.go +++ b/emotes.go @@ -70,7 +70,7 @@ func processEmoteDir(path string) (common.EmotesMap, error) { } for _, d := range subd { 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()) em, err = findEmotes(p, em) 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 } -func findEmotes(dir string, em common.EmotesMap) (common.EmotesMap, error) { - //em := NewEmotesMap() +func substr(input string, start int, length int) string { + 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")) if err != nil { 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")) if err != nil { 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 { - em = em.Add(file) - //emotes = append(emotes, common.Emote{FullPath: dir, Code: file}) + png := strings.ReplaceAll(common.Substr(file, runPathLength, len(file)), "\\", "/") + //common.LogDebugf("Emote PNG: %s", png) + em = em.Add(png) } 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 @@ -122,7 +139,7 @@ func getEmotes(names []string) error { 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) { os.MkdirAll(emoteUserDir, os.ModePerm) } diff --git a/handlers.go b/handlers.go index 4815401..0a2a3a2 100644 --- a/handlers.go +++ b/handlers.go @@ -283,6 +283,7 @@ func handleEmoteTemplate(w http.ResponseWriter, r *http.Request) { Emotes: common.Emotes, } + common.LogDebugf("Emotes Data: %s", data) err := common.ExecuteServerTemplate(w, "emotes", data) if err != nil { common.LogErrorf("Error executing file, %v", err)