From d8ec25b36d95682b3c5e68465a8c5c9e382d96f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Mon, 5 Aug 2024 23:09:00 -0700 Subject: [PATCH] use percent encoding on filenames --- internal/dirs/dirs.go | 26 +++++++++++++++++++++----- internal/files/trash.go | 6 +++--- internal/interactive/interactive.go | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/internal/dirs/dirs.go b/internal/dirs/dirs.go index ad37991..ed3d84a 100644 --- a/internal/dirs/dirs.go +++ b/internal/dirs/dirs.go @@ -1,4 +1,4 @@ -// Package dirs provides functions sanitize directory names. +// Package dirs provides functions to sanitize directory and file names. package dirs import ( @@ -7,7 +7,13 @@ import ( "strings" ) -const sep = string(os.PathSeparator) +const ( + sep = string(os.PathSeparator) + space = " " + spacep = "%20" + newline = "\n" + newlinep = "%0A" +) var ( home = os.Getenv("HOME") @@ -35,7 +41,7 @@ func UnExpand(dir, workdir string) (outdir string) { outdir = strings.Replace(outdir, home, "~", 1) - outdir = UnEscape(outdir) + outdir = PercentDecode(outdir) if outdir == "" { outdir = "/" @@ -44,8 +50,18 @@ func UnExpand(dir, workdir string) (outdir string) { return } -func UnEscape(input string) string { - return strings.ReplaceAll(input, "%20", " ") +func PercentDecode(input string) (output string) { + output = strings.ReplaceAll(input, spacep, space) + output = strings.ReplaceAll(output, newlinep, newline) + + return +} + +func PercentEncode(input string) (output string) { + output = strings.ReplaceAll(input, space, spacep) + output = strings.ReplaceAll(output, newline, newlinep) + + return } func cleanDir(dir, pwd string) (out string) { diff --git a/internal/files/trash.go b/internal/files/trash.go index 22c9d28..a235a95 100644 --- a/internal/files/trash.go +++ b/internal/files/trash.go @@ -245,7 +245,7 @@ func restore(files Files) (restored int, err error) { } var cancel bool - outpath := dirs.UnEscape(file.ogpath) + outpath := dirs.PercentDecode(file.ogpath) log.Infof("restoring %s back to %s\n", file.name, outpath) if _, e := os.Lstat(outpath); e == nil { outpath, cancel = prompt.NewPath(outpath) @@ -323,7 +323,7 @@ func getTrashFilenames(filename, trashDir string) (string, string) { if _, err := os.Stat(info); os.IsNotExist(err) { // doesn't exist, so use it path := filepath.Join(filedir, filename) - return info, path + return dirs.PercentEncode(info), dirs.PercentEncode(path) } // otherwise, try random suffixes until one works @@ -339,7 +339,7 @@ func getTrashFilenames(filename, trashDir string) (string, string) { if os.IsNotExist(infoErr) && os.IsNotExist(fileErr) { path := filepath.Join(filedir, filename+rando) log.Debugf("settled on random name %s%s on the %s try", filename, rando, humanize.Ordinal(tries)) - return newInfo, path + return dirs.PercentEncode(newInfo), dirs.PercentEncode(path) } } } diff --git a/internal/interactive/interactive.go b/internal/interactive/interactive.go index 5d5bf51..13fa0bc 100644 --- a/internal/interactive/interactive.go +++ b/internal/interactive/interactive.go @@ -669,7 +669,7 @@ func newRow(file files.File, workdir string) table.Row { size = humanize.Bytes(uint64(file.Filesize())) } return table.Row{ - dirs.UnEscape(file.Name()), + dirs.PercentDecode(file.Name()), dirs.UnExpand(filepath.Dir(file.Path()), workdir), time, size,