Skip to content

Commit

Permalink
Fixing recursive emotes and making it better
Browse files Browse the repository at this point in the history
I wonder if zorch will see this... I HAVE THE POWER....for now I guess
  • Loading branch information
joeyak committed Dec 3, 2022
1 parent 95231ef commit 6bb3df7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 43 deletions.
49 changes: 11 additions & 38 deletions emotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package main

import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"

"github.com/zorchenhimer/MovieNight/common"
"github.com/zorchenhimer/MovieNight/files"
"golang.org/x/exp/slices"
)

func loadEmotes() error {
Expand Down Expand Up @@ -38,48 +40,19 @@ func processEmoteDir(dir string) (common.EmotesMap, error) {
}
}

// Find top level emotes
em, err = findEmotes(dir, em)
if err != nil {
return nil, fmt.Errorf("could not find emotes in top level directory: %w", err)
}

// Get second level subdirs (eg, "twitch", "zorchenhimer", etc)
for _, subDir := range subDirs {
subd, err := os.ReadDir(path.Join(dir, subDir))
if err != nil {
common.LogErrorf("Error reading dir %q: %v\n", subd, err)
continue
filepath.WalkDir(dir, func(fpath string, d fs.DirEntry, err error) error {
if d.IsDir() || err != nil {
return nil
}
for _, d := range subd {
if d.IsDir() {
p := path.Join(dir, subDir, d.Name())
em, err = findEmotes(p, em)
if err != nil {
common.LogErrorf("Error finding emotes in %q: %v\n", p, err)
}
}
}
}

common.LogInfof("processEmoteDir: %d\n", len(em))
return em, nil
}

func findEmotes(dir string, em common.EmotesMap) (common.EmotesMap, error) {
common.LogDebugf("finding emotes in %q\n", dir)

for _, ext := range []string{"*.png", "*.gif"} {
files, err := filepath.Glob(path.Join(dir, ext))
if err != nil {
return nil, fmt.Errorf("unable to glob emote directory with %q: %w", ext, err)
if slices.Contains([]string{".png", ".gif"}, filepath.Ext(fpath)) {
em = em.Add(path.Join("emotes", strings.TrimPrefix(filepath.ToSlash(fpath), dir)))
}
common.LogInfof("Found %d %s emotes\n", len(files), ext)

for _, file := range files {
em = em.Add(path.Join("emotes", strings.TrimPrefix(filepath.ToSlash(file), dir)))
}
}
return nil
})

common.LogInfof("Found %d emotes in %s\n", len(em), dir)

return em, nil
}
18 changes: 15 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
module github.com/zorchenhimer/MovieNight

go 1.16
go 1.18

require (
github.com/alexflint/go-arg v1.4.3
github.com/gorilla/sessions v1.2.1
github.com/gorilla/websocket v1.4.2
github.com/gorilla/websocket v1.5.0
github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369
github.com/playwright-community/playwright-go v0.2000.1 // indirect
github.com/playwright-community/playwright-go v0.2000.1
github.com/stretchr/testify v1.7.1
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9
)

require (
github.com/alexflint/go-scalar v1.2.0 // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/u
github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA=
github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM=
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -15,6 +17,8 @@ github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7Fsg
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/h2non/filetype v1.1.1/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369 h1:Yp0zFEufLz0H7jzffb4UPXijavlyqlYeOg7dcyVUNnQ=
github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369/go.mod h1:aFJ1ZwLjvHN4yEzE5Bkz8rD8/d8Vlj3UIuvz2yfET7I=
Expand All @@ -27,6 +31,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 h1:yZNXmy+j/JpX19vZkVktWqAo7Gny4PBWYYK3zskGpx4=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI=
Expand Down
50 changes: 48 additions & 2 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package main

import (
"encoding/json"
"errors"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"sync"

Expand Down Expand Up @@ -42,7 +46,49 @@ func (w writeFlusher) Flush() error {
}

func wsEmotes(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, strings.TrimPrefix(r.URL.Path, "/"))
file := strings.TrimPrefix(r.URL.Path, "/")

body, err := os.ReadFile(file)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
common.LogErrorf("Could not read emote file %s: %v\n", err)
w.WriteHeader(http.StatusNotFound)
return
}

err = filepath.WalkDir(filepath.Dir(file), func(path string, d fs.DirEntry, err error) error {
if d.IsDir() || err != nil || len(body) > 0 {
return nil
}

if filepath.Base(path) != filepath.Base(file) {
return nil
}

body, err = os.ReadFile(path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
})
if err != nil {
common.LogErrorf("Subdir emote could not be read %s: %v\n", file, err)
w.WriteHeader(http.StatusNotFound)
return
}
}

if len(body) == 0 {
common.LogErrorf("Found emote file but pulled no data: %v\n", err)
w.WriteHeader(http.StatusNotFound)
return
}

_, err = w.Write(body)
if err != nil {
common.LogErrorf("Could not write emote %s to response: %v\n", file, err)
w.WriteHeader(http.StatusNotFound)
}
}

// Handling the websocket
Expand All @@ -52,7 +98,7 @@ var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true }, //not checking origin
}

//this is also the handler for joining to the chat
// this is also the handler for joining to the chat
func wsHandler(w http.ResponseWriter, r *http.Request) {

conn, err := upgrader.Upgrade(w, r, nil)
Expand Down

0 comments on commit 6bb3df7

Please sign in to comment.