Handle subdirectories of emoji in copying, fetching and deleting. For #1916

This commit is contained in:
Gabe Kangas
2022-12-16 20:22:21 -08:00
parent 71be5e4b41
commit 0ebb968074
3 changed files with 62 additions and 22 deletions

View File

@@ -11,25 +11,30 @@ import (
"github.com/owncast/owncast/models"
"github.com/owncast/owncast/static"
"github.com/owncast/owncast/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
// GetEmojiList returns a list of custom emoji from the emoji directory.
func GetEmojiList() []models.CustomEmoji {
var emojiFS = os.DirFS(config.CustomEmojiPath)
emojiFS := os.DirFS(config.CustomEmojiPath)
emojiResponse := make([]models.CustomEmoji, 0)
files, err := fs.Glob(emojiFS, "*")
if err != nil {
log.Errorln(err)
return emojiResponse
walkFunction := func(path string, d os.DirEntry, err error) error {
if d.IsDir() {
return nil
}
emojiPath := filepath.Join(config.EmojiDir, path)
singleEmoji := models.CustomEmoji{Name: d.Name(), URL: emojiPath}
emojiResponse = append(emojiResponse, singleEmoji)
return nil
}
for _, name := range files {
emojiPath := filepath.Join(config.EmojiDir, name)
singleEmoji := models.CustomEmoji{Name: name, URL: emojiPath}
emojiResponse = append(emojiResponse, singleEmoji)
if err := fs.WalkDir(emojiFS, ".", walkFunction); err != nil {
log.Errorln("unable to fetch emojis: " + err.Error())
return emojiResponse
}
return emojiResponse
@@ -38,6 +43,11 @@ func GetEmojiList() []models.CustomEmoji {
// SetupEmojiDirectory sets up the custom emoji directory by copying all built-in
// emojis if the directory does not yet exist.
func SetupEmojiDirectory() (err error) {
type emojiDirectory struct {
path string
isDir bool
}
if utils.DoesFileExists(config.CustomEmojiPath) {
return nil
}
@@ -47,14 +57,42 @@ func SetupEmojiDirectory() (err error) {
}
staticFS := static.GetEmoji()
files, err := fs.Glob(staticFS, "*")
files := []emojiDirectory{}
walkFunction := func(path string, d os.DirEntry, err error) error {
if path == "." {
return nil
}
if d.Name() == "LICENSE.md" {
return nil
}
files = append(files, emojiDirectory{path: path, isDir: d.IsDir()})
return nil
}
if err := fs.WalkDir(staticFS, ".", walkFunction); err != nil {
log.Errorln("unable to fetch emojis: " + err.Error())
return errors.Wrap(err, "unable to fetch embedded emoji files")
}
if err != nil {
return fmt.Errorf("unable to read built-in emoji files: %w", err)
}
// Now copy all built-in emojis to the custom emoji directory
for _, name := range files {
emojiPath := filepath.Join(config.CustomEmojiPath, filepath.Base(name))
for _, path := range files {
emojiPath := filepath.Join(config.CustomEmojiPath, path.path)
if path.isDir {
if err := os.Mkdir(emojiPath, 0o700); err != nil {
return errors.Wrap(err, "unable to create emoji directory, check permissions?: "+path.path)
}
continue
}
memFile, err := staticFS.Open(path.path)
// nolint:gosec
diskFile, err := os.Create(emojiPath)
@@ -62,7 +100,6 @@ func SetupEmojiDirectory() (err error) {
return fmt.Errorf("unable to create custom emoji file on disk: %w", err)
}
memFile, err := staticFS.Open(name)
if err != nil {
_ = diskFile.Close()
return fmt.Errorf("unable to open built-in emoji file: %w", err)