Commit a6ef022d authored by jmank88's avatar jmank88

set WebFile fpath to URL base

parent 49cf8cee
...@@ -276,6 +276,17 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error { ...@@ -276,6 +276,17 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
return err return err
} }
} else if u := isURL(fpath); u != nil { } 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) file = files.NewWebFile(u)
} else { } else {
fpath = filepath.ToSlash(filepath.Clean(fpath)) fpath = filepath.ToSlash(filepath.Clean(fpath))
...@@ -363,6 +374,13 @@ func isURL(path string) *url.URL { ...@@ -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) { func splitkv(opt string) (k, v string, ok bool) {
split := strings.SplitN(opt, "=", 2) split := strings.SplitN(opt, "=", 2)
if len(split) == 2 { if len(split) == 2 {
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net/url"
"os" "os"
"strings" "strings"
"testing" "testing"
...@@ -534,3 +535,21 @@ func Test_isURL(t *testing.T) { ...@@ -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