Unverified Commit 03b14462 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #158 from jmank88/webfile-fpath

set WebFile fpath to URL base
parents 066003e4 a6ef022d
......@@ -276,6 +276,17 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
return err
}
} else if u := isURL(fpath); u != nil {
base := urlBase(u)
fpath = base
if _, ok := fileArgs[fpath]; ok {
// Ensure a unique fpath by suffixing ' (n)'.
for i := 1; ; i++ {
fpath = fmt.Sprintf("%s (%d)", base, i)
if _, ok := fileArgs[fpath]; !ok {
break
}
}
}
file = files.NewWebFile(u)
} else {
fpath = filepath.ToSlash(filepath.Clean(fpath))
......@@ -363,6 +374,13 @@ func isURL(path string) *url.URL {
}
}
func urlBase(u *url.URL) string {
if u.Path == "" {
return u.Host
}
return path.Base(u.Path)
}
func splitkv(opt string) (k, v string, ok bool) {
split := strings.SplitN(opt, "=", 2)
if len(split) == 2 {
......
......@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
"testing"
......@@ -534,3 +535,21 @@ func Test_isURL(t *testing.T) {
}
}
}
func Test_urlBase(t *testing.T) {
for _, test := range []struct{ url, base string }{
{"http://host", "host"},
{"http://host/test", "test"},
{"http://host/test?param=val", "test"},
{"http://host/test?param=val&param2=val", "test"},
} {
u, err := url.Parse(test.url)
if err != nil {
t.Errorf("failed to parse %q: %v", test.url, err)
continue
}
if got := urlBase(u); got != test.base {
t.Errorf("expected %q but got %q", test.base, got)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment