mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
opt: download img
Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -12,7 +12,6 @@ import 'package:brotli/brotli.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio/io.dart';
|
||||
import 'package:dio_http2_adapter/dio_http2_adapter.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../utils/storage.dart';
|
||||
import 'api.dart';
|
||||
import 'constants.dart';
|
||||
@@ -242,20 +241,28 @@ class Request {
|
||||
/*
|
||||
* 下载文件
|
||||
*/
|
||||
downloadFile(urlPath, savePath) async {
|
||||
Response response;
|
||||
Future<Response> downloadFile(urlPath, savePath, {cancelToken}) async {
|
||||
try {
|
||||
response = await dio.download(urlPath, savePath,
|
||||
onReceiveProgress: (int count, int total) {
|
||||
Response response = await dio.download(
|
||||
urlPath,
|
||||
savePath,
|
||||
cancelToken: cancelToken,
|
||||
// onReceiveProgress: (int count, int total) {
|
||||
//进度
|
||||
// debugPrint("$count $total");
|
||||
});
|
||||
debugPrint('downloadFile success: ${response.data}');
|
||||
|
||||
return response.data;
|
||||
// },
|
||||
);
|
||||
// debugPrint('downloadFile success: ${response.data}');
|
||||
return response;
|
||||
} on DioException catch (e) {
|
||||
debugPrint('downloadFile error: $e');
|
||||
return Future.error(AccountManager.dioError(e));
|
||||
// debugPrint('downloadFile error: $e');
|
||||
return Response(
|
||||
data: {
|
||||
'message': await AccountManager.dioError(e),
|
||||
},
|
||||
statusCode: -1,
|
||||
requestOptions: RequestOptions(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:PiliPlus/http/init.dart';
|
||||
import 'package:PiliPlus/utils/extension.dart';
|
||||
import 'package:PiliPlus/utils/utils.dart';
|
||||
@@ -202,48 +200,64 @@ class DownloadUtils {
|
||||
cancelToken.cancel();
|
||||
},
|
||||
);
|
||||
final picName =
|
||||
"${imgType}_${DateTime.now().toString().substring(0, 19).replaceAll(' ', '_').replaceAll(':', '-')}";
|
||||
try {
|
||||
await Future.wait(imgList.map((i) async {
|
||||
var response = await Request().get(
|
||||
i.http2https,
|
||||
options: Options(responseType: ResponseType.bytes),
|
||||
final isAndroid = Platform.isAndroid;
|
||||
final tempPath = (await getTemporaryDirectory()).path;
|
||||
final futures = imgList.map((url) async {
|
||||
var name = Utils.getFileName(url);
|
||||
var filePath = '$tempPath/$name';
|
||||
|
||||
var response = await Request().downloadFile(
|
||||
url.http2https,
|
||||
filePath,
|
||||
cancelToken: cancelToken,
|
||||
);
|
||||
|
||||
if (response.data is Map) {
|
||||
throw HttpException(response.data['message']);
|
||||
}
|
||||
|
||||
var name = '${picName}_${Utils.getFileName(i)}';
|
||||
|
||||
final SaveResult result = await SaverGallery.saveImage(
|
||||
response.data as Uint8List,
|
||||
quality: 100,
|
||||
if (isAndroid) {
|
||||
if (response.statusCode == 200) {
|
||||
await SaverGallery.saveFile(
|
||||
filePath: filePath,
|
||||
fileName: name,
|
||||
// 保存到 PiliPlus文件夹
|
||||
androidRelativePath: "Pictures/PiliPlus",
|
||||
skipIfExists: false,
|
||||
);
|
||||
|
||||
// SmartDialog.dismiss();
|
||||
// SmartDialog.showLoading(msg: '正在保存图片至图库');
|
||||
|
||||
if (result.isSuccess) {
|
||||
// SmartDialog.showToast('「$name」已保存');
|
||||
} else {
|
||||
throw Exception('保存失败,${result.errorMessage}');
|
||||
}
|
||||
}), eagerError: true);
|
||||
SmartDialog.dismiss();
|
||||
}
|
||||
return {
|
||||
'filePath': filePath,
|
||||
'name': name,
|
||||
'statusCode': response.statusCode,
|
||||
};
|
||||
});
|
||||
final result = await Future.wait(futures, eagerError: true);
|
||||
if (!isAndroid) {
|
||||
for (Map res in result) {
|
||||
if (res['statusCode'] == 200) {
|
||||
await SaverGallery.saveFile(
|
||||
filePath: res['filePath'],
|
||||
fileName: res['name'],
|
||||
androidRelativePath: "Pictures/PiliPlus",
|
||||
skipIfExists: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
// `SmartDialog.dismiss();` will call `onDismiss();`
|
||||
if (cancelToken.isCancelled) {
|
||||
SmartDialog.dismiss(status: SmartStatus.loading);
|
||||
SmartDialog.showToast('已取消下载');
|
||||
return false;
|
||||
} else {
|
||||
SmartDialog.dismiss(status: SmartStatus.loading);
|
||||
SmartDialog.showToast('图片已保存');
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
SmartDialog.dismiss();
|
||||
if (cancelToken.isCancelled) {
|
||||
SmartDialog.dismiss(status: SmartStatus.loading);
|
||||
SmartDialog.showToast('已取消下载');
|
||||
} else {
|
||||
SmartDialog.dismiss(status: SmartStatus.loading);
|
||||
SmartDialog.showToast(e.toString());
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -1677,6 +1677,6 @@ class Utils {
|
||||
}
|
||||
|
||||
static String getFileName(String uri) {
|
||||
return uri.substring(uri.lastIndexOf('/') + 1, uri.lastIndexOf('.'));
|
||||
return uri.substring(uri.lastIndexOf('/') + 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user