Browse Source

Fixed the Emotes feature

pull/1/head
Zorglube 2 years ago
parent
commit
6b7e6cd083
  1. 14
      common/utils.go
  2. 37
      emotes.go
  3. 1
      handlers.go

14
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])
}

37
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 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])
}
func findEmotes(dir string, em common.EmotesMap) (common.EmotesMap, error) {
//em := NewEmotesMap()
var runPathLength = len(common.RunPath() + "/static/")
//fmt.Printf("finding emotes in %q\n", dir)
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)
}

1
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)

Loading…
Cancel
Save