mirror of
https://github.com/owncast/owncast.git
synced 2025-11-01 02:44:31 +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:
@ -2,6 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -364,3 +365,42 @@ func ShuffleStringSlice(s []string) []string {
|
||||
func IntPercentage(x, total int) int {
|
||||
return int(float64(x) / float64(total) * 100)
|
||||
}
|
||||
|
||||
// DecodeBase64Image decodes a base64 image string into a byte array, returning the extension (including dot) for the content type.
|
||||
func DecodeBase64Image(url string) (bytes []byte, extension string, err error) {
|
||||
s := strings.SplitN(url, ",", 2)
|
||||
if len(s) < 2 {
|
||||
err = errors.New("error splitting base64 image data")
|
||||
return
|
||||
}
|
||||
|
||||
bytes, err = base64.StdEncoding.DecodeString(s[1])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
splitHeader := strings.Split(s[0], ":")
|
||||
if len(splitHeader) < 2 {
|
||||
err = errors.New("error splitting base64 image header")
|
||||
return
|
||||
}
|
||||
|
||||
contentType := strings.Split(splitHeader[1], ";")[0]
|
||||
|
||||
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 == "" {
|
||||
err = errors.New("missing or invalid contentType in base64 image")
|
||||
return
|
||||
}
|
||||
|
||||
return bytes, extension, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user