Sanitize file names

I falsely assumed that YT didn't allow this

Not only was this wrong, but other platforms exist too...
This commit is contained in:
Logan G 2025-01-03 02:01:48 -07:00
parent eada3e28d7
commit dc0f9af220
Signed by: logan
GPG key ID: E328528C921E7A7A
5 changed files with 36 additions and 3 deletions

View file

@ -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 != "" {

25
file.go
View file

@ -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
}

1
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=

View file

@ -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) {