adding packages
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
library file_access;
|
||||
|
||||
export 'src/classes/file/file.dart';
|
||||
export 'src/pick_file/pick_file.dart';
|
||||
@@ -0,0 +1,3 @@
|
||||
export 'unsupported.dart'
|
||||
if (dart.library.html) 'web.dart'
|
||||
if (dart.library.io) 'io.dart';
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'dart:convert';
|
||||
|
||||
abstract class FileXBase {
|
||||
final String path;
|
||||
|
||||
FileXBase(this.path);
|
||||
|
||||
Future<FileXBase> writeAsBytes(List<int> data);
|
||||
|
||||
Future<FileXBase> writeAsString(String data, {Encoding encoding});
|
||||
|
||||
Future<List<int>> readAsBytes();
|
||||
|
||||
Future<String> readAsString();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'impl.dart';
|
||||
|
||||
class FileX extends FileXBase {
|
||||
final String path;
|
||||
File _file;
|
||||
|
||||
FileX(this.path)
|
||||
: _file = File(path),
|
||||
super(path);
|
||||
|
||||
@override
|
||||
Future<FileX> writeAsBytes(List<int> data) async {
|
||||
if (!_file.existsSync()) {
|
||||
_file = await _file.create(recursive: true);
|
||||
}
|
||||
await _file.writeAsBytes(data);
|
||||
return FileX(path);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FileX> writeAsString(String data, {Encoding encoding}) async {
|
||||
if (!_file.existsSync()) {
|
||||
_file = await _file.create(recursive: true);
|
||||
}
|
||||
await _file.writeAsString(data, encoding: encoding);
|
||||
return FileX(path);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<int>> readAsBytes() {
|
||||
return _file.readAsBytes();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> readAsString() {
|
||||
return _file.readAsString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'impl.dart';
|
||||
|
||||
class FileX extends FileXBase {
|
||||
final String path;
|
||||
|
||||
FileX(this.path) : super(path);
|
||||
|
||||
@override
|
||||
Future<FileX> writeAsBytes(List<int> data) {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FileX> writeAsString(String data, {Encoding encoding}) {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<int>> readAsBytes() {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> readAsString() {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'impl.dart';
|
||||
|
||||
class FileX extends FileXBase {
|
||||
final String path;
|
||||
final List<int> _bytes;
|
||||
Encoding _encoding = utf8;
|
||||
|
||||
FileX(this.path, {List<int> bytes = const []})
|
||||
: _bytes = bytes,
|
||||
super(path);
|
||||
|
||||
@override
|
||||
Future<FileX> writeAsBytes(List<int> data) {
|
||||
return Future.value(FileX(path, bytes: data));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FileX> writeAsString(String data, {Encoding encoding}) {
|
||||
List<int> _data;
|
||||
_encoding = encoding ?? utf8;
|
||||
_data = _encoding.encode(data);
|
||||
return writeAsBytes(_data);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<int>> readAsBytes() async {
|
||||
return _bytes;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> readAsString() async {
|
||||
return _encoding.decode(_bytes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
const kImageExtensions = [
|
||||
"ase",
|
||||
"art",
|
||||
"bmp",
|
||||
"blp",
|
||||
"cd5",
|
||||
"cit",
|
||||
"cpt",
|
||||
"cr2",
|
||||
"cut",
|
||||
"dds",
|
||||
"dib",
|
||||
"djvu",
|
||||
"egt",
|
||||
"exif",
|
||||
"gif",
|
||||
"gpl",
|
||||
"grf",
|
||||
"icns",
|
||||
"ico",
|
||||
"iff",
|
||||
"jng",
|
||||
"jpeg",
|
||||
"jpg",
|
||||
"jfif",
|
||||
"jp2",
|
||||
"jps",
|
||||
"lbm",
|
||||
"max",
|
||||
"miff",
|
||||
"mng",
|
||||
"msp",
|
||||
"nitf",
|
||||
"ota",
|
||||
"pbm",
|
||||
"pc1",
|
||||
"pc2",
|
||||
"pc3",
|
||||
"pcf",
|
||||
"pcx",
|
||||
"pdn",
|
||||
"pgm",
|
||||
"PI1",
|
||||
"PI2",
|
||||
"PI3",
|
||||
"pict",
|
||||
"pct",
|
||||
"pnm",
|
||||
"pns",
|
||||
"ppm",
|
||||
"psb",
|
||||
"psd",
|
||||
"pdd",
|
||||
"psp",
|
||||
"px",
|
||||
"pxm",
|
||||
"pxr",
|
||||
"qfx",
|
||||
"raw",
|
||||
"rle",
|
||||
"sct",
|
||||
"sgi",
|
||||
"rgb",
|
||||
"int",
|
||||
"bw",
|
||||
"tga",
|
||||
"tiff",
|
||||
"tif",
|
||||
"vtf",
|
||||
"xbm",
|
||||
"xcf",
|
||||
"xpm",
|
||||
"3dv",
|
||||
"amf",
|
||||
"ai",
|
||||
"awg",
|
||||
"cgm",
|
||||
"cdr",
|
||||
"cmx",
|
||||
"dxf",
|
||||
"e2d",
|
||||
"egt",
|
||||
"eps",
|
||||
"fs",
|
||||
"gbr",
|
||||
"odg",
|
||||
"svg",
|
||||
"stl",
|
||||
"vrml",
|
||||
"x3d",
|
||||
"sxd",
|
||||
"v2d",
|
||||
"vnd",
|
||||
"wmf",
|
||||
"emf",
|
||||
"art",
|
||||
"xar",
|
||||
"png",
|
||||
"webp",
|
||||
"jxr",
|
||||
"hdp",
|
||||
"wdp",
|
||||
"cur",
|
||||
"ecw",
|
||||
"iff",
|
||||
"lbm",
|
||||
"liff",
|
||||
"nrrd",
|
||||
"pam",
|
||||
"pcx",
|
||||
"pgf",
|
||||
"sgi",
|
||||
"rgb",
|
||||
"rgba",
|
||||
"bw",
|
||||
"int",
|
||||
"inta",
|
||||
"sid",
|
||||
"ras",
|
||||
"sun",
|
||||
"tga",
|
||||
];
|
||||
|
||||
const kVideoExtensions = [
|
||||
"3g2",
|
||||
"3gp",
|
||||
"aaf",
|
||||
"asf",
|
||||
"avchd",
|
||||
"avi",
|
||||
"drc",
|
||||
"flv",
|
||||
"m2v",
|
||||
"m4p",
|
||||
"m4v",
|
||||
"mkv",
|
||||
"mng",
|
||||
"mov",
|
||||
"mp2",
|
||||
"mp4",
|
||||
"mpe",
|
||||
"mpeg",
|
||||
"mpg",
|
||||
"mpv",
|
||||
"mxf",
|
||||
"nsv",
|
||||
"ogg",
|
||||
"ogv",
|
||||
"qt",
|
||||
"rm",
|
||||
"rmvb",
|
||||
"roq",
|
||||
"svi",
|
||||
"vob",
|
||||
"webm",
|
||||
"wmv",
|
||||
"yuv",
|
||||
];
|
||||
@@ -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