re-use fileutil.Safe

This commit is contained in:
sentriz
2023-11-28 18:18:23 +00:00
parent 707ebe1a49
commit 7bcd69dcdf

View File

@@ -7,11 +7,12 @@ import (
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
"go.senan.xyz/gonic/fileutil"
) )
var ( var (
@@ -69,7 +70,7 @@ type Playlist struct {
} }
func NewPath(userID int, playlistName string) string { func NewPath(userID int, playlistName string) string {
playlistName = safeFilename(playlistName) playlistName = fileutil.Safe(playlistName)
if playlistName == "" { if playlistName == "" {
playlistName = "pl" playlistName = "pl"
} }
@@ -219,13 +220,6 @@ func (s *Store) Delete(relPath string) error {
return os.Remove(filepath.Join(s.basePath, relPath)) return os.Remove(filepath.Join(s.basePath, relPath))
} }
var nonAlphaNum = regexp.MustCompile("[^a-zA-Z0-9_.]+")
func safeFilename(filename string) string {
filename = nonAlphaNum.ReplaceAllString(filename, "")
return filename
}
func firstPathEl(path string) string { func firstPathEl(path string) string {
path = strings.TrimPrefix(path, string(filepath.Separator)) path = strings.TrimPrefix(path, string(filepath.Separator))
parts := strings.Split(path, string(filepath.Separator)) parts := strings.Split(path, string(filepath.Separator))