mirror of
https://github.com/lifegpc/eh_downloader_flutter.git
synced 2026-07-08 01:30:28 +08:00
Support download zip on web
This commit is contained in:
@@ -209,4 +209,19 @@ class EHApi extends __EHApi {
|
||||
Future<ApiResult<Tags>> getTags(List<int> ids, {CancelToken? cancel}) {
|
||||
return _getTags(ids.join(","), cancel: cancel);
|
||||
}
|
||||
|
||||
String exportGalleryZipUrl(int gid,
|
||||
{bool? jpnTitle, int? maxLength, bool? exportAd}) {
|
||||
final uri = Uri.parse(_combineBaseUrls(_dio.options.baseUrl, baseUrl));
|
||||
var queries = {
|
||||
"jpn_title": jpnTitle?.toString(),
|
||||
"max_length": maxLength?.toString(),
|
||||
"export_ad": exportAd?.toString(),
|
||||
};
|
||||
queries.removeWhere((key, value) => value == null);
|
||||
final newUri = uri
|
||||
.resolve("export/gallery/zip/$gid")
|
||||
.replace(queryParameters: queries);
|
||||
return newUri.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ bool tryInitApi(BuildContext context) {
|
||||
if (_api != null) {
|
||||
return true;
|
||||
}
|
||||
initApi("${Uri.base.origin}/api");
|
||||
initApi("${Uri.base.origin}/api/");
|
||||
clearAllStates(context);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -4,3 +4,7 @@ void saveFileWeb(
|
||||
Uint8List data, String mimeType, String filenameWithoutExtension) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
void saveUriWeb(String uri) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@@ -26,3 +26,9 @@ void saveFileWeb(
|
||||
a.click();
|
||||
Url.revokeObjectUrl(url);
|
||||
}
|
||||
|
||||
void saveUriWeb(String uri) {
|
||||
final a = document.createElement("a") as AnchorElement;
|
||||
a.href = uri;
|
||||
a.click();
|
||||
}
|
||||
|
||||
@@ -5,3 +5,22 @@ bool get isDesktop =>
|
||||
!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS);
|
||||
bool get isWindows => !kIsWeb && Platform.isWindows;
|
||||
bool get isAndroid => !kIsWeb && Platform.isAndroid;
|
||||
|
||||
String? getFilenameFromContentDisposition(String? contentDisposition) {
|
||||
if (contentDisposition == null) {
|
||||
return null;
|
||||
}
|
||||
var ind = contentDisposition.indexOf("filename=\"");
|
||||
if (ind != -1) {
|
||||
final filename = contentDisposition.substring(
|
||||
ind + 10, contentDisposition.lastIndexOf("\""));
|
||||
return Uri.decodeComponent(filename);
|
||||
}
|
||||
ind = contentDisposition.indexOf("filename*=UTF-8''");
|
||||
if (ind != -1) {
|
||||
final filename =
|
||||
contentDisposition.substring(ind + 17, contentDisposition.length);
|
||||
return Uri.decodeComponent(filename);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import '../globals.dart';
|
||||
import '../platform/save_file.dart';
|
||||
import '../utils.dart';
|
||||
|
||||
Future<void> downloadZip(int gid,
|
||||
{bool? jpnTitle, int? maxLength, bool? exportAd}) async {
|
||||
if (kIsWeb) {
|
||||
saveUriWeb(api.exportGalleryZipUrl(gid,
|
||||
jpnTitle: jpnTitle, maxLength: maxLength, exportAd: exportAd));
|
||||
return;
|
||||
}
|
||||
final cancel = CancelToken();
|
||||
final re = await api.exportGalleryZip(gid,
|
||||
jpnTitle: jpnTitle,
|
||||
@@ -16,12 +23,11 @@ Future<void> downloadZip(int gid,
|
||||
throw Exception("${data.statusCode} ${data.statusMessage}.");
|
||||
}
|
||||
final fileName = re.response.headers.value("content-disposition");
|
||||
final filenameWithoutExtension = fileName?.substring(
|
||||
fileName.indexOf("filename=\"") + 10, fileName.lastIndexOf(".")) ??
|
||||
"$gid";
|
||||
final filenameWithoutExtension = path.basenameWithoutExtension(
|
||||
getFilenameFromContentDisposition(fileName) ?? "$gid");
|
||||
try {
|
||||
final f = await platformPath.openFile(
|
||||
Uri.decodeComponent(filenameWithoutExtension), "application/zip");
|
||||
filenameWithoutExtension, "application/zip");
|
||||
try {
|
||||
await data.stream.forEach((data) {
|
||||
f.write(data);
|
||||
|
||||
Reference in New Issue
Block a user