1
1
package path_provider
2
2
3
3
import (
4
- "os"
5
4
"path/filepath"
6
5
6
+ "github.com/adrg/xdg"
7
+
8
+ "github.com/pkg/errors"
9
+
7
10
flutter "github.com/go-flutter-desktop/go-flutter"
8
11
"github.com/go-flutter-desktop/go-flutter/plugin"
9
- "github.com/pkg/errors"
10
12
)
11
13
12
14
const channelName = "plugins.flutter.io/path_provider"
@@ -21,8 +23,6 @@ type PathProviderPlugin struct {
21
23
// this application. Note that the value must be valid as a cross-platform
22
24
// directory name.
23
25
ApplicationName string
24
-
25
- userConfigFolder string
26
26
}
27
27
28
28
var _ flutter.Plugin = & PathProviderPlugin {} // compile-time type check
@@ -38,26 +38,42 @@ func (p *PathProviderPlugin) InitPlugin(messenger plugin.BinaryMessenger) error
38
38
return errors .New ("PathProviderPlugin.ApplicationName must be set" )
39
39
}
40
40
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
-
47
41
channel := plugin .NewMethodChannel (messenger , channelName , plugin.StandardMethodCodec {})
48
42
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 )
50
50
return nil
51
51
}
52
52
53
+ func (p * PathProviderPlugin ) returnError (arguments interface {}) (reply interface {}, err error ) {
54
+ return nil , errors .New ("This channel is not supported" )
55
+ }
56
+
53
57
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
59
72
}
60
73
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
63
79
}
0 commit comments