Use bundled images instead of old webroot files

This commit is contained in:
Gabe Kangas
2022-06-20 21:43:53 -07:00
parent d3a5ebd4be
commit 18a184eeb7
168 changed files with 164 additions and 260 deletions

View File

@@ -2,59 +2,60 @@ package controllers
import (
"encoding/json"
"io/fs"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/models"
"github.com/owncast/owncast/static"
"github.com/owncast/owncast/utils"
log "github.com/sirupsen/logrus"
)
var (
emojiCache = make([]models.CustomEmoji, 0)
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)
bundledEmoji := static.GetEmoji()
emojiResponse := make([]models.CustomEmoji, 0)
files, err := fs.Glob(bundledEmoji, "*")
if err != nil {
log.Errorln(err)
}
if emojiDirInfo.ModTime() != emojiCacheTimestamp {
log.Traceln("Emoji cache invalid")
emojiCache = make([]models.CustomEmoji, 0)
return emojiResponse
}
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, URL: emojiPath}
emojiCache = append(emojiCache, singleEmoji)
}
emojiCacheTimestamp = emojiDirInfo.ModTime()
for _, name := range files {
emojiPath := filepath.Join(config.EmojiDir, name)
singleEmoji := models.CustomEmoji{Name: name, URL: emojiPath}
emojiResponse = append(emojiResponse, singleEmoji)
}
return emojiCache
return emojiResponse
}
// GetCustomEmoji returns a list of custom emoji via the API.
func GetCustomEmoji(w http.ResponseWriter, r *http.Request) {
// GetCustomEmojiList returns a list of custom emoji via the API.
func GetCustomEmojiList(w http.ResponseWriter, r *http.Request) {
emojiList := getCustomEmojiList()
if err := json.NewEncoder(w).Encode(emojiList); err != nil {
InternalErrorHandler(w, err)
}
}
// GetCustomEmojiImage returns a single emoji image.
func GetCustomEmojiImage(w http.ResponseWriter, r *http.Request) {
bundledEmoji := static.GetEmoji()
path := strings.TrimPrefix(r.URL.Path, "/img/emoji/")
b, err := fs.ReadFile(bundledEmoji, path)
if err != nil {
log.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
contentType := "image/jpeg"
cacheTime := utils.GetCacheDurationSecondsForPath(path)
writeBytesAsImage(b, contentType, w, cacheTime)
}