Skip to content

Commit

Permalink
feat-add-mime-type
Browse files Browse the repository at this point in the history
  • Loading branch information
EArminjon committed Jan 14, 2025
1 parent 36425c7 commit 2e8bfff
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void startFileExplorer(final String type, final boolean isMultipleSelecti
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void saveFile(String fileName, String type, String initialDirectory, String[] allowedExtensions, byte[] bytes, MethodChannel.Result result) {
public void saveFile(String fileName, String type, String mimeType, String initialDirectory, String[] allowedExtensions, byte[] bytes, MethodChannel.Result result) {
if (!this.setPendingMethodCallAndResult(result)) {
finishWithAlreadyActiveError(result);
return;
Expand All @@ -301,7 +301,9 @@ public void saveFile(String fileName, String type, String initialDirectory, Stri
intent.putExtra(Intent.EXTRA_TITLE, fileName);
}
this.bytes = bytes;
if (type != null && !"dir".equals(type) && type.split(",").length == 1) {
if (mimeType != null) {
intent.setType(mimeType);
} else if (type != null && !"dir".equals(type) && type.split(",").length == 1) {
intent.setType(type);
} else {
intent.setType("*/*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ public void onMethodCall(final MethodCall call, final MethodChannel.Result rawRe
if (call.method != null && call.method.equals("save")) {
String fileName = (String) arguments.get("fileName");
String type = resolveType((String) arguments.get("fileType"));
String mimeType = (String) arguments.get("mimeType");
String initialDirectory = (String) arguments.get("initialDirectory");
String[] allowedExtensions = FileUtils.getMimeTypes((ArrayList<String>) arguments.get("allowedExtensions"));
byte[] bytes = (byte[]) arguments.get("bytes");
this.delegate.saveFile(fileName, type, initialDirectory, allowedExtensions, bytes,result);
this.delegate.saveFile(fileName, type, mimeType, initialDirectory, allowedExtensions, bytes,result);
return;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/src/file_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ abstract class FilePicker extends PlatformInterface {
/// stay in front of the Flutter window until it is closed (like a modal
/// window). This parameter works only on Windows desktop.
///
/// [mimeType] can be optionally set to provide a mime type for the file.
/// It only work for iOS and Android.
///
/// Returns `null` if aborted. Returns a [Future<String?>] which resolves to
/// the absolute path of the selected file, if the user selected a file.
Future<String?> saveFile({
Expand All @@ -172,6 +175,7 @@ abstract class FilePicker extends PlatformInterface {
List<String>? allowedExtensions,
Uint8List? bytes,
bool lockParentWindow = false,
String? mimeType,
}) async =>
throw UnimplementedError('saveFile() has not been implemented.');
}
20 changes: 12 additions & 8 deletions lib/src/file_picker_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,20 @@ class FilePickerIO extends FilePicker {
}

@override
Future<String?> saveFile(
{String? dialogTitle,
String? fileName,
String? initialDirectory,
FileType type = FileType.any,
List<String>? allowedExtensions,
Uint8List? bytes,
bool lockParentWindow = false}) {
Future<String?> saveFile({
String? dialogTitle,
String? fileName,
String? initialDirectory,
FileType type = FileType.any,
List<String>? allowedExtensions,
Uint8List? bytes,
bool lockParentWindow = false,
String? mimeType,
}) {
if (Platform.isIOS || Platform.isAndroid) {
return _channel.invokeMethod("save", {
"fileName": fileName,
"mimeType": mimeType,
"fileType": type.name,
"initialDirectory": initialDirectory,
"allowedExtensions": allowedExtensions,
Expand All @@ -162,6 +165,7 @@ class FilePickerIO extends FilePicker {
allowedExtensions: allowedExtensions,
bytes: bytes,
lockParentWindow: lockParentWindow,
mimeType: mimeType,
);
}
}
1 change: 1 addition & 0 deletions lib/src/file_picker_macos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class FilePickerMacOS extends FilePicker {
List<String>? allowedExtensions,
Uint8List? bytes,
bool lockParentWindow = false,
String? mimeType,
}) async {
final String executable = await isExecutableOnPath('osascript');
final String fileFilter = fileTypeToFileFilter(
Expand Down
1 change: 1 addition & 0 deletions lib/src/linux/file_picker_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class FilePickerLinux extends FilePicker {
List<String>? allowedExtensions,
Uint8List? bytes,
bool lockParentWindow = false,
String? mimeType,
}) async {
final executable = await _getPathToExecutable();
final dialogHandler = DialogHandler(executable);
Expand Down
1 change: 1 addition & 0 deletions lib/src/windows/file_picker_windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class FilePickerWindows extends FilePicker {
List<String>? allowedExtensions,
Uint8List? bytes,
bool lockParentWindow = false,
String? mimeType,
}) async {
final port = ReceivePort();
await Isolate.spawn(
Expand Down

0 comments on commit 2e8bfff

Please sign in to comment.