-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correctly handle URIs like c: on Windows #4606
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package repository | |
import ( | ||
"errors" | ||
"path/filepath" | ||
"regexp" | ||
"runtime" | ||
"strings" | ||
|
||
|
@@ -11,6 +12,9 @@ import ( | |
"fyne.io/fyne/v2" | ||
) | ||
|
||
// Pattern to find Windows drive paths | ||
var windowsDrivePrefixhPattern = regexp.MustCompile(`(?i)^[a-z]:`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is |
||
|
||
// NewFileURI implements the back-end logic to storage.NewFileURI, which you | ||
// should use instead. This is only here because other functions in repository | ||
// need to call it, and it prevents a circular import. | ||
|
@@ -22,6 +26,10 @@ func NewFileURI(path string) fyne.URI { | |
// or NT style paths, with / or \, but when we reconstruct | ||
// the URI, we want to have / only. | ||
if runtime.GOOS == "windows" { | ||
// Make sure that Windows paths (eg "c:\") also correctly start with a "/" | ||
if windowsDrivePrefixhPattern.MatchString(path) { | ||
path = "/" + path | ||
} | ||
// seems that sometimes we end up with | ||
// double-backslashes | ||
path = filepath.ToSlash(path) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ import ( | |
|
||
func TestNewFileURI(t *testing.T) { | ||
assert.Equal(t, "file:///tmp/foo.txt", NewFileURI("/tmp/foo.txt").String()) | ||
assert.Equal(t, "file://C:/tmp/foo.txt", NewFileURI("C:/tmp/foo.txt").String()) | ||
assert.Equal(t, "file:///C:/tmp/foo.txt", NewFileURI("C:/tmp/foo.txt").String()) | ||
} | ||
|
||
func TestParseURI(t *testing.T) { | ||
|
@@ -20,9 +20,9 @@ func TestParseURI(t *testing.T) { | |
assert.Nil(t, err) | ||
assert.Equal(t, "file:///tmp/foo.txt", uri.String()) | ||
|
||
uri, err = ParseURI("file://C:/tmp/foo.txt") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've stated that legacy URI will still work - so please leave the old test in so we can ensure it stays working. |
||
uri, err = ParseURI("file:///C:/tmp/foo.txt") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "file://C:/tmp/foo.txt", uri.String()) | ||
assert.Equal(t, "file:///C:/tmp/foo.txt", uri.String()) | ||
} | ||
|
||
func TestParseInvalidURI(t *testing.T) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ import ( | |
"bufio" | ||
"mime" | ||
"path/filepath" | ||
"regexp" | ||
"runtime" | ||
"strings" | ||
"unicode/utf8" | ||
|
||
|
@@ -13,6 +15,9 @@ import ( | |
// Declare conformance with fyne.URI interface. | ||
var _ fyne.URI = &uri{} | ||
|
||
// Regex pattern to match Windows drive paths | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably needs a note about them being URI related or the slash in the middle may be unobvious or look like duplication of the other prefix pattern |
||
var windowsDrivePathPrefixPattern = regexp.MustCompile(`(?i)^/[a-z]:`) | ||
|
||
type uri struct { | ||
scheme string | ||
authority string | ||
|
@@ -76,6 +81,9 @@ func (u *uri) Authority() string { | |
} | ||
|
||
func (u *uri) Path() string { | ||
if runtime.GOOS == "windows" && windowsDrivePathPrefixPattern.MatchString(u.path) { | ||
return u.path[1:] | ||
} | ||
return u.path | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a drive identifier isn't it - not a path?