mirror of
https://github.com/lifegpc/eh_downloader_flutter.git
synced 2026-07-08 01:30:28 +08:00
rewrite file api
This commit is contained in:
@@ -31,38 +31,57 @@ class Path {
|
||||
return _currentExe;
|
||||
}
|
||||
|
||||
/// 保存文件
|
||||
Future<void> saveFile(
|
||||
String filenameWithoutExtension, String mimeType, Uint8List bytes,
|
||||
{String dir = ""}) async {
|
||||
{String dir = "", bool saveAs = true}) async {
|
||||
if (kIsWeb) {
|
||||
return saveFileWeb(bytes, mimeType, filenameWithoutExtension);
|
||||
}
|
||||
return _safChannel.invokeMethod(
|
||||
"saveFile", [filenameWithoutExtension, dir, mimeType, bytes]);
|
||||
final f = await openFile(filenameWithoutExtension, mimeType,
|
||||
dir: dir, saveAs: saveAs);
|
||||
try {
|
||||
await f.write(bytes);
|
||||
} finally {
|
||||
await f.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Future<SAFFile> openFile(String filenameWithoutExtension, String mimeType,
|
||||
{String dir = ""}) async {
|
||||
final fd = await _safChannel.invokeMethod<int>(
|
||||
"openFile", [filenameWithoutExtension, dir, mimeType]);
|
||||
return SAFFile(fd!);
|
||||
{String dir = "",
|
||||
bool read = false,
|
||||
bool write = true,
|
||||
bool append = false,
|
||||
bool saveAs = true}) async {
|
||||
if (!write) {
|
||||
append = false;
|
||||
}
|
||||
final fd = await _safChannel.invokeMethod<int>("openFile",
|
||||
[filenameWithoutExtension, dir, mimeType, read, write, append, saveAs]);
|
||||
return SAFFile(fd!, read, write);
|
||||
}
|
||||
}
|
||||
|
||||
class SAFFile {
|
||||
SAFFile(this._fd);
|
||||
SAFFile(this._fd, this._read, this._write);
|
||||
|
||||
final int _fd;
|
||||
final bool _read;
|
||||
final bool _write;
|
||||
bool _disposed = false;
|
||||
|
||||
Future<Uint8List> read(int maxLen) async {
|
||||
if (_disposed) throw Exception("File already closed");
|
||||
if (!_read) throw Exception("File not opened for read");
|
||||
return await Path._safChannel.invokeMethod("readFile", [_fd, maxLen]);
|
||||
}
|
||||
|
||||
/// 写入文件,返回此次写入的字节数
|
||||
///
|
||||
/// [data] 要写入文件的数据
|
||||
Future<int> write(Uint8List data) async {
|
||||
if (_disposed) throw Exception("File already closed");
|
||||
return await Path._safChannel
|
||||
.invokeMethod("writeFile", [_fd, data]);
|
||||
if (!_write) throw Exception("File not opened for write");
|
||||
return await Path._safChannel.invokeMethod("writeFile", [_fd, data]);
|
||||
}
|
||||
|
||||
Future<void> dispose() async {
|
||||
|
||||
Reference in New Issue
Block a user