mirror of
https://github.com/owncast/owncast.git
synced 2025-11-03 13:01:46 +08:00
Feature: emoji editor (#2411)
* Custom emoji editor: implement backend This reuses the logo upload code * Implement emoji edit admin interface Again reuse base64 logic from the logo upload * Allow toggling between uploaded and default emojis * Add route that always serves uploaded emojis This is needed for the admin emoji interface, as otherwise the emojis will 404 if custom emojis are disabled * Fix linter warnings * Remove custom/uploaded emoji logic * Reset timer after emoji deletion * Setup: copy built-in emojis to emoji directory
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
@ -228,39 +227,12 @@ func SetLogo(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s := strings.SplitN(configValue.Value.(string), ",", 2)
|
||||
if len(s) < 2 {
|
||||
controllers.WriteSimpleResponse(w, false, "Error splitting base64 image data.")
|
||||
return
|
||||
}
|
||||
bytes, err := base64.StdEncoding.DecodeString(s[1])
|
||||
bytes, extension, err := utils.DecodeBase64Image(configValue.Value.(string))
|
||||
if err != nil {
|
||||
controllers.WriteSimpleResponse(w, false, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
splitHeader := strings.Split(s[0], ":")
|
||||
if len(splitHeader) < 2 {
|
||||
controllers.WriteSimpleResponse(w, false, "Error splitting base64 image header.")
|
||||
return
|
||||
}
|
||||
contentType := strings.Split(splitHeader[1], ";")[0]
|
||||
extension := ""
|
||||
if contentType == "image/svg+xml" {
|
||||
extension = ".svg"
|
||||
} else if contentType == "image/gif" {
|
||||
extension = ".gif"
|
||||
} else if contentType == "image/png" {
|
||||
extension = ".png"
|
||||
} else if contentType == "image/jpeg" {
|
||||
extension = ".jpeg"
|
||||
}
|
||||
|
||||
if extension == "" {
|
||||
controllers.WriteSimpleResponse(w, false, "Missing or invalid contentType in base64 image.")
|
||||
return
|
||||
}
|
||||
|
||||
imgPath := filepath.Join("data", "logo"+extension)
|
||||
if err := os.WriteFile(imgPath, bytes, 0o600); err != nil {
|
||||
controllers.WriteSimpleResponse(w, false, err.Error())
|
||||
|
||||
92
controllers/admin/emoji.go
Normal file
92
controllers/admin/emoji.go
Normal file
@ -0,0 +1,92 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/controllers"
|
||||
"github.com/owncast/owncast/utils"
|
||||
)
|
||||
|
||||
// UploadCustomEmoji allows POSTing a new custom emoji to the server.
|
||||
func UploadCustomEmoji(w http.ResponseWriter, r *http.Request) {
|
||||
if !requirePOST(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
type postEmoji struct {
|
||||
Name string `json:"name"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
emoji := new(postEmoji)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(emoji); err != nil {
|
||||
controllers.WriteSimpleResponse(w, false, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
bytes, _, err := utils.DecodeBase64Image(emoji.Data)
|
||||
if err != nil {
|
||||
controllers.WriteSimpleResponse(w, false, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Prevent path traversal attacks
|
||||
var emojiFileName = filepath.Base(emoji.Name)
|
||||
var targetPath = filepath.Join(config.CustomEmojiPath, emojiFileName)
|
||||
|
||||
err = os.MkdirAll(config.CustomEmojiPath, 0700)
|
||||
if err != nil {
|
||||
controllers.WriteSimpleResponse(w, false, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if utils.DoesFileExists(targetPath) {
|
||||
controllers.WriteSimpleResponse(w, false, fmt.Sprintf("An emoji with the name %q already exists", emojiFileName))
|
||||
return
|
||||
}
|
||||
|
||||
if err = os.WriteFile(targetPath, bytes, 0o600); err != nil {
|
||||
controllers.WriteSimpleResponse(w, false, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
controllers.WriteSimpleResponse(w, true, fmt.Sprintf("Emoji %q has been uploaded", emojiFileName))
|
||||
}
|
||||
|
||||
// DeleteCustomEmoji deletes a custom emoji.
|
||||
func DeleteCustomEmoji(w http.ResponseWriter, r *http.Request) {
|
||||
if !requirePOST(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
type deleteEmoji struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
emoji := new(deleteEmoji)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(emoji); err != nil {
|
||||
controllers.WriteSimpleResponse(w, false, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var emojiFileName = filepath.Base(emoji.Name)
|
||||
var targetPath = filepath.Join(config.CustomEmojiPath, emojiFileName)
|
||||
|
||||
if err := os.Remove(targetPath); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
controllers.WriteSimpleResponse(w, false, fmt.Sprintf("Emoji %q doesn't exist", emojiFileName))
|
||||
} else {
|
||||
controllers.WriteSimpleResponse(w, false, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
controllers.WriteSimpleResponse(w, true, fmt.Sprintf("Emoji %q has been deleted", emojiFileName))
|
||||
}
|
||||
@ -2,50 +2,17 @@ package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
)
|
||||
|
||||
var useCustomEmojiDirectory = utils.DoesFileExists(config.CustomEmojiPath)
|
||||
|
||||
// getCustomEmojiList returns a list of custom emoji either from the cache or from the emoji directory.
|
||||
func getCustomEmojiList() []models.CustomEmoji {
|
||||
var emojiFS fs.FS
|
||||
if useCustomEmojiDirectory {
|
||||
emojiFS = os.DirFS(config.CustomEmojiPath)
|
||||
} else {
|
||||
emojiFS = static.GetEmoji()
|
||||
}
|
||||
|
||||
emojiResponse := make([]models.CustomEmoji, 0)
|
||||
|
||||
files, err := fs.Glob(emojiFS, "*")
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return emojiResponse
|
||||
}
|
||||
|
||||
for _, name := range files {
|
||||
emojiPath := filepath.Join(config.EmojiDir, name)
|
||||
singleEmoji := models.CustomEmoji{Name: name, URL: emojiPath}
|
||||
emojiResponse = append(emojiResponse, singleEmoji)
|
||||
}
|
||||
|
||||
return emojiResponse
|
||||
}
|
||||
|
||||
// GetCustomEmojiList returns a list of custom emoji via the API.
|
||||
// GetCustomEmojiList returns a list of emoji via the API.
|
||||
func GetCustomEmojiList(w http.ResponseWriter, r *http.Request) {
|
||||
emojiList := getCustomEmojiList()
|
||||
emojiList := data.GetEmojiList()
|
||||
|
||||
if err := json.NewEncoder(w).Encode(emojiList); err != nil {
|
||||
InternalErrorHandler(w, err)
|
||||
@ -57,13 +24,6 @@ func GetCustomEmojiImage(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/img/emoji/")
|
||||
r.URL.Path = path
|
||||
|
||||
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()))
|
||||
}
|
||||
|
||||
emojiStaticServer.ServeHTTP(w, r)
|
||||
emojiFS := os.DirFS(config.CustomEmojiPath)
|
||||
http.FileServer(http.FS(emojiFS)).ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user