Skip to content

Commit ad85056

Browse files
authored
Merge pull request #44 from jld3103/update_path_provider
Update path_provider
2 parents a95ac83 + 13b9a6f commit ad85056

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

path_provider/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/go-flutter-desktop/plugins/path_provider
33
go 1.13
44

55
require (
6+
github.com/adrg/xdg v0.2.1
67
github.com/go-flutter-desktop/go-flutter v0.37.0
78
github.com/pkg/errors v0.9.1
89
)

path_provider/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/adrg/xdg v0.2.1 h1:VSVdnH7cQ7V+B33qSJHTCRlNgra1607Q8PzEmnvb2Ic=
2+
github.com/adrg/xdg v0.2.1/go.mod h1:ZuOshBmzV4Ta+s23hdfFZnBsdzmoR3US0d7ErpqSbTQ=
13
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
35
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

path_provider/plugin.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package path_provider
22

33
import (
4-
"os"
54
"path/filepath"
65

6+
"github.com/adrg/xdg"
7+
8+
"github.com/pkg/errors"
9+
710
flutter "github.com/go-flutter-desktop/go-flutter"
811
"github.com/go-flutter-desktop/go-flutter/plugin"
9-
"github.com/pkg/errors"
1012
)
1113

1214
const channelName = "plugins.flutter.io/path_provider"
@@ -21,8 +23,6 @@ type PathProviderPlugin struct {
2123
// this application. Note that the value must be valid as a cross-platform
2224
// directory name.
2325
ApplicationName string
24-
25-
userConfigFolder string
2626
}
2727

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

41-
var err error
42-
p.userConfigFolder, err = os.UserConfigDir()
43-
if err != nil {
44-
return errors.Wrap(err, "failed to resolve user config dir")
45-
}
46-
4741
channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{})
4842
channel.HandleFunc("getTemporaryDirectory", p.handleTempDir)
49-
channel.HandleFunc("getApplicationDocumentsDirectory", p.handleAppDir)
43+
channel.HandleFunc("getApplicationSupportDirectory", p.handleAppSupportDir)
44+
channel.HandleFunc("getLibraryDirectory", p.handleLibraryDir) // MacOS only
45+
channel.HandleFunc("getApplicationDocumentsDirectory", p.handleAppDocumentsDir)
46+
channel.HandleFunc("getStorageDirectory", p.returnError) // Android only
47+
channel.HandleFunc("getExternalCacheDirectories", p.returnError) // Android only
48+
channel.HandleFunc("getExternalStorageDirectories", p.returnError) // Android only
49+
channel.HandleFunc("getDownloadsDirectory", p.handleDownloadsDir)
5050
return nil
5151
}
5252

53+
func (p *PathProviderPlugin) returnError(arguments interface{}) (reply interface{}, err error) {
54+
return nil, errors.New("This channel is not supported")
55+
}
56+
5357
func (p *PathProviderPlugin) handleTempDir(arguments interface{}) (reply interface{}, err error) {
54-
cacheDir, err := os.UserCacheDir()
55-
if err != nil {
56-
return nil, err
57-
}
58-
return filepath.Join(cacheDir, p.VendorName, p.ApplicationName), nil
58+
return filepath.Join(xdg.CacheHome, p.VendorName, p.ApplicationName), nil
59+
}
60+
61+
func (p *PathProviderPlugin) handleAppSupportDir(arguments interface{}) (reply interface{}, err error) {
62+
return filepath.Join(xdg.DataHome, p.VendorName, p.ApplicationName), nil
63+
}
64+
65+
// handleLibraryDir is MacOS only and therefore hardcoded, as it is not specified in the XDG specifications
66+
func (p *PathProviderPlugin) handleLibraryDir(arguments interface{}) (reply interface{}, err error) {
67+
return "/Library/", nil
68+
}
69+
70+
func (p *PathProviderPlugin) handleAppDocumentsDir(arguments interface{}) (reply interface{}, err error) {
71+
return filepath.Join(xdg.ConfigHome, p.VendorName, p.ApplicationName), nil
5972
}
6073

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

0 commit comments

Comments
 (0)