owncast/controllers/emoji.go
Eng Zer Jun 7996df85e1
refactor: move from io/ioutil to io and os packages (#1546)
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-11-19 22:42:50 -08:00

58 lines
1.4 KiB
Go

package controllers
import (
"encoding/json"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/models"
log "github.com/sirupsen/logrus"
)
var emojiCache = make([]models.CustomEmoji, 0)
var emojiCacheTimestamp time.Time
// 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, config.EmojiDir)
emojiDirInfo, err := os.Stat(fullPath)
if err != nil {
log.Errorln(err)
}
if emojiDirInfo.ModTime() != emojiCacheTimestamp {
log.Traceln("Emoji cache invalid")
emojiCache = make([]models.CustomEmoji, 0)
}
if len(emojiCache) == 0 {
files, err := os.ReadDir(fullPath)
if err != nil {
log.Errorln(err)
return emojiCache
}
for _, f := range files {
name := strings.TrimSuffix(f.Name(), path.Ext(f.Name()))
emojiPath := filepath.Join(config.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 {
InternalErrorHandler(w, err)
}
}