diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e95e0e3..8384220b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.1.2 +Fixed desktop plugin implementation + ## 2.1.1 iOS: Fixes an issue that could result in a crash when selecting a media item twice. ([#518](https://github.com/miguelpruivo/flutter_file_picker/issues/518)). diff --git a/go/plugin.go b/go/plugin.go index a9dba4d5..67f7b0e5 100755 --- a/go/plugin.go +++ b/go/plugin.go @@ -1,10 +1,13 @@ package file_picker import ( + "encoding/json" "github.com/gen2brain/dlgs" "github.com/go-flutter-desktop/go-flutter" "github.com/go-flutter-desktop/go-flutter/plugin" "github.com/pkg/errors" + "os" + "path/filepath" ) const channelName = "miguelruivo.flutter.plugins.filepicker" @@ -14,7 +17,7 @@ type FilePickerPlugin struct{} var _ flutter.Plugin = &FilePickerPlugin{} // compile-time type check func (p *FilePickerPlugin) InitPlugin(messenger plugin.BinaryMessenger) error { - channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{}) + channel := plugin.NewMethodChannel(messenger, channelName, plugin.JSONMethodCodec{}) channel.CatchAllHandleFunc(p.handleFilePicker) return nil } @@ -30,7 +33,12 @@ func (p *FilePickerPlugin) handleFilePicker(methodCall interface{}) (reply inter return dirPath, nil } - arguments := methodCall.(plugin.MethodCall).Arguments.(map[interface{}]interface{}) + var arguments map[string]interface{} + + err = json.Unmarshal(methodCall.(plugin.MethodCall).Arguments.(json.RawMessage), &arguments) + if err != nil { + return nil, errors.Wrap(err, "failed to decode arguments") + } var allowedExtensions []string @@ -52,24 +60,66 @@ func (p *FilePickerPlugin) handleFilePicker(methodCall interface{}) (reply inter return nil, errors.Wrap(err, "failed to get filter") } + withData, ok := arguments["withData"].(bool) + + var selectedFilePaths []string + if selectMultiple { filePaths, _, err := dlgs.FileMulti("Select one or more files", filter) if err != nil { return nil, errors.Wrap(err, "failed to open dialog picker") } - // type []string is not supported by StandardMessageCodec - sliceFilePaths := make([]interface{}, len(filePaths)) - for i, file := range filePaths { - sliceFilePaths[i] = file + selectedFilePaths = make([]string, len(filePaths)) + + for i, filePath := range filePaths { + selectedFilePaths[i] = filePath } + } else { + selectedFilePaths = make([]string, 1) - return sliceFilePaths, nil + filePath, err := fileDialog("Select a file", filter) + if err != nil { + return nil, errors.Wrap(err, "failed to open dialog picker") + } + + selectedFilePaths[0] = filePath } - filePath, err := fileDialog("Select a file", filter) - if err != nil { - return nil, errors.Wrap(err, "failed to open dialog picker") + result := make([]map[string]interface{}, len(selectedFilePaths)) + + for i, filePath := range selectedFilePaths { + file, err := os.Open(filePath) + if err != nil { + return nil, errors.Wrap(err, "Can't open selected file") + } + + fi, err := file.Stat() + if err != nil { + return nil, errors.Wrap(err, "Can't open selected file") + } + + var bytes []byte + + if withData { + _, err := file.Read(bytes) + if err != nil { + return nil, errors.Wrap(err, "Can't read selected file") + } + } + + result[i] = map[string]interface{}{ + "path": filePath, + "name": filepath.Base(filePath), + "bytes": bytes, + "size": fi.Size(), + } + + err = file.Close() + if err != nil { + return nil, errors.Wrap(err, "Can't close selected file after reading") + } } - return filePath, nil + + return result, nil } diff --git a/lib/src/file_picker_io.dart b/lib/src/file_picker_io.dart index 11d170e2..acc9b1f2 100644 --- a/lib/src/file_picker_io.dart +++ b/lib/src/file_picker_io.dart @@ -8,8 +8,13 @@ import 'package:flutter/services.dart'; import 'file_picker_result.dart'; -const MethodChannel _channel = - MethodChannel('miguelruivo.flutter.plugins.filepicker'); +final MethodChannel _channel = MethodChannel( + 'miguelruivo.flutter.plugins.filepicker', + Platform.isLinux || Platform.isWindows || Platform.isMacOS + ? const JSONMethodCodec() + : const StandardMethodCodec(), +); + const EventChannel _eventChannel = EventChannel('miguelruivo.flutter.plugins.filepickerevent'); diff --git a/pubspec.yaml b/pubspec.yaml index 19222886..4a913b18 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: file_picker description: A package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extension filtering support. homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker -version: 2.1.1 +version: 2.1.2 dependencies: flutter: