Skip to content
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

Fixed desktop plugin implementation #542

Merged
merged 1 commit into from
Dec 10, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)).

Expand Down
72 changes: 61 additions & 11 deletions go/plugin.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
Expand All @@ -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

Expand All @@ -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
}
9 changes: 7 additions & 2 deletions lib/src/file_picker_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
miguelpruivo marked this conversation as resolved.
Show resolved Hide resolved
'miguelruivo.flutter.plugins.filepicker',
Platform.isLinux || Platform.isWindows || Platform.isMacOS
? const JSONMethodCodec()
: const StandardMethodCodec(),
);

const EventChannel _eventChannel =
EventChannel('miguelruivo.flutter.plugins.filepickerevent');

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down