2020-09-07 19:06:18 +00:00
import ' dart:async ' ;
2020-11-12 17:28:57 +00:00
import ' dart:io ' ;
2020-09-07 19:06:18 +00:00
import ' package:file_picker/file_picker.dart ' ;
import ' package:file_picker/src/platform_file.dart ' ;
import ' package:flutter/foundation.dart ' ;
import ' package:flutter/services.dart ' ;
import ' file_picker_result.dart ' ;
2020-12-10 19:09:13 +00:00
final MethodChannel _channel = MethodChannel (
' miguelruivo.flutter.plugins.filepicker ' ,
Platform . isLinux | | Platform . isWindows | | Platform . isMacOS
? const JSONMethodCodec ( )
: const StandardMethodCodec ( ) ,
) ;
2020-09-11 17:37:45 +00:00
const EventChannel _eventChannel =
EventChannel ( ' miguelruivo.flutter.plugins.filepickerevent ' ) ;
2020-09-07 19:06:18 +00:00
/// An implementation of [FilePicker] that uses method channels.
class FilePickerIO extends FilePicker {
static const String _tag = ' MethodChannelFilePicker ' ;
static StreamSubscription _eventSubscription ;
@ override
Future < FilePickerResult > pickFiles ( {
FileType type = FileType . any ,
List < String > allowedExtensions ,
Function ( FilePickerStatus ) onFileLoading ,
2020-09-11 17:01:34 +00:00
bool allowCompression = true ,
2020-09-07 19:06:18 +00:00
bool allowMultiple = false ,
2020-09-11 17:01:34 +00:00
bool withData = false ,
2020-11-12 17:28:57 +00:00
bool withReadStream = false ,
2020-09-07 19:06:18 +00:00
} ) = >
2020-09-11 17:01:34 +00:00
_getPath (
type ,
allowMultiple ,
allowCompression ,
allowedExtensions ,
onFileLoading ,
withData ,
2020-11-12 17:28:57 +00:00
withReadStream ,
2020-09-11 17:01:34 +00:00
) ;
2020-09-07 19:06:18 +00:00
@ override
2020-09-11 17:37:45 +00:00
Future < bool > clearTemporaryFiles ( ) async = >
_channel . invokeMethod < bool > ( ' clear ' ) ;
2020-09-07 19:06:18 +00:00
@ override
2020-09-11 13:53:18 +00:00
Future < String > getDirectoryPath ( ) async {
2020-09-07 19:06:18 +00:00
try {
2020-09-11 13:53:18 +00:00
return await _channel . invokeMethod ( ' dir ' , { } ) ;
2020-09-07 19:06:18 +00:00
} on PlatformException catch ( ex ) {
if ( ex . code = = " unknown_path " ) {
print (
' [ $ _tag ] Could not resolve directory path. Maybe it \' s a protected one or unsupported (such as Downloads folder). If you are on Android, make sure that you are on SDK 21 or above. ' ) ;
}
}
return null ;
}
Future < FilePickerResult > _getPath (
FileType fileType ,
bool allowMultipleSelection ,
bool allowCompression ,
List < String > allowedExtensions ,
Function ( FilePickerStatus ) onFileLoading ,
2020-09-11 17:01:34 +00:00
bool withData ,
2020-11-12 17:28:57 +00:00
bool withReadStream ,
2020-09-07 19:06:18 +00:00
) async {
final String type = describeEnum ( fileType ) ;
if ( type ! = ' custom ' & & ( allowedExtensions ? . isNotEmpty ? ? false ) ) {
2020-09-11 17:37:45 +00:00
throw Exception (
' If you are using a custom extension filter, please use the FileType.custom instead. ' ) ;
2020-09-07 19:06:18 +00:00
}
try {
_eventSubscription ? . cancel ( ) ;
if ( onFileLoading ! = null ) {
_eventSubscription = _eventChannel . receiveBroadcastStream ( ) . listen (
2020-09-11 17:37:45 +00:00
( data ) = > onFileLoading ( ( data as bool )
? FilePickerStatus . picking
: FilePickerStatus . done ) ,
2020-09-07 19:06:18 +00:00
onError: ( error ) = > throw Exception ( error ) ,
) ;
}
2020-09-09 23:52:45 +00:00
final List < Map > result = await _channel . invokeListMethod ( type , {
2020-09-07 19:06:18 +00:00
' allowMultipleSelection ' : allowMultipleSelection ,
' allowedExtensions ' : allowedExtensions ,
' allowCompression ' : allowCompression ,
2020-09-11 17:01:34 +00:00
' withData ' : withData ,
2020-09-07 19:06:18 +00:00
} ) ;
if ( result = = null ) {
return null ;
}
2020-11-12 17:28:57 +00:00
final List < PlatformFile > platformFiles = < PlatformFile > [ ] ;
for ( final platformFileMap in result ) {
platformFiles . add (
PlatformFile . fromMap (
platformFileMap ,
readStream: withReadStream
? File ( platformFileMap [ ' path ' ] ) . openRead ( )
: null ,
) ,
) ;
}
return FilePickerResult ( platformFiles ) ;
2020-09-07 19:06:18 +00:00
} on PlatformException catch ( e ) {
print ( ' [ $ _tag ] Platform exception: $ e ' ) ;
rethrow ;
} catch ( e ) {
2020-09-11 17:37:45 +00:00
print (
' [ $ _tag ] Unsupported operation. Method not found. The exception thrown was: $ e ' ) ;
2020-09-07 19:06:18 +00:00
rethrow ;
}
}
}