2020-08-13 06:56:41 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/models"
|
2020-08-13 06:56:41 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Make this path configurable if somebody has a valid reason
|
|
|
|
// to need it to be. The config is getting a bit bloated.
|
2020-10-17 00:04:31 +02:00
|
|
|
const emojiDir = "/img/emoji" // Relative to webroot
|
2020-08-13 06:56:41 +02:00
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// GetCustomEmoji returns a list of custom emoji via the API.
|
2020-08-13 06:56:41 +02:00
|
|
|
func GetCustomEmoji(w http.ResponseWriter, r *http.Request) {
|
|
|
|
emojiList := make([]models.CustomEmoji, 0)
|
|
|
|
|
2020-10-17 00:04:31 +02:00
|
|
|
fullPath := filepath.Join(config.WebRoot, emojiDir)
|
2020-08-13 06:56:41 +02:00
|
|
|
files, err := ioutil.ReadDir(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
// Throw HTTP 500
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Memoize this result somewhere? Right now it iterates through the
|
|
|
|
// filesystem every time the API is hit. Should you need to restart
|
|
|
|
// the server to add emoji?
|
|
|
|
for _, f := range files {
|
|
|
|
name := strings.TrimSuffix(f.Name(), path.Ext(f.Name()))
|
2020-10-17 00:04:31 +02:00
|
|
|
emojiPath := filepath.Join(emojiDir, f.Name())
|
2020-11-15 03:39:53 +01:00
|
|
|
singleEmoji := models.CustomEmoji{Name: name, Emoji: emojiPath}
|
2020-08-13 06:56:41 +02:00
|
|
|
emojiList = append(emojiList, singleEmoji)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|