From dc0f9af220faa0f3b179e5f7fb133d8621775a37 Mon Sep 17 00:00:00 2001 From: Logan G Date: Fri, 3 Jan 2025 02:01:48 -0700 Subject: [PATCH] Sanitize file names I falsely assumed that YT didn't allow this Not only was this wrong, but other platforms exist too... --- download.go | 6 +++++- file.go | 25 +++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ ytva.go | 5 +++-- 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/download.go b/download.go index 467d8a7..9b36c75 100644 --- a/download.go +++ b/download.go @@ -8,6 +8,7 @@ import ( "errors" "net/http" //"strconv" + "path/filepath" "github.com/charmbracelet/log" @@ -47,7 +48,10 @@ func downloadVideo(url string, path string, group GroupConfig) (err error) { return err } - tempPath := MainConfig.TempDir+"/"+video.Info.ID + tempPath := filepath.Join(MainConfig.TempDir, sanitizeFilename(replaceDelimiters(video.Info.ID))) + tempPath = filepath.Clean(tempPath) + + log.Debugf("Using temp path \"%s\".", tempPath) /* VIDEO */ if group.VideoFormat != "" { diff --git a/file.go b/file.go index e884206..7cdde53 100644 --- a/file.go +++ b/file.go @@ -5,6 +5,8 @@ import ( "os" "errors" "io" + "strings" + "regexp" "github.com/charmbracelet/log" @@ -58,3 +60,26 @@ func copyFile(src, dst string) (error) { return nil } + +func replaceDelimiters(input string) string { + replacer := strings.NewReplacer( + "/", "/", + "\\", "\", + ) + return replacer.Replace(input) +} + +func sanitizeFilename(filename string) string { + invalidFilenameChars := regexp.MustCompile(`[^a-zA-Z0-9._-/\[[:blank:]]]+`) + + sanitized := invalidFilenameChars.ReplaceAllString(filename, "-") + + // Trim any leading or trailing spaces or underscores. + sanitized = strings.Trim(sanitized, " _.") + + if sanitized == "" { + sanitized = " " + } + + return sanitized +} diff --git a/go.mod b/go.mod index ab695da..089bce4 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/alexflint/go-arg v1.5.1 github.com/charmbracelet/log v0.4.0 github.com/h2non/filetype v1.1.3 + github.com/kennygrant/sanitize v1.2.4 github.com/u2takey/ffmpeg-go v0.5.0 github.com/wader/goutubedl v0.0.0-20241224160441-33e26ae8181c ) diff --git a/go.sum b/go.sum index a6e52c9..c8a8463 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o= +github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= diff --git a/ytva.go b/ytva.go index 8e234e1..2f17bca 100644 --- a/ytva.go +++ b/ytva.go @@ -8,6 +8,7 @@ import ( //"io" //"reflect" "regexp" + "path/filepath" //"google.golang.org/api/option" //"google.golang.org/api/youtube/v3" @@ -159,9 +160,9 @@ func main() { dirPath := fillPlaceholders(group.OutputDir, v) fileName := fillPlaceholders(group.FileName, v) - path := dirPath+"/"+fileName + path := filepath.Join(dirPath, filepath.Clean(sanitizeFilename(replaceDelimiters(fileName)))) - log.Debugf("Output: %s", path) + log.Debugf("Output: \"%s\"", path) if _, err := os.Stat(dirPath); err != nil { if os.IsNotExist(err) {