From 95f60f61531916832e900d4906b675d2dd3f27f5 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Thu, 18 Jan 2018 22:43:08 -0600 Subject: [PATCH] Modified common/config.go to accommodate some of the new DownloadableURL policies made by the PR #5761 merge. common/config.go: Added the ability for DownloadableURL to promote UNC paths to the SMB uri. Modified DownloadableURL to include the "./" prefix when a relative path is passed to it. Fix-up the DownloadableURL argument if on windows and incorrectly prefixed with "/". --- common/config.go | 50 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/common/config.go b/common/config.go index c79710221..a3723b40a 100644 --- a/common/config.go +++ b/common/config.go @@ -74,18 +74,40 @@ func SupportedURL(u *url.URL) bool { func DownloadableURL(original string) (string, error) { var result string + // Check that the user specified a UNC path, and promote it to an smb:// uri. + if strings.HasPrefix(original, "\\\\") && len(original) > 2 && original[2] != '?' { + result = filepath.ToSlash(original[2:]) + return fmt.Sprintf("smb://%s", result), nil + } + // Fix the url if it's using bad characters commonly mistaken with a path. original = filepath.ToSlash(original) - // Check to see that this is a parseable URL with a scheme. If so, then just pass it through. + // Check to see that this is a parseable URL with a scheme and a host. + // If so, then just pass it through. if u, err := url.Parse(original); err == nil && u.Scheme != "" && u.Host != "" { - return filepath.ToSlash(original), nil + return original, nil } - // Since it's not a url, this might be a path. So, check that the file exists, - // then make it an absolute path so we can make a proper uri. - if _, err := os.Stat(original); err == nil { - result, err = filepath.Abs(filepath.FromSlash(original)) + // If it's a file scheme, then convert it back to a regular path so the next + // case which forces it to an absolute path, will correct it. + if u, err := url.Parse(original); err == nil && strings.ToLower(u.Scheme) == "file" { + original = u.Path + } + + // If we're on Windows and we start with a slash, then this absolute path + // is wrong. Fix it up, so the next case can figure out the absolute path. + if rpath := strings.SplitN(original, "/", 2); rpath[0] == "" && runtime.GOOS == "windows" { + result = rpath[1] + } else { + result = original + } + + // Since we should be some kind of path (relative or absolute), check + // that the file exists, then make it an absolute path so we can return an + // absolute uri. + if _, err := os.Stat(result); err == nil { + result, err = filepath.Abs(filepath.FromSlash(result)) if err != nil { return "", err } @@ -96,15 +118,17 @@ func DownloadableURL(original string) (string, error) { } result = filepath.Clean(result) - result = filepath.ToSlash(result) - - // We have no idea what this might be, so we'll leave it as is. - } else { - result = filepath.ToSlash(original) + return fmt.Sprintf("file:///%s", filepath.ToSlash(result)), nil } - // We should have a path that can just turn into a file:// scheme'd url. - return fmt.Sprintf("file://%s", result), nil + // Otherwise, check if it was originally an absolute path, and fix it if so. + if strings.HasPrefix(original, "/") { + return fmt.Sprintf("file:///%s", result), nil + } + + // Anything left should be a non-existent relative path. So fix it up here. + result = filepath.ToSlash(filepath.Clean(result)) + return fmt.Sprintf("file://./%s", result), nil } // Force the parameter into a url. This will transform the parameter into