Skip to content

Commit

Permalink
fix: decode all URL-encoded characters in the filename
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishb committed Oct 3, 2024
1 parent 68ca7ab commit 3f3f51e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/wp2hugo/internal/hugogenerator/media_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"net/url"
"os"
"path"
"strings"
"regexp"
)

func writeFavicon(outputDirPath string, faviconData io.Reader) error {
log.Debug().Msg("Writing favicon")
return download(path.Join(outputDirPath, "favicon.ico"), faviconData)
}

// Match %dd
var _hexPattern = regexp.MustCompile(`%[0-9a-fA-F]{2}`)

func download(destFilePath string, reader io.Reader) error {
log.Debug().
Str("destFilePath", destFilePath).
Expand All @@ -24,7 +27,7 @@ func download(destFilePath string, reader io.Reader) error {
return err
}
fileName := path.Base(destFilePath)
if strings.Contains(fileName, "%5F") {
if _hexPattern.MatchString(fileName) {
tmp1, err := url.PathUnescape(fileName)
if err != nil {
return fmt.Errorf("error unescaping filename %s: %s", fileName, err)
Expand All @@ -39,7 +42,7 @@ func download(destFilePath string, reader io.Reader) error {

file, err := os.OpenFile(destFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("error opening file %s: %s", file.Name(), err)
return fmt.Errorf("error opening file %s: %s", destFilePath, err)
}
defer file.Close()
_, err = io.Copy(file, reader)
Expand Down

0 comments on commit 3f3f51e

Please sign in to comment.