Skip to content

Commit

Permalink
specialize all the file urls
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Sep 26, 2022
1 parent abfc590 commit 60973af
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
15 changes: 15 additions & 0 deletions client/cmd/dexc/fileurl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build systray && !darwin && !windows

package main

import (
"path/filepath"
)

func filePathToURL(name string) (string, error) {
path, err := filepath.Abs(name)
if err != nil { // can't pwd if name was relative, probably impossible
return "", err
}
return "file://" + path, nil
}
24 changes: 24 additions & 0 deletions client/cmd/dexc/fileurl_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build systray && darwin

package main

import (
"net/url"
"path/filepath"
)

// Darwin appears to need paths pre-escaped.

func filePathToURL(name string) (string, error) {
path, err := filepath.Abs(name)
if err != nil { // can't pwd if name was relative, probably impossible
return "", err
}
fileURL, err := url.Parse("file://" + path)
if err != nil {
return "", err
}
// url.Parse can be touchy, so consider replacing only spaces, manually:
// path = strings.ReplaceAll(path, " ", "%20")
return fileURL.String(), nil
}
20 changes: 20 additions & 0 deletions client/cmd/dexc/fileurl_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build systray && windows

package main

import (
"path/filepath"
)

// Windows requires a leading "/" before the "C:" of an absolute path, and
// slashes converted to forward slashes.
// https://en.wikipedia.org/wiki/File_URI_scheme#Windows

func filePathToURL(name string) (string, error) {
path, err := filepath.Abs(name)
if err != nil { // can't pwd if name was relative, probably impossible
return "", err
}
path = filepath.ToSlash(path)
return "file:///" + path, nil
}
14 changes: 0 additions & 14 deletions client/cmd/dexc/main_tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,14 @@ package main

import (
"fmt"
"net/url"
"os"
"path/filepath"

"fyne.io/systray"
"github.com/pkg/browser"
)

var mainDone = make(chan struct{})

func filePathToURL(name string) (string, error) {
path, err := filepath.Abs(name)
if err != nil { // can't pwd if name was relative, probably impossible
return "", err
}
fileURL, err := url.Parse("file://" + path)
if err != nil {
return "", err
}
return fileURL.String(), nil
}

func onReady() {
go func() {
defer close(mainDone)
Expand Down

0 comments on commit 60973af

Please sign in to comment.