44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package backend
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
|
|
"music-server/internal/logging"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func GetCharacterList() []string {
|
|
charactersPath := os.Getenv("CHARACTERS_PATH")
|
|
logging.GetLogger().Debug("Getting character list", zap.String("path", charactersPath))
|
|
// Clean the path - remove trailing slashes and then add one for consistency
|
|
charactersPath = strings.TrimSuffix(charactersPath, "/")
|
|
charactersPath += "/"
|
|
files, err := os.ReadDir(charactersPath)
|
|
if err != nil {
|
|
logging.GetLogger().Fatal("Failed to read characters directory", zap.String("path", charactersPath), zap.String("error", err.Error()))
|
|
}
|
|
|
|
var characters []string
|
|
for _, file := range files {
|
|
if isImage(file) {
|
|
characters = append(characters, file.Name())
|
|
}
|
|
}
|
|
return characters
|
|
}
|
|
|
|
func GetCharacter(character string) string {
|
|
charactersPath := os.Getenv("CHARACTERS_PATH")
|
|
logging.GetLogger().Debug("Getting character", zap.String("character", character), zap.String("path", charactersPath))
|
|
// Clean the path - remove trailing slashes and then add one for consistency
|
|
charactersPath = strings.TrimSuffix(charactersPath, "/")
|
|
charactersPath += "/"
|
|
return charactersPath + character
|
|
}
|
|
|
|
func isImage(entry os.DirEntry) bool {
|
|
return !entry.IsDir() && (strings.HasSuffix(entry.Name(), ".jpg") || strings.HasSuffix(entry.Name(), ".jpeg") ||
|
|
strings.HasSuffix(entry.Name(), ".png"))
|
|
}
|