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