adding packages
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import 'package:file_access/file_access.dart';
|
||||
import 'package:file_access/src/constants.dart';
|
||||
import 'package:file_chooser/file_chooser.dart';
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
Future<FileX> openFile() async {
|
||||
final _files = await _open(false, false);
|
||||
if (_files == null) return null;
|
||||
return _files.first;
|
||||
}
|
||||
|
||||
Future<List<FileX>> openFiles() async {
|
||||
final _files = await _open(true, false);
|
||||
if (_files == null) return null;
|
||||
return _files;
|
||||
}
|
||||
|
||||
Future<FileX> pickImage() async {
|
||||
if (Platform.isIOS || Platform.isAndroid) {
|
||||
var selection = await ImagePicker.pickImage(source: ImageSource.gallery);
|
||||
return _add(selection);
|
||||
}
|
||||
final _files = await _open(false, false, allowedTypes: [
|
||||
FileTypeFilterGroup(
|
||||
label: 'image',
|
||||
fileExtensions: kImageExtensions,
|
||||
),
|
||||
]);
|
||||
if (_files == null || _files.isEmpty) return null;
|
||||
return _files.first;
|
||||
}
|
||||
|
||||
Future<FileX> pickVideo() async {
|
||||
if (Platform.isIOS || Platform.isAndroid) {
|
||||
var selection = await ImagePicker.pickVideo(source: ImageSource.gallery);
|
||||
return _add(selection);
|
||||
}
|
||||
final _files = await _open(false, false, allowedTypes: [
|
||||
FileTypeFilterGroup(
|
||||
label: 'video',
|
||||
fileExtensions: kVideoExtensions,
|
||||
),
|
||||
]);
|
||||
if (_files == null || _files.isEmpty) return null;
|
||||
return _files.first;
|
||||
}
|
||||
|
||||
Future<List<FileX>> _open(bool multiple, bool folders,
|
||||
{List<FileTypeFilterGroup> allowedTypes}) async {
|
||||
if (Platform.isMacOS || Platform.isLinux || Platform.isWindows) {
|
||||
final results = await showOpenPanel(
|
||||
allowedFileTypes: allowedTypes,
|
||||
canSelectDirectories: folders,
|
||||
allowsMultipleSelection: multiple,
|
||||
);
|
||||
if (results.canceled) return null;
|
||||
final List<FileX> _files = [];
|
||||
for (final item in results.paths) {
|
||||
_files.add(await _add(File(item)));
|
||||
}
|
||||
return _files;
|
||||
}
|
||||
if (Platform.isIOS || Platform.isAndroid) {
|
||||
final List<FileX> _files = [];
|
||||
if (multiple) {
|
||||
List<File> files = await FilePicker.getMultiFile();
|
||||
if (files == null) return null;
|
||||
for (final file in files) {
|
||||
_files.add(await _add(file));
|
||||
}
|
||||
} else {
|
||||
File file = await FilePicker.getFile();
|
||||
if (file == null) return null;
|
||||
_files.add(await _add(file));
|
||||
}
|
||||
return _files;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<FileX> _add(File file) async {
|
||||
try {
|
||||
final _base = FileX(file.path);
|
||||
final _bytes = await file.readAsBytes();
|
||||
return await _base.writeAsBytes(_bytes);
|
||||
} catch (e) {}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export 'unsupported.dart'
|
||||
if (dart.library.html) 'web.dart'
|
||||
if (dart.library.io) 'io.dart';
|
||||
@@ -0,0 +1,17 @@
|
||||
import 'package:file_access/file_access.dart';
|
||||
|
||||
Future<FileX> openFile() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future<List<FileX>> openFiles() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future<FileX> pickImage() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future<FileX> pickVideo() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:file_access/file_access.dart';
|
||||
|
||||
import 'dart:html' as html;
|
||||
|
||||
Future<FileX> pickImage() async {
|
||||
final _files = await _open(false, false, 'image/*');
|
||||
if (_files == null || _files.isEmpty) return null;
|
||||
return _files.first;
|
||||
}
|
||||
|
||||
Future<FileX> pickVideo() async {
|
||||
final _files = await _open(false, false, 'video/*');
|
||||
if (_files == null || _files.isEmpty) return null;
|
||||
return _files.first;
|
||||
}
|
||||
|
||||
Future<FileX> pickAudio() async {
|
||||
final _files = await _open(false, false, 'audio/*');
|
||||
if (_files == null || _files.isEmpty) return null;
|
||||
return _files.first;
|
||||
}
|
||||
|
||||
Future<FileX> openFile() async {
|
||||
final _files = await _open(false, false);
|
||||
if (_files == null || _files.isEmpty) return null;
|
||||
return _files.first;
|
||||
}
|
||||
|
||||
Future<List<FileX>> openFiles() async {
|
||||
final _files = await _open(true, false);
|
||||
if (_files == null) return null;
|
||||
return _files;
|
||||
}
|
||||
|
||||
Future<List<FileX>> _open(bool multiple, bool folders,
|
||||
[String types = '*']) async {
|
||||
html.InputElement _filePicker = html.querySelector('#file-picker');
|
||||
final _upload = _filePicker ?? html.FileUploadInputElement();
|
||||
_upload.accept = types;
|
||||
_upload.multiple = multiple;
|
||||
if (folders) {
|
||||
_upload.setAttribute('webkitdirectory', '');
|
||||
_upload.setAttribute('mozdirectory', '');
|
||||
}
|
||||
_upload.click();
|
||||
final _file = await _upload.onChange.first;
|
||||
if (_file == null) return null;
|
||||
List<html.File> files = (_file.target as dynamic).files;
|
||||
final List<FileX> _files = [];
|
||||
for (final f in files) {
|
||||
try {
|
||||
final _path = folders ? f.relativePath : f.name;
|
||||
final _base = FileX(_path);
|
||||
final reader = new html.FileReader();
|
||||
reader.readAsArrayBuffer(f);
|
||||
final _loaded = await _listen(reader.onLoad);
|
||||
if (!_loaded) continue;
|
||||
final _bytes = reader.result as List<int>;
|
||||
final _newFile = await _base.writeAsBytes(_bytes);
|
||||
_files.add(_newFile);
|
||||
} catch (e) {}
|
||||
}
|
||||
return _files;
|
||||
}
|
||||
|
||||
Future<bool> _listen(Stream<html.ProgressEvent> stream) async {
|
||||
try {
|
||||
await for (var value in stream) {
|
||||
if (value.total == value.loaded) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user