Skip to content

Commit

Permalink
OS Related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xssnick committed Sep 11, 2023
1 parent 691cd7c commit 992b071
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 42 deletions.
54 changes: 28 additions & 26 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -45,6 +44,20 @@ func NewApp() *App {
oshook.HookStartup(a.openFile, a.openHash)
adnl.Logger = func(v ...any) {}
storage.Logger = log.Println

var err error
a.rootPath, err = PrepareRootPath()
if err != nil {
a.Throw(err)
}

cfg, err := LoadConfig(a.rootPath)
if err != nil {
a.Throw(err)
}

a.config = cfg

return a
}

Expand Down Expand Up @@ -96,30 +109,14 @@ func (a *App) ShowWarnMsg(text string) {
func (a *App) prepare() {
oshook.HookStartup(a.openFile, a.openHash)

var err error
a.rootPath, err = PrepareRootPath()
if err != nil {
a.Throw(err)
}

cfg, err := LoadConfig(a.rootPath)
if err != nil {
a.Throw(err)
}

a.config = cfg

var exPath = CustomRoot // if we have defined root, use it for daemon path too
if exPath == "" {
ex, err := os.Executable()
if err != nil {
a.Throw(err)
}
exPath = filepath.Dir(ex)

if runtime.GOOS == "darwin" {
exPath = filepath.Dir(exPath) + "/Resources/Storage.app/Contents/MacOS"
if !a.config.PortsChecked && !a.config.SeedMode {
ip, seed := CheckCanSeed()
if seed {
a.config.SeedMode = true
a.config.ListenAddr = ip + ":13333"
}
a.config.PortsChecked = true
_ = a.config.SaveConfig(a.rootPath)
}

/*go func() {
Expand Down Expand Up @@ -500,8 +497,10 @@ func (a *App) SaveConfig(downloads string, useTonutilsStorage, seedMode bool, st
func (a *App) OpenFolder(path string) {
var cmd string
switch runtime.GOOS {
case "darwin", "linux":
case "darwin":
cmd = "open"
case "linux":
cmd = "xdg-open"
case "windows":
path = strings.ReplaceAll(path, "/", "\\")
cmd = "explorer"
Expand All @@ -512,8 +511,11 @@ func (a *App) OpenFolder(path string) {

func (a *App) OpenFolderSelectFile(path string) {
switch runtime.GOOS {
case "darwin", "linux":
case "darwin":
exec.Command("open", "-R", path).Start()
case "linux":
// TODO: select file somehow
exec.Command("xdg-open", path).Start()
case "windows":
path = strings.ReplaceAll(path, "/", "\\")
exec.Command("explorer", "/select,"+path).Start()
Expand Down
2 changes: 1 addition & 1 deletion build/windows/installer/project.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Unicode true
## !define INFO_PROJECTNAME "MyProject" # Default "{{.Name}}"
## !define INFO_COMPANYNAME "MyCompany" # Default "{{.Info.CompanyName}}"
## !define INFO_PRODUCTNAME "MyProduct" # Default "{{.Info.ProductName}}"
## !define INFO_PRODUCTVERSION "1.0.0" # Default "{{.Info.ProductVersion}}"
## !define INFO_PRODUCTVERSION "1.0.1" # Default "{{.Info.ProductVersion}}"
## !define INFO_COPYRIGHT "Copyright" # Default "{{.Info.Copyright}}"
###
## !define PRODUCT_EXECUTABLE "Application.exe" # Default "${INFO_PROJECTNAME}.exe"
Expand Down
15 changes: 8 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"runtime"
"sync"
"time"
)

Expand All @@ -21,6 +22,9 @@ type Config struct {
UseDaemon bool
DaemonDBPath string
DaemonControlAddr string
PortsChecked bool

mx sync.Mutex
}

func LoadConfig(dir string) (*Config, error) {
Expand All @@ -40,12 +44,6 @@ func LoadConfig(dir string) (*Config, error) {
UseDaemon: false,
}

ip, seed := checkCanSeed()
if seed {
cfg.SeedMode = true
cfg.ListenAddr = ip + ":13333"
}

err = cfg.SaveConfig(dir)
if err != nil {
return nil, err
Expand Down Expand Up @@ -79,6 +77,9 @@ func LoadConfig(dir string) (*Config, error) {
}

func (cfg *Config) SaveConfig(dir string) error {
cfg.mx.Lock()
defer cfg.mx.Unlock()

path := dir + "/config.json"

data, err := json.MarshalIndent(cfg, "", "\t")
Expand Down Expand Up @@ -121,7 +122,7 @@ func checkIPAddress(ip string) string {
return p.String()
}

func checkCanSeed() (string, bool) {
func CheckCanSeed() (string, bool) {
ch := make(chan bool, 1)

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ModalSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class SettingsModal extends Component<SettingsModalProps, State> {
</button>
</div>
<div className="modal-version">
<span className="version">Version 1.0.0</span>
<span className="version">Version 1.0.1</span>
<span className="check" onClick={()=>{
BrowserOpenURL("https://github.com/xssnick/TON-Torrent/releases")
}}>Check updates</span>
Expand Down
14 changes: 7 additions & 7 deletions oshook/hook_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ func OnLoadFile(data *C.char, length C.uint) {
//export OnLoadURL
func OnLoadURL(u *C.char) {
if cbHash != nil {
go func() { // to not block main thread
ul, err := url.Parse(C.GoString(u))
if err == nil {
cbHash(ul.Host)
}
}()
ul, err := url.Parse(C.GoString(u))
if err == nil {
// to not block main thread
go cbHash(ul.Host)
}
}
}

//export OnLoadFileFromPath
func OnLoadFileFromPath(path *C.char) {
if cbFile != nil {
pt := C.GoString(path)
go func() { // to not block main thread
data, err := os.ReadFile(C.GoString(path))
data, err := os.ReadFile(pt)
if err == nil {
cbFile(data)
}
Expand Down

0 comments on commit 992b071

Please sign in to comment.