mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
win (#1240)
Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -3,55 +3,61 @@ import 'dart:io';
|
||||
|
||||
import 'package:PiliPlus/utils/extension.dart';
|
||||
import 'package:PiliPlus/utils/storage_pref.dart';
|
||||
import 'package:PiliPlus/utils/utils.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
abstract class CacheManage {
|
||||
// 获取缓存目录
|
||||
static Future<double> loadApplicationCache() async {
|
||||
static Future<int> loadApplicationCache() async {
|
||||
/// clear all of image in memory
|
||||
// clearMemoryImageCache();
|
||||
/// get ImageCache
|
||||
// var res = getMemoryImageCache();
|
||||
|
||||
// 缓存大小
|
||||
double cacheSize = 0;
|
||||
// cached_network_image directory
|
||||
Directory tempDirectory = await getTemporaryDirectory();
|
||||
if (Utils.isDesktop) {
|
||||
final dir = Directory('${tempDirectory.path}/libCachedImageData');
|
||||
if (dir.existsSync()) {
|
||||
return await getTotalSizeOfFilesInDir(dir);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// get_storage directory
|
||||
Directory docDirectory = await getApplicationDocumentsDirectory();
|
||||
|
||||
int cacheSize = 0;
|
||||
// 获取缓存大小
|
||||
if (tempDirectory.existsSync()) {
|
||||
double value = await getTotalSizeOfFilesInDir(tempDirectory);
|
||||
cacheSize += value;
|
||||
cacheSize += await getTotalSizeOfFilesInDir(tempDirectory);
|
||||
}
|
||||
|
||||
/// 获取缓存大小 dioCache
|
||||
if (docDirectory.existsSync()) {
|
||||
double value = 0;
|
||||
String dioCacheFileName =
|
||||
'${docDirectory.path}${Platform.pathSeparator}DioCache.db';
|
||||
var dioCacheFile = File(dioCacheFileName);
|
||||
if (dioCacheFile.existsSync()) {
|
||||
value = await getTotalSizeOfFilesInDir(dioCacheFile);
|
||||
cacheSize += await getTotalSizeOfFilesInDir(dioCacheFile);
|
||||
}
|
||||
cacheSize += value;
|
||||
}
|
||||
|
||||
return cacheSize;
|
||||
}
|
||||
|
||||
// 循环计算文件的大小(递归)
|
||||
static Future<double> getTotalSizeOfFilesInDir(
|
||||
static Future<int> getTotalSizeOfFilesInDir(
|
||||
final FileSystemEntity file,
|
||||
) async {
|
||||
if (file is File) {
|
||||
int length = await file.length();
|
||||
return double.parse(length.toString());
|
||||
return int.parse(length.toString());
|
||||
}
|
||||
if (file is Directory) {
|
||||
final List<FileSystemEntity> children = file.listSync();
|
||||
double total = 0;
|
||||
int total = 0;
|
||||
for (final FileSystemEntity child in children) {
|
||||
total += await getTotalSizeOfFilesInDir(child);
|
||||
}
|
||||
@@ -87,10 +93,17 @@ abstract class CacheManage {
|
||||
|
||||
// 清除 Library/Caches 目录及文件缓存
|
||||
static Future<void> clearLibraryCache() async {
|
||||
var appDocDir = await getTemporaryDirectory();
|
||||
if (appDocDir.existsSync()) {
|
||||
var tempDirectory = await getTemporaryDirectory();
|
||||
if (Utils.isDesktop) {
|
||||
final dir = Directory('${tempDirectory.path}/libCachedImageData');
|
||||
if (dir.existsSync()) {
|
||||
await dir.delete(recursive: true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (tempDirectory.existsSync()) {
|
||||
// await appDocDir.delete(recursive: true);
|
||||
final List<FileSystemEntity> children = appDocDir.listSync(
|
||||
final List<FileSystemEntity> children = tempDirectory.listSync(
|
||||
recursive: false,
|
||||
);
|
||||
for (final FileSystemEntity file in children) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:intl/intl.dart' show DateFormat;
|
||||
|
||||
class DateUtil {
|
||||
class DateFormatUtils {
|
||||
static final shortFormat = DateFormat('MM-dd');
|
||||
static final longFormat = DateFormat('yyyy-MM-dd');
|
||||
static final _shortFormatD = DateFormat('MM-dd HH:mm');
|
||||
@@ -38,7 +38,7 @@ class DateUtil {
|
||||
}
|
||||
final DateFormat sdf = now.year == date.year
|
||||
? short ?? shortFormat
|
||||
: long ?? DateUtil.longFormat;
|
||||
: long ?? DateFormatUtils.longFormat;
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'dart:math' show pow;
|
||||
|
||||
class DurationUtil {
|
||||
class DurationUtils {
|
||||
static String formatDuration(num? seconds) {
|
||||
if (seconds == null || seconds == 0) {
|
||||
return '00:00';
|
||||
@@ -227,9 +227,9 @@ extension ThreeDotItemTypeExt on ThreeDotItemType {
|
||||
}
|
||||
|
||||
extension FileExt on File {
|
||||
void delSync({bool recursive = false}) {
|
||||
void tryDel({bool recursive = false}) {
|
||||
try {
|
||||
deleteSync(recursive: recursive);
|
||||
delete(recursive: recursive);
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class FavUtil {
|
||||
class FavUtils {
|
||||
static bool isDefaultFav(int? attr) {
|
||||
if (attr == null) {
|
||||
return false;
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:PiliPlus/http/init.dart';
|
||||
import 'package:PiliPlus/utils/extension.dart';
|
||||
@@ -6,6 +7,7 @@ import 'package:PiliPlus/utils/global_data.dart';
|
||||
import 'package:PiliPlus/utils/storage_pref.dart';
|
||||
import 'package:PiliPlus/utils/utils.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
import 'package:intl/intl.dart' show DateFormat;
|
||||
@@ -14,7 +16,7 @@ import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:saver_gallery/saver_gallery.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
|
||||
class ImageUtil {
|
||||
class ImageUtils {
|
||||
static String get time =>
|
||||
DateFormat('yyyy-MM-dd_HH-mm-ss').format(DateTime.now());
|
||||
static bool silentDownImg = Pref.silentDownImg;
|
||||
@@ -35,7 +37,7 @@ class ImageUtil {
|
||||
sharePositionOrigin: await Utils.sharePositionOrigin,
|
||||
),
|
||||
)
|
||||
.whenComplete(() => File(path).delSync());
|
||||
.whenComplete(File(path).tryDel);
|
||||
}
|
||||
} catch (e) {
|
||||
SmartDialog.showToast(e.toString());
|
||||
@@ -131,8 +133,8 @@ class ImageUtil {
|
||||
height: height,
|
||||
).whenComplete(
|
||||
() {
|
||||
File(videoPath).delSync();
|
||||
File(imagePath).delSync();
|
||||
File(videoPath).tryDel();
|
||||
File(imagePath).tryDel();
|
||||
},
|
||||
);
|
||||
if (success) {
|
||||
@@ -143,18 +145,12 @@ class ImageUtil {
|
||||
}
|
||||
} else {
|
||||
if (!silentDownImg) SmartDialog.showLoading(msg: '正在保存');
|
||||
final SaveResult result = await SaverGallery.saveFile(
|
||||
await saveFileImg(
|
||||
filePath: videoPath,
|
||||
fileName: videoName,
|
||||
androidRelativePath: "Pictures/PiliPlus",
|
||||
skipIfExists: false,
|
||||
).whenComplete(() => File(videoPath).delSync());
|
||||
if (result.isSuccess) {
|
||||
SmartDialog.showToast(' 已保存 ');
|
||||
} else {
|
||||
SmartDialog.showToast('保存失败,${result.errorMessage}');
|
||||
return false;
|
||||
}
|
||||
type: FileType.video,
|
||||
needToast: true,
|
||||
);
|
||||
}
|
||||
return true;
|
||||
} catch (err) {
|
||||
@@ -199,7 +195,7 @@ class ImageUtil {
|
||||
fileName: name,
|
||||
androidRelativePath: "Pictures/PiliPlus",
|
||||
skipIfExists: false,
|
||||
).whenComplete(() => File(filePath).delSync());
|
||||
).whenComplete(File(filePath).tryDel);
|
||||
}
|
||||
}
|
||||
return (
|
||||
@@ -212,13 +208,10 @@ class ImageUtil {
|
||||
if (!isAndroid) {
|
||||
for (var res in result) {
|
||||
if (res.statusCode == 200) {
|
||||
await SaverGallery.saveFile(
|
||||
await saveFileImg(
|
||||
filePath: res.filePath,
|
||||
fileName: res.name,
|
||||
androidRelativePath: "Pictures/PiliPlus",
|
||||
skipIfExists: false,
|
||||
);
|
||||
File(res.filePath).delSync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -265,4 +258,83 @@ class ImageUtil {
|
||||
}
|
||||
return src.http2https;
|
||||
}
|
||||
|
||||
static Future<SaveResult?> saveByteImg({
|
||||
required Uint8List bytes,
|
||||
required String fileName,
|
||||
String ext = 'png',
|
||||
}) async {
|
||||
SaveResult? result;
|
||||
fileName += '.$ext';
|
||||
if (Utils.isMobile) {
|
||||
SmartDialog.showLoading(msg: '正在保存');
|
||||
result = await SaverGallery.saveImage(
|
||||
bytes,
|
||||
fileName: fileName,
|
||||
androidRelativePath: "Pictures/PiliPlus",
|
||||
skipIfExists: false,
|
||||
);
|
||||
SmartDialog.dismiss();
|
||||
if (result.isSuccess) {
|
||||
SmartDialog.showToast(' 已保存 ');
|
||||
} else {
|
||||
SmartDialog.showToast('保存失败,${result.errorMessage}');
|
||||
}
|
||||
} else {
|
||||
SmartDialog.dismiss();
|
||||
final savePath = await FilePicker.platform.saveFile(
|
||||
type: FileType.image,
|
||||
fileName: fileName,
|
||||
);
|
||||
if (savePath == null) {
|
||||
SmartDialog.showToast("取消保存");
|
||||
return null;
|
||||
}
|
||||
await File(savePath).writeAsBytes(bytes);
|
||||
SmartDialog.showToast(' 已保存 ');
|
||||
result = SaveResult(true, null);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static Future<void> saveFileImg({
|
||||
required String filePath,
|
||||
required String fileName,
|
||||
FileType type = FileType.image,
|
||||
bool needToast = false,
|
||||
}) async {
|
||||
final file = File(filePath);
|
||||
if (!file.existsSync()) {
|
||||
SmartDialog.showToast("文件不存在");
|
||||
return;
|
||||
}
|
||||
SaveResult? result;
|
||||
if (Utils.isMobile) {
|
||||
result = await SaverGallery.saveFile(
|
||||
filePath: filePath,
|
||||
fileName: fileName,
|
||||
androidRelativePath: "Pictures/PiliPlus",
|
||||
skipIfExists: false,
|
||||
).whenComplete(file.tryDel);
|
||||
} else {
|
||||
final savePath = await FilePicker.platform.saveFile(
|
||||
type: type,
|
||||
fileName: fileName,
|
||||
);
|
||||
if (savePath == null) {
|
||||
SmartDialog.showToast("取消保存");
|
||||
return;
|
||||
}
|
||||
await file.copy(savePath);
|
||||
file.tryDel();
|
||||
result = SaveResult(true, null);
|
||||
}
|
||||
if (needToast) {
|
||||
if (result.isSuccess) {
|
||||
SmartDialog.showToast(' 已保存 ');
|
||||
} else {
|
||||
SmartDialog.showToast('保存失败,${result.errorMessage}');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async' show FutureOr;
|
||||
import 'dart:io' show Platform;
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:PiliPlus/grpc/grpc_req.dart';
|
||||
@@ -16,6 +18,7 @@ import 'package:PiliPlus/pages/pgc/controller.dart';
|
||||
import 'package:PiliPlus/services/account_service.dart';
|
||||
import 'package:PiliPlus/utils/accounts.dart';
|
||||
import 'package:PiliPlus/utils/accounts/account.dart';
|
||||
import 'package:PiliPlus/utils/request_utils.dart';
|
||||
import 'package:PiliPlus/utils/storage.dart';
|
||||
import 'package:PiliPlus/utils/storage_pref.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart' as web;
|
||||
@@ -25,7 +28,10 @@ import 'package:get/get.dart';
|
||||
class LoginUtils {
|
||||
static final random = Random();
|
||||
|
||||
static Future setWebCookie([Account? account]) async {
|
||||
static FutureOr setWebCookie([Account? account]) {
|
||||
if (Platform.isWindows) {
|
||||
return null;
|
||||
}
|
||||
final cookies = (account ?? Accounts.main).cookieJar.toList();
|
||||
final webManager = web.CookieManager();
|
||||
return Future.wait(
|
||||
@@ -47,6 +53,7 @@ class LoginUtils {
|
||||
final account = Accounts.main;
|
||||
GrpcReq.updateHeaders(account.accessKey);
|
||||
setWebCookie(account);
|
||||
RequestUtils.syncHistoryStatus();
|
||||
final result = await UserHttp.userInfo();
|
||||
if (result.isSuccess) {
|
||||
final UserInfoData data = result.data;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:flutter/foundation.dart' show kDebugMode, debugPrint;
|
||||
import 'package:get/get_utils/get_utils.dart';
|
||||
|
||||
class NumUtil {
|
||||
class NumUtils {
|
||||
static final _numRegExp = RegExp(r'([\d\.]+)([千万亿])?');
|
||||
|
||||
static int _getUnit(String? unit) {
|
||||
@@ -54,7 +54,7 @@ abstract class GStorage {
|
||||
}
|
||||
|
||||
static String exportAllSettings() {
|
||||
return jsonEncode({
|
||||
return const JsonEncoder.withIndent(' ').convert({
|
||||
setting.name: setting.toMap(),
|
||||
video.name: video.toMap(),
|
||||
});
|
||||
|
||||
@@ -50,7 +50,7 @@ class ThemeUtils {
|
||||
titleSpacing: 0,
|
||||
centerTitle: false,
|
||||
scrolledUnderElevation: 0,
|
||||
backgroundColor: isDynamic ? null : colorScheme.surface,
|
||||
backgroundColor: colorScheme.surface,
|
||||
titleTextStyle: TextStyle(
|
||||
fontSize: 16,
|
||||
color: colorScheme.onSurface,
|
||||
|
||||
@@ -16,6 +16,11 @@ class Utils {
|
||||
|
||||
static const channel = MethodChannel("PiliPlus");
|
||||
|
||||
static final bool isMobile = Platform.isAndroid || Platform.isIOS;
|
||||
|
||||
static final bool isDesktop =
|
||||
Platform.isWindows || Platform.isMacOS || Platform.isLinux;
|
||||
|
||||
static Color parseColor(String color) =>
|
||||
Color(int.parse(color.replaceFirst('#', 'FF'), radix: 16));
|
||||
|
||||
@@ -70,7 +75,9 @@ class Utils {
|
||||
String baseDirectory,
|
||||
List<String> shaders,
|
||||
) {
|
||||
return shaders.map((shader) => path.join(baseDirectory, shader)).join(':');
|
||||
return shaders
|
||||
.map((shader) => path.join(baseDirectory, shader))
|
||||
.join(Platform.isWindows ? ';' : ':');
|
||||
}
|
||||
|
||||
static final numericRegex = RegExp(r'^[\d\.]+$');
|
||||
|
||||
Reference in New Issue
Block a user