Skip to content

Update path_provider #44

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

Merged
merged 2 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
1 change: 1 addition & 0 deletions path_provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/go-flutter-desktop/plugins/path_provider
go 1.13

require (
github.com/adrg/xdg v0.2.1
github.com/go-flutter-desktop/go-flutter v0.37.0
github.com/pkg/errors v0.9.1
)
2 changes: 2 additions & 0 deletions path_provider/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/adrg/xdg v0.2.1 h1:VSVdnH7cQ7V+B33qSJHTCRlNgra1607Q8PzEmnvb2Ic=
github.com/adrg/xdg v0.2.1/go.mod h1:ZuOshBmzV4Ta+s23hdfFZnBsdzmoR3US0d7ErpqSbTQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
52 changes: 34 additions & 18 deletions path_provider/plugin.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package path_provider

import (
"os"
"path/filepath"

"github.com/adrg/xdg"

"github.com/pkg/errors"

flutter "github.com/go-flutter-desktop/go-flutter"
"github.com/go-flutter-desktop/go-flutter/plugin"
"github.com/pkg/errors"
)

const channelName = "plugins.flutter.io/path_provider"
Expand All @@ -21,8 +23,6 @@ type PathProviderPlugin struct {
// this application. Note that the value must be valid as a cross-platform
// directory name.
ApplicationName string

userConfigFolder string
}

var _ flutter.Plugin = &PathProviderPlugin{} // compile-time type check
Expand All @@ -38,26 +38,42 @@ func (p *PathProviderPlugin) InitPlugin(messenger plugin.BinaryMessenger) error
return errors.New("PathProviderPlugin.ApplicationName must be set")
}

var err error
p.userConfigFolder, err = os.UserConfigDir()
if err != nil {
return errors.Wrap(err, "failed to resolve user config dir")
}

channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{})
channel.HandleFunc("getTemporaryDirectory", p.handleTempDir)
channel.HandleFunc("getApplicationDocumentsDirectory", p.handleAppDir)
channel.HandleFunc("getApplicationSupportDirectory", p.handleAppSupportDir)
channel.HandleFunc("getLibraryDirectory", p.handleLibraryDir) // MacOS only
channel.HandleFunc("getApplicationDocumentsDirectory", p.handleAppDocumentsDir)
channel.HandleFunc("getStorageDirectory", p.returnError) // Android only
channel.HandleFunc("getExternalCacheDirectories", p.returnError) // Android only
channel.HandleFunc("getExternalStorageDirectories", p.returnError) // Android only
channel.HandleFunc("getDownloadsDirectory", p.handleDownloadsDir)
return nil
}

func (p *PathProviderPlugin) returnError(arguments interface{}) (reply interface{}, err error) {
return nil, errors.New("This channel is not supported")
}

func (p *PathProviderPlugin) handleTempDir(arguments interface{}) (reply interface{}, err error) {
cacheDir, err := os.UserCacheDir()
if err != nil {
return nil, err
}
return filepath.Join(cacheDir, p.VendorName, p.ApplicationName), nil
return filepath.Join(xdg.CacheHome, p.VendorName, p.ApplicationName), nil
}

func (p *PathProviderPlugin) handleAppSupportDir(arguments interface{}) (reply interface{}, err error) {
return filepath.Join(xdg.DataHome, p.VendorName, p.ApplicationName), nil
}

// handleLibraryDir is MacOS only and therefore hardcoded, as it is not specified in the XDG specifications
func (p *PathProviderPlugin) handleLibraryDir(arguments interface{}) (reply interface{}, err error) {
return "/Library/", nil
}

func (p *PathProviderPlugin) handleAppDocumentsDir(arguments interface{}) (reply interface{}, err error) {
return filepath.Join(xdg.ConfigHome, p.VendorName, p.ApplicationName), nil
}

func (p *PathProviderPlugin) handleAppDir(arguments interface{}) (reply interface{}, err error) {
return filepath.Join(p.userConfigFolder, p.VendorName, p.ApplicationName), nil
// handleDownloadsDir is from the flutter plugin side MacOS only
// (https://github.com/flutter/plugins/blob/8819b219c5ca83a000ae482b9a51b7f1f421845b/packages/path_provider/path_provider_platform_interface/lib/src/method_channel_path_provider.dart#L82)
// but should work out of the box once the restriction is not longer there
func (p *PathProviderPlugin) handleDownloadsDir(arguments interface{}) (reply interface{}, err error) {
return xdg.UserDirs.Download, nil
}