Skip to content

Commit

Permalink
utils/config: add DownloadsDir()
Browse files Browse the repository at this point in the history
  • Loading branch information
benma committed Nov 27, 2018
1 parent e13a2a0 commit bea7b1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
13 changes: 5 additions & 8 deletions backend/coins/btc/handlers/handlers.go
Expand Up @@ -21,7 +21,6 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
Expand All @@ -35,6 +34,7 @@ import (
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/eth/types"
"github.com/digitalbitbox/bitbox-wallet-app/backend/keystore"
"github.com/digitalbitbox/bitbox-wallet-app/util/config"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -202,14 +202,11 @@ func (handlers *Handlers) getAccountTransactions(_ *http.Request) (interface{},

func (handlers *Handlers) postExportTransactions(_ *http.Request) (interface{}, error) {
name := time.Now().Format("2006-01-02-at-15-04-05-") + handlers.account.Code() + "-export.csv"
homeFolder := os.Getenv("HOME")
if runtime.GOOS == "windows" && homeFolder == "" {
homeFolder = os.Getenv("USERPROFILE")
if homeFolder == "" {
return nil, errp.New("can't find home directory")
}
downloadsDir, err := config.DownloadsDir()
if err != nil {
return nil, err
}
path := filepath.Join(homeFolder, "Downloads", name)
path := filepath.Join(downloadsDir, name)
handlers.log.Infof("Export transactions to %s.", path)

file, err := os.Create(path)
Expand Down
14 changes: 14 additions & 0 deletions util/config/appdir.go
Expand Up @@ -19,6 +19,8 @@ import (
"path/filepath"
"runtime"
"sync"

"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
)

// appFolder is what AppDir always returns. It is initialized only once
Expand Down Expand Up @@ -71,3 +73,15 @@ func AppDir() string {
initAppFolderOnce.Do(initAppFolder)
return appFolder
}

// DownloadsDir returns the absolute path to the Downloads folder in the home folder.
func DownloadsDir() (string, error) {
homeFolder := os.Getenv("HOME")
if runtime.GOOS == "windows" && homeFolder == "" {
homeFolder = os.Getenv("USERPROFILE")
}
if homeFolder == "" {
return "", errp.New("can't find home directory")
}
return filepath.Join(homeFolder, "Downloads"), nil
}

0 comments on commit bea7b1a

Please sign in to comment.