Add download zip page

This commit is contained in:
2023-09-16 13:14:23 +08:00
parent 93a307b143
commit f2b6ad465d
13 changed files with 412 additions and 16 deletions

View File

@@ -0,0 +1,36 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import '../globals.dart';
import '../platform/save_file.dart';
Future<void> downloadZip(int gid,
{bool? jpnTitle, int? maxLength, bool? exportAd}) async {
final cancel = CancelToken();
final re = await api.exportGalleryZip(gid,
jpnTitle: jpnTitle,
maxLength: maxLength,
exportAd: exportAd,
cancel: cancel);
final data = re.data as ResponseBody;
if (data.statusCode != 200) {
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";
try {
final f = await platformPath.openFile(
Uri.decodeComponent(filenameWithoutExtension), "application/zip");
try {
await data.stream.forEach((data) {
f.write(data);
});
} finally {
await f.dispose();
}
} catch (e) {
cancel.cancel();
rethrow;
}
}