2020-08-13 06:56:41 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-06-21 06:43:53 +02:00
|
|
|
"io/fs"
|
2020-08-13 06:56:41 +02:00
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/models"
|
2022-06-21 06:43:53 +02:00
|
|
|
"github.com/owncast/owncast/static"
|
|
|
|
"github.com/owncast/owncast/utils"
|
2020-08-13 06:56:41 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-07-12 00:34:56 +02:00
|
|
|
// getCustomEmojiList returns a list of custom emoji either from the cache or from the emoji directory.
|
|
|
|
func getCustomEmojiList() []models.CustomEmoji {
|
2022-06-21 06:43:53 +02:00
|
|
|
bundledEmoji := static.GetEmoji()
|
|
|
|
emojiResponse := make([]models.CustomEmoji, 0)
|
|
|
|
|
|
|
|
files, err := fs.Glob(bundledEmoji, "*")
|
2020-08-13 06:56:41 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
2022-06-21 06:43:53 +02:00
|
|
|
return emojiResponse
|
2020-08-13 06:56:41 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 06:43:53 +02:00
|
|
|
for _, name := range files {
|
|
|
|
emojiPath := filepath.Join(config.EmojiDir, name)
|
|
|
|
singleEmoji := models.CustomEmoji{Name: name, URL: emojiPath}
|
|
|
|
emojiResponse = append(emojiResponse, singleEmoji)
|
2020-08-13 06:56:41 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 06:43:53 +02:00
|
|
|
return emojiResponse
|
2021-07-12 00:34:56 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 06:43:53 +02:00
|
|
|
// GetCustomEmojiList returns a list of custom emoji via the API.
|
|
|
|
func GetCustomEmojiList(w http.ResponseWriter, r *http.Request) {
|
2021-07-12 00:34:56 +02:00
|
|
|
emojiList := getCustomEmojiList()
|
|
|
|
|
2020-11-15 03:39:53 +01:00
|
|
|
if err := json.NewEncoder(w).Encode(emojiList); err != nil {
|
2021-02-19 08:05:52 +01:00
|
|
|
InternalErrorHandler(w, err)
|
2020-11-15 03:39:53 +01:00
|
|
|
}
|
2020-08-13 06:56:41 +02:00
|
|
|
}
|
2022-06-21 06:43:53 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|