Cache the custom emoji list (#1175)
* use emojiCache * add emojiCacheTimestamp * add function description * better logging
This commit is contained in:
@@ -4,9 +4,11 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/owncast/owncast/config"
|
"github.com/owncast/owncast/config"
|
||||||
"github.com/owncast/owncast/models"
|
"github.com/owncast/owncast/models"
|
||||||
@@ -17,28 +19,44 @@ import (
|
|||||||
// to need it to be. The config is getting a bit bloated.
|
// to need it to be. The config is getting a bit bloated.
|
||||||
const emojiDir = "/img/emoji" // Relative to webroot
|
const emojiDir = "/img/emoji" // Relative to webroot
|
||||||
|
|
||||||
// GetCustomEmoji returns a list of custom emoji via the API.
|
var emojiCache = make([]models.CustomEmoji, 0)
|
||||||
func GetCustomEmoji(w http.ResponseWriter, r *http.Request) {
|
var emojiCacheTimestamp time.Time
|
||||||
emojiList := make([]models.CustomEmoji, 0)
|
|
||||||
|
|
||||||
|
// getCustomEmojiList returns a list of custom emoji either from the cache or from the emoji directory.
|
||||||
|
func getCustomEmojiList() []models.CustomEmoji {
|
||||||
fullPath := filepath.Join(config.WebRoot, emojiDir)
|
fullPath := filepath.Join(config.WebRoot, emojiDir)
|
||||||
files, err := ioutil.ReadDir(fullPath)
|
emojiDirInfo, err := os.Stat(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln(err)
|
log.Errorln(err)
|
||||||
// Throw HTTP 500
|
}
|
||||||
return
|
if emojiDirInfo.ModTime() != emojiCacheTimestamp {
|
||||||
|
log.Traceln("Emoji cache invalid")
|
||||||
|
emojiCache = make([]models.CustomEmoji, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Memoize this result somewhere? Right now it iterates through the
|
if len(emojiCache) == 0 {
|
||||||
// filesystem every time the API is hit. Should you need to restart
|
files, err := ioutil.ReadDir(fullPath)
|
||||||
// the server to add emoji?
|
if err != nil {
|
||||||
for _, f := range files {
|
log.Errorln(err)
|
||||||
name := strings.TrimSuffix(f.Name(), path.Ext(f.Name()))
|
return emojiCache
|
||||||
emojiPath := filepath.Join(emojiDir, f.Name())
|
}
|
||||||
singleEmoji := models.CustomEmoji{Name: name, Emoji: emojiPath}
|
for _, f := range files {
|
||||||
emojiList = append(emojiList, singleEmoji)
|
name := strings.TrimSuffix(f.Name(), path.Ext(f.Name()))
|
||||||
|
emojiPath := filepath.Join(emojiDir, f.Name())
|
||||||
|
singleEmoji := models.CustomEmoji{Name: name, Emoji: emojiPath}
|
||||||
|
emojiCache = append(emojiCache, singleEmoji)
|
||||||
|
}
|
||||||
|
|
||||||
|
emojiCacheTimestamp = emojiDirInfo.ModTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return emojiCache
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCustomEmoji returns a list of custom emoji via the API.
|
||||||
|
func GetCustomEmoji(w http.ResponseWriter, r *http.Request) {
|
||||||
|
emojiList := getCustomEmojiList()
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(emojiList); err != nil {
|
if err := json.NewEncoder(w).Encode(emojiList); err != nil {
|
||||||
InternalErrorHandler(w, err)
|
InternalErrorHandler(w, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user