Skip to content

Commit

Permalink
Add a file URI helper, cleans up path usage
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Oct 10, 2020
1 parent 1076d2b commit d3d7f45
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
10 changes: 5 additions & 5 deletions dialog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (f *fileDialog) loadFavorites() ([]fyne.CanvasObject, error) {
osHome, err = os.UserHomeDir()

if err == nil {
home, err1 = storage.ListerForURI(storage.NewURI("file://" + osHome))
home, err1 = storage.ListerForURI(storage.NewFileURI(osHome))
if err1 == nil {
var documentsURI fyne.URI
documentsURI, err1 = storage.Child(home, "Documents")
Expand Down Expand Up @@ -304,7 +304,7 @@ func (f *fileDialog) setDirectory(dir fyne.ListableURI) error {
buildDir = d + string(os.PathSeparator)
}

newDir, err := storage.ListerForURI(storage.NewURI("file://" + buildDir))
newDir, err := storage.ListerForURI(storage.NewFileURI(buildDir))
if err != nil {
return err
}
Expand Down Expand Up @@ -384,7 +384,7 @@ func (f *FileDialog) effectiveStartingDir() fyne.ListableURI {
// Try home dir
dir, err := os.UserHomeDir()
if err == nil {
lister, err := storage.ListerForURI(storage.NewURI("file://" + dir))
lister, err := storage.ListerForURI(storage.NewFileURI(dir))
if err == nil {
return lister
}
Expand All @@ -395,14 +395,14 @@ func (f *FileDialog) effectiveStartingDir() fyne.ListableURI {
// Try to get ./
wd, err := os.Getwd()
if err == nil {
lister, err := storage.ListerForURI(storage.NewURI("file://" + wd))
lister, err := storage.ListerForURI(storage.NewFileURI(wd))
if err == nil {
return lister
}
fyne.LogError("Could not create lister for working dir", err)
}

lister, err := storage.ListerForURI(storage.NewURI("file:///"))
lister, err := storage.ListerForURI(storage.NewFileURI("/"))
if err != nil {
fyne.LogError("could not create lister for /", err)
return nil
Expand Down
6 changes: 3 additions & 3 deletions dialog/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestEffectiveStartingDir(t *testing.T) {
if err != nil {
t.Skipf("os.Gethome() failed, cannot run this test on this system (error stat()-ing ../) error was '%s'", err)
}
home, err := storage.ListerForURI(storage.NewURI("file://" + homeString))
home, err := storage.ListerForURI(storage.NewFileURI(homeString))
if err != nil {
t.Skipf("could not get lister for working directory: %s", err)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestEffectiveStartingDir(t *testing.T) {
}

// make sure we fail over if the specified directory does not exist
dialog.StartingLocation, err = storage.ListerForURI(storage.NewURI("file:///some/file/that/does/not/exist"))
dialog.StartingLocation, err = storage.ListerForURI(storage.NewFileURI("/some/file/that/does/not/exist"))
if err == nil {
t.Errorf("Should have failed to create lister for nonexistant file")
}
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestFileFilters(t *testing.T) {
fyne.LogError("Could not get current working directory", err)
t.FailNow()
}
testDataDir := storage.NewURI("file://" + filepath.Join(workingDir, "testdata"))
testDataDir := storage.NewFileURI(filepath.Join(workingDir, "testdata"))
testDataLister, err := storage.ListerForURI(testDataDir)
if err != nil {
t.Error(err)
Expand Down
24 changes: 18 additions & 6 deletions storage/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,31 @@ type uri struct {
raw string
}

// NewURI creates a new URI from the given string representation.
// This could be a URI from an external source or one saved from URI.String()
func NewURI(u string) fyne.URI {
func NewFileURI(path string) fyne.URI {
// URIs are supposed to use forward slashes. On Windows, it
// should be OK to use the platform native filepath with UNIX
// or NT sytle paths, with / or \, but when we reconstruct
// or NT style paths, with / or \, but when we reconstruct
// the URI, we want to have / only.
if runtime.GOOS == "windows" {
// seems that sometimes we end up with
// double-backslashes
u = strings.ReplaceAll(u, "\\\\", "/")
u = strings.ReplaceAll(u, "\\", "/")
path = strings.ReplaceAll(path, "\\\\", "/")
path = strings.ReplaceAll(path, "\\", "/")
}
return &uri{raw: "file://" + path}
}

// NewURI creates a new URI from the given string representation.
// This could be a URI from an external source or one saved from URI.String()
func NewURI(u string) fyne.URI {
if len(u) > 5 && u[:5] == "file:" {
path := u[5:]
if len(path) > 2 && path[:2] == "//" {
path = path[2:]
}
return NewFileURI(path)
}

return &uri{raw: u}
}

Expand Down
10 changes: 6 additions & 4 deletions storage/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func TestNewURI_Content(t *testing.T) {
func TestURI_Scheme(t *testing.T) {
assert.Equal(t, "http", storage.NewURI("http://google.com").Scheme())
assert.Equal(t, "http", storage.NewURI("hTtP://google.com").Scheme())
assert.Equal(t, "file", storage.NewURI("file:///home/user/file.txt").Scheme())
assert.Equal(t, "file", storage.NewURI("FILE://C:/autoexec.bat").Scheme())
assert.Equal(t, "file", storage.NewFileURI("/home/user/file.txt").Scheme())
}

func TestURI_Name(t *testing.T) {
assert.Equal(t, "text.txt", storage.NewURI("file:///text.txt").Name())
assert.Equal(t, "library.a", storage.NewURI("file:///library.a").Name())
assert.Equal(t, "image.JPEG", storage.NewURI("file://C:/image.JPEG").Name())
assert.Equal(t, "directory", storage.NewURI("file://C:/directory/").Name())
assert.Equal(t, "image.JPEG", storage.NewFileURI("C:/image.JPEG").Name())
}

func TestURI_Parent(t *testing.T) {
Expand All @@ -55,7 +55,7 @@ func TestURI_Parent(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "file:///foo/bar/", parent.String())

parent, err = storage.Parent(storage.NewURI("file:///foo/bar/baz/"))
parent, err = storage.Parent(storage.NewFileURI("/foo/bar/baz/"))
assert.Nil(t, err)
assert.Equal(t, "file:///foo/bar/", parent.String())

Expand Down Expand Up @@ -86,6 +86,8 @@ func TestURI_Parent(t *testing.T) {
// backslashes.
uri := storage.NewURI("file://C:\\foo\\bar\\baz\\")
assert.Equal(t, "file://C:/foo/bar/baz/", uri.String())
uri = storage.NewFileURI("C:\\foo\\bar\\baz\\")
assert.Equal(t, "file://C:/foo/bar/baz/", uri.String())

parent, err = storage.Parent(uri)
assert.Nil(t, err)
Expand Down Expand Up @@ -114,9 +116,9 @@ func TestURI_Parent(t *testing.T) {

func TestURI_Extension(t *testing.T) {
assert.Equal(t, ".txt", storage.NewURI("file:///text.txt").Extension())
assert.Equal(t, ".a", storage.NewURI("file:///library.a").Extension())
assert.Equal(t, ".JPEG", storage.NewURI("file://C:/image.JPEG").Extension())
assert.Equal(t, "", storage.NewURI("file://C:/directory/").Extension())
assert.Equal(t, ".a", storage.NewFileURI("/library.a").Extension())
}

func TestURI_Child(t *testing.T) {
Expand Down

0 comments on commit d3d7f45

Please sign in to comment.