Feat/saf (#81)

* [Android] Use SAF to change local path

* Use IOOverrides to replace openDirectoryPlatform and openFilePlatform

* fix io
This commit is contained in:
nyne
2024-11-29 21:33:28 +08:00
committed by GitHub
parent ce48a89cc1
commit 430b6eeb3a
14 changed files with 147 additions and 134 deletions

View File

@@ -235,20 +235,21 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
}
if (path == null) {
var dir = await LocalManager().findValidDirectory(
comicId,
comicType,
comic!.title,
);
if (!(await dir.exists())) {
try {
try {
var dir = await LocalManager().findValidDirectory(
comicId,
comicType,
comic!.title,
);
if (!(await dir.exists())) {
await dir.create();
} catch (e) {
_setError("Error: $e");
return;
}
path = dir.path;
} catch (e, s) {
Log.error("Download", e.toString(), s);
_setError("Error: $e");
return;
}
path = dir.path;
}
await LocalManager().saveCurrentDownloadingTasks();
@@ -266,11 +267,13 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
throw "Failed to download cover";
}
var fileType = detectFileType(data);
var file = File(FilePath.join(path!, "cover${fileType.ext}"));
var file =
File(FilePath.join(path!, "cover${fileType.ext}"));
file.writeAsBytesSync(data);
return "file://${file.path}";
});
if (res.error) {
Log.error("Download", res.errorMessage!);
_setError("Error: ${res.errorMessage}");
return;
} else {
@@ -294,6 +297,7 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
return;
}
if (res.error) {
Log.error("Download", res.errorMessage!);
_setError("Error: ${res.errorMessage}");
return;
} else {
@@ -323,6 +327,7 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
return;
}
if (res.error) {
Log.error("Download", res.errorMessage!);
_setError("Error: ${res.errorMessage}");
return;
} else {
@@ -347,6 +352,7 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
return;
}
if (task.error != null) {
Log.error("Download", task.error.toString());
_setError("Error: ${task.error}");
return;
}
@@ -375,7 +381,6 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
_message = message;
notifyListeners();
stopRecorder();
Log.error("Download", message);
}
@override
@@ -448,7 +453,8 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
}).toList(),
directory: Directory(path!).name,
chapters: comic!.chapters,
cover: File(_cover!.split("file://").last).uri.pathSegments.last,
cover:
File(_cover!.split("file://").last).name,
comicType: ComicType(source.key.hashCode),
downloadedChapters: chapters ?? [],
createdAt: DateTime.now(),
@@ -721,13 +727,12 @@ class ArchiveDownloadTask extends DownloadTask {
_currentBytes = status.downloadedBytes;
_expectedBytes = status.totalBytes;
_message =
"${bytesToReadableString(_currentBytes)}/${bytesToReadableString(_expectedBytes)}";
"${bytesToReadableString(_currentBytes)}/${bytesToReadableString(_expectedBytes)}";
_speed = status.bytesPerSecond;
isDownloaded = status.isFinished;
notifyListeners();
}
}
catch(e) {
} catch (e) {
_setError("Error: $e");
return;
}