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:
parent
eada3e28d7
commit
dc0f9af220
5 changed files with 36 additions and 3 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
//"strconv"
|
//"strconv"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
|
@ -47,7 +48,10 @@ func downloadVideo(url string, path string, group GroupConfig) (err error) {
|
||||||
return err
|
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 */
|
/* VIDEO */
|
||||||
if group.VideoFormat != "" {
|
if group.VideoFormat != "" {
|
||||||
|
|
25
file.go
25
file.go
|
@ -5,6 +5,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
|
@ -58,3 +60,26 @@ func copyFile(src, dst string) (error) {
|
||||||
|
|
||||||
return nil
|
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
1
go.mod
|
@ -7,6 +7,7 @@ require (
|
||||||
github.com/alexflint/go-arg v1.5.1
|
github.com/alexflint/go-arg v1.5.1
|
||||||
github.com/charmbracelet/log v0.4.0
|
github.com/charmbracelet/log v0.4.0
|
||||||
github.com/h2non/filetype v1.1.3
|
github.com/h2non/filetype v1.1.3
|
||||||
|
github.com/kennygrant/sanitize v1.2.4
|
||||||
github.com/u2takey/ffmpeg-go v0.5.0
|
github.com/u2takey/ffmpeg-go v0.5.0
|
||||||
github.com/wader/goutubedl v0.0.0-20241224160441-33e26ae8181c
|
github.com/wader/goutubedl v0.0.0-20241224160441-33e26ae8181c
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
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 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
|
||||||
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
|
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/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/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/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
|
|
5
ytva.go
5
ytva.go
|
@ -8,6 +8,7 @@ import (
|
||||||
//"io"
|
//"io"
|
||||||
//"reflect"
|
//"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
//"google.golang.org/api/option"
|
//"google.golang.org/api/option"
|
||||||
//"google.golang.org/api/youtube/v3"
|
//"google.golang.org/api/youtube/v3"
|
||||||
|
@ -159,9 +160,9 @@ func main() {
|
||||||
|
|
||||||
dirPath := fillPlaceholders(group.OutputDir, v)
|
dirPath := fillPlaceholders(group.OutputDir, v)
|
||||||
fileName := fillPlaceholders(group.FileName, 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 _, err := os.Stat(dirPath); err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
|
Loading…
Reference in a new issue