Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
improve export & import data
Browse files Browse the repository at this point in the history
  • Loading branch information
wgh136 committed Sep 13, 2024
1 parent 8891d8b commit 34aa2c2
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 126 deletions.
31 changes: 20 additions & 11 deletions lib/foundation/image_manager.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_qjs/flutter_qjs.dart';
import 'package:html/parser.dart';
import 'package:dio/dio.dart';
import 'package:pica_comic/comic_source/comic_source.dart';
import 'package:pica_comic/foundation/cache_manager.dart';
import 'package:pica_comic/foundation/image_loader/image_recombine.dart';
import 'package:pica_comic/foundation/log.dart';
import 'package:pica_comic/network/app_dio.dart';
import 'package:pica_comic/network/cookie_jar.dart';
import 'package:pica_comic/network/eh_network/eh_models.dart';
import 'package:pica_comic/network/eh_network/get_gallery_id.dart';
import 'package:pica_comic/network/hitomi_network/hitomi_models.dart';
import 'package:pica_comic/tools/extensions.dart';
import 'package:pica_comic/foundation/image_loader/image_recombine.dart';
import 'package:pica_comic/tools/file_type.dart';

import '../base.dart';
import '../network/eh_network/eh_main_network.dart';
import '../network/hitomi_network/image.dart';
Expand Down Expand Up @@ -358,7 +360,7 @@ class ImageManager {
int retryTimes = 0;
var currentBytes = 0;

while(true) {
while (true) {
try {
data.clear();
cachingFile.reset();
Expand All @@ -368,27 +370,33 @@ class ImageManager {
res = await dio.get<ResponseBody>(image,
options: Options(responseType: ResponseType.stream));
if (res.data!.headers["Content-Type"]?[0] ==
"text/html; charset=UTF-8" ||
"text/html; charset=UTF-8" ||
res.data!.headers["content-type"]?[0] ==
"text/html; charset=UTF-8") {
throw ImageExceedError();
}
var stream = res.data!.stream;
int? expectedBytes;
try {
expectedBytes = int.parse(res.data!.headers["Content-Length"]![0]);
expectedBytes =
int.parse(res.data!.headers["Content-Length"]![0]);
} catch (e) {
try {
expectedBytes = int.parse(res.data!.headers["content-length"]![0]);
expectedBytes =
int.parse(res.data!.headers["content-length"]![0]);
} finally {}
}

await for (var b in stream) {
await cachingFile.writeBytes(b);
currentBytes += b.length;
data.addAll(b);
var progress = DownloadProgress(currentBytes,
(expectedBytes ?? currentBytes) + 1, cacheKey, savePath);
var progress = DownloadProgress(
currentBytes,
expectedBytes + 1,
cacheKey,
savePath,
);
yield progress;
loadingItems[cacheKey] = progress;
}
Expand Down Expand Up @@ -561,7 +569,6 @@ class ImageManager {
}));
ext = getExt(res);
var stream = res.data!.stream;
int i = 0;
await for (var b in stream) {
//不直接写入文件, 因为需要对图片进行重组, 处理完成后再写入
bytes.addAll(b);
Expand Down Expand Up @@ -728,7 +735,7 @@ class ImageManager {
}

Stream<DownloadProgress> getCustomThumbnail(
String url, String sourceKey) async* {
String url, String sourceKey, [Map<String, String>? headers]) async* {
var cacheKey = "$sourceKey$url";
await wait(cacheKey);
loadingItems[cacheKey] = DownloadProgress(0, 1, cacheKey, "");
Expand All @@ -754,6 +761,8 @@ class ImageManager {
config = source.getThumbnailLoadingConfig!(url);
}

config['headers'] ??= headers;

caching = await CacheManager().openWrite(cacheKey);
final savePath = caching.file.path;

Expand Down Expand Up @@ -802,7 +811,7 @@ class ImageManager {
await caching.close();
yield DownloadProgress(
1, 1, url, savePath, result ?? Uint8List.fromList(imageData));
} catch (e, s) {
} catch (e) {
Log.error("Network", "Failed to load a image:\nUrl:$url\nError:$e");
caching?.cancel();
if (e is DioException && e.type == DioExceptionType.badResponse) {
Expand Down
17 changes: 12 additions & 5 deletions lib/network/cookie_jar.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:pica_comic/tools/extensions.dart';
import 'package:sqlite3/sqlite3.dart';

import 'dart:io';

class CookieJarSql {
final Database _db;
late Database _db;

final String path;

CookieJarSql(this.path){
init();
}

CookieJarSql(String path) : _db = sqlite3.open(path) {
void init() {
_db = sqlite3.open(path);
_db.execute('''
CREATE TABLE IF NOT EXISTS cookies (
name TEXT NOT NULL,
Expand Down Expand Up @@ -172,7 +179,7 @@ class CookieJarSql {
''');
}

void close() {
void dispose() {
_db.dispose();
}
}
Expand Down
8 changes: 8 additions & 0 deletions lib/network/download.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ class DownloadManager with _DownloadDb implements Listenable {
}
}

void dispose() {
_runInit = false;
downloading.forEach((e) => e.stop());
downloading.clear();
_db?.dispose();
_db = null;
}

///初始化下载管理器
Future<void> init() async {
if (_runInit) return;
Expand Down
46 changes: 26 additions & 20 deletions lib/network/jm_network/jm_network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension _CachedNetwork on CachedNetwork {
dio.interceptors.add(CookieManager(cookieJar));
}
var res = await dio.get<Uint8List>(url);
if(res.data == null){
if (res.data == null) {
throw Exception("Empty data");
}
var body = utf8.decoder.convert(res.data!);
Expand All @@ -62,7 +62,8 @@ extension _CachedNetwork on CachedNetwork {
if (expiredTime != CacheExpiredTime.no) {
await CacheManager().writeCache(key, res.data!, expiredTime.time);
}
return CachedNetworkRes(decodedData, res.statusCode, res.realUri.toString());
return CachedNetworkRes(
decodedData, res.statusCode, res.realUri.toString());
}
}

Expand Down Expand Up @@ -119,15 +120,16 @@ class JmNetwork {
return res.substring(0, i + 1);
}

Future<void> init() async{
Future<void> init() async {
loginFromAppdata();
}

///get请求, 返回Json数据中的data
Future<Res<dynamic>> get(String url,
{Map<String, String>? header,
CacheExpiredTime expiredTime = CacheExpiredTime.long, bool isRetry = false}) async {
while(_performingLogin){
CacheExpiredTime expiredTime = CacheExpiredTime.long,
bool isRetry = false}) async {
while (_performingLogin) {
await Future.delayed(const Duration(milliseconds: 100));
}

Expand All @@ -139,10 +141,11 @@ class JmNetwork {
var res = await dio.getJm(url, options, time,
cookieJar: cookieJar, expiredTime: CacheExpiredTime.no);
if (res.statusCode == 401) {
var message = const JsonDecoder().convert(res.data)["errorMsg"] ?? "Error";
if(message == "請先登入會員" && jm.isLogin && !isRetry) {
var message =
const JsonDecoder().convert(res.data)["errorMsg"] ?? "Error";
if (message == "請先登入會員" && jm.isLogin && !isRetry) {
var res = await jm.reLogin();
if(res) {
if (res) {
return get(
url,
header: header,
Expand All @@ -151,7 +154,7 @@ class JmNetwork {
);
}
}
return Res(null, errorMessage:message);
return Res(null, errorMessage: message);
}

final data = const JsonDecoder().convert(res.data);
Expand Down Expand Up @@ -410,8 +413,7 @@ class JmNetwork {
"$baseUrl/search?&search_query=$keyword&o=$order&page=$page",
expiredTime: CacheExpiredTime.no);
} else {
res = await get(
"$baseUrl/search?&search_query=$keyword&o=$order",
res = await get("$baseUrl/search?&search_query=$keyword&o=$order",
expiredTime: CacheExpiredTime.no);
}
if (res.error) {
Expand Down Expand Up @@ -510,12 +512,12 @@ class JmNetwork {
}
}
Object total = res.data["total"];
if(total is String){
if (total is String) {
total = int.parse(total);
}
var current = res.data["content"].length;
var pagesCount = 1;
if(current != 0){
if (current != 0) {
pagesCount = ((total as int) / current).ceil();
}
return Res(comics, subData: pagesCount);
Expand All @@ -529,7 +531,7 @@ class JmNetwork {
var res = await get("$baseUrl/album?comicName=&id=$id",
expiredTime: CacheExpiredTime.no);
if (res.error) {
if(res.errorMessage!.contains("Empty data")){
if (res.errorMessage!.contains("Empty data")) {
throw Exception("漫畫不存在: id = $id");
}
return Res(null, errorMessage: res.errorMessage);
Expand Down Expand Up @@ -591,8 +593,7 @@ class JmNetwork {
jm.data['name'] = account;
appdata.writeData();
return const Res(true);
}
finally {
} finally {
_performingLogin = false;
}
}
Expand Down Expand Up @@ -704,7 +705,7 @@ class JmNetwork {
res = await post("$baseUrl/favorite", "aid=$id&$baseData");
}
Res res2 = const Res(true);
if(folder != "0"){
if (folder != "0") {
res2 = await moveToFolder(id, folder);
}
if (res.error || res2.error) {
Expand Down Expand Up @@ -780,7 +781,12 @@ class JmNetwork {
comments.add(Comment(c["CID"], getJmAvatarUrl(c["photo"]),
c["username"], c["addtime"], parseContent(c["content"]), reply));
}
return Res(comments, subData: int.parse(res.data["total"]));
return Res(
comments,
subData: res.data["total"] is int
? res.data["total"]
: (int.tryParse(res.data["total"]) ?? 1),
);
} catch (e, s) {
LogManager.addLog(LogLevel.error, "Data Analysis", "$e\n$s");
return Res(null, errorMessage: e.toString());
Expand Down Expand Up @@ -899,8 +905,8 @@ enum ComicsOrder {
const ComicsOrder(this.value);

static ComicsOrder fromValue(String value) {
for(var v in ComicsOrder.values) {
if(v.value == value) return v;
for (var v in ComicsOrder.values) {
if (v.value == value) return v;
}
throw UnimplementedError();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/network/webdav.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pica_comic/components/components.dart';
Expand All @@ -8,6 +9,7 @@ import 'package:pica_comic/tools/extensions.dart';
import 'package:pica_comic/tools/io_tools.dart';
import 'package:pica_comic/tools/translations.dart';
import 'package:webdav_client/webdav_client.dart';

import '../base.dart';

Future<bool> _retryZone(Future<bool> Function() fn) async {
Expand Down Expand Up @@ -78,7 +80,7 @@ class Webdav {
}
}
}
await client.writeFromFile(await exportDataToFile(false),
await client.writeFromFile(await exportDataToFile(false, "${App.cachePath}/userdata.picadata"),
"${configs[3]}${appdata.settings[46]}.picadata");
} catch (e, s) {
LogManager.addLog(LogLevel.error, "Sync",
Expand Down
Loading

0 comments on commit 34aa2c2

Please sign in to comment.