use percent encoding on filenames

This commit is contained in:
Lilian Jónsdóttir 2024-08-05 23:09:00 -07:00
parent d6f066095b
commit d8ec25b36d
3 changed files with 25 additions and 9 deletions

View file

@ -1,4 +1,4 @@
// Package dirs provides functions sanitize directory names. // Package dirs provides functions to sanitize directory and file names.
package dirs package dirs
import ( import (
@ -7,7 +7,13 @@ import (
"strings" "strings"
) )
const sep = string(os.PathSeparator) const (
sep = string(os.PathSeparator)
space = " "
spacep = "%20"
newline = "\n"
newlinep = "%0A"
)
var ( var (
home = os.Getenv("HOME") home = os.Getenv("HOME")
@ -35,7 +41,7 @@ func UnExpand(dir, workdir string) (outdir string) {
outdir = strings.Replace(outdir, home, "~", 1) outdir = strings.Replace(outdir, home, "~", 1)
outdir = UnEscape(outdir) outdir = PercentDecode(outdir)
if outdir == "" { if outdir == "" {
outdir = "/" outdir = "/"
@ -44,8 +50,18 @@ func UnExpand(dir, workdir string) (outdir string) {
return return
} }
func UnEscape(input string) string { func PercentDecode(input string) (output string) {
return strings.ReplaceAll(input, "%20", " ") 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) { func cleanDir(dir, pwd string) (out string) {

View file

@ -245,7 +245,7 @@ func restore(files Files) (restored int, err error) {
} }
var cancel bool var cancel bool
outpath := dirs.UnEscape(file.ogpath) outpath := dirs.PercentDecode(file.ogpath)
log.Infof("restoring %s back to %s\n", file.name, outpath) log.Infof("restoring %s back to %s\n", file.name, outpath)
if _, e := os.Lstat(outpath); e == nil { if _, e := os.Lstat(outpath); e == nil {
outpath, cancel = prompt.NewPath(outpath) outpath, cancel = prompt.NewPath(outpath)
@ -323,7 +323,7 @@ func getTrashFilenames(filename, trashDir string) (string, string) {
if _, err := os.Stat(info); os.IsNotExist(err) { if _, err := os.Stat(info); os.IsNotExist(err) {
// doesn't exist, so use it // doesn't exist, so use it
path := filepath.Join(filedir, filename) path := filepath.Join(filedir, filename)
return info, path return dirs.PercentEncode(info), dirs.PercentEncode(path)
} }
// otherwise, try random suffixes until one works // 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) { if os.IsNotExist(infoErr) && os.IsNotExist(fileErr) {
path := filepath.Join(filedir, filename+rando) path := filepath.Join(filedir, filename+rando)
log.Debugf("settled on random name %s%s on the %s try", filename, rando, humanize.Ordinal(tries)) 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)
} }
} }
} }

View file

@ -669,7 +669,7 @@ func newRow(file files.File, workdir string) table.Row {
size = humanize.Bytes(uint64(file.Filesize())) size = humanize.Bytes(uint64(file.Filesize()))
} }
return table.Row{ return table.Row{
dirs.UnEscape(file.Name()), dirs.PercentDecode(file.Name()),
dirs.UnExpand(filepath.Dir(file.Path()), workdir), dirs.UnExpand(filepath.Dir(file.Path()), workdir),
time, time,
size, size,