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"
|
2022-06-26 08:06:31 +02:00
|
|
|
"os"
|
2020-08-13 06:56:41 +02:00
|
|
|
"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"
|
2022-06-26 08:06:31 +02:00
|
|
|
"github.com/owncast/owncast/utils"
|
2020-08-13 06:56:41 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-06-26 08:06:31 +02:00
|
|
|
var useCustomEmojiDirectory = utils.DoesFileExists(config.CustomEmojiPath)
|
2022-06-21 07:12:44 +02:00
|
|
|
|
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-26 08:06:31 +02:00
|
|
|
var emojiFS fs.FS
|
|
|
|
if useCustomEmojiDirectory {
|
|
|
|
emojiFS = os.DirFS(config.CustomEmojiPath)
|
|
|
|
} else {
|
|
|
|
emojiFS = static.GetEmoji()
|
|
|
|
}
|
|
|
|
|
2022-06-21 06:43:53 +02:00
|
|
|
emojiResponse := make([]models.CustomEmoji, 0)
|
|
|
|
|
2022-06-26 08:06:31 +02:00
|
|
|
files, err := fs.Glob(emojiFS, "*")
|
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) {
|
|
|
|
path := strings.TrimPrefix(r.URL.Path, "/img/emoji/")
|
2022-06-21 07:12:44 +02:00
|
|
|
r.URL.Path = path
|
2022-06-26 08:06:31 +02:00
|
|
|
|
|
|
|
var emojiStaticServer http.Handler
|
|
|
|
if useCustomEmojiDirectory {
|
|
|
|
emojiFS := os.DirFS(config.CustomEmojiPath)
|
|
|
|
emojiStaticServer = http.FileServer(http.FS(emojiFS))
|
|
|
|
} else {
|
|
|
|
emojiStaticServer = http.FileServer(http.FS(static.GetEmoji()))
|
|
|
|
}
|
|
|
|
|
2022-06-21 07:12:44 +02:00
|
|
|
emojiStaticServer.ServeHTTP(w, r)
|
2022-06-21 06:43:53 +02:00
|
|
|
}
|