From b9e7926eaa65ee0c0b30cec2d3e3c05e893dc163 Mon Sep 17 00:00:00 2001 From: TaYaKi71751 Date: Fri, 8 Dec 2023 21:16:10 +0900 Subject: [PATCH 1/3] Try download latest db at first try --- .../database_download_page.dart | 77 +++++++++++++-- lib/pages/splash/splash_page.dart | 2 +- lib/version/sync.dart | 94 +++---------------- 3 files changed, 80 insertions(+), 93 deletions(-) diff --git a/lib/pages/database_download/database_download_page.dart b/lib/pages/database_download/database_download_page.dart index c4c492bb8..ff66f4ad0 100644 --- a/lib/pages/database_download/database_download_page.dart +++ b/lib/pages/database_download/database_download_page.dart @@ -85,6 +85,21 @@ class DataBaseDownloadPageState extends State { } Future downloadFileAndroid() async { + try { + await downloadFileAndroidWith('latest', true); + } catch(e){ + await downloadFileAndroidWith('old', false); + } + } + Future downloadFileIOS() async { + try { + await downloadFileIOSWith('latest', true); + } catch(e){ + await downloadFileIOSWith('old', false); + } + } + + Future downloadFileAndroidWith(String target, bool _throw) async { Dio dio = Dio(); int oneMega = 1024 * 1024; int nu = 0; @@ -97,6 +112,18 @@ class DataBaseDownloadPageState extends State { if (await File('${dir.path}/db.sql.7z').exists()) { await File('${dir.path}/db.sql.7z').delete(); } + switch(target){ + case 'latest': await SyncManager.checkSyncLatest(_throw); break; + case 'old': await SyncManager.checkSyncOld(_throw); break; + default: { + try { + await downloadFileAndroidWith('latest', true); + } catch(e){ + await downloadFileAndroidWith('old', false); + } + return; + } + } Timer timer = Timer.periodic( const Duration(seconds: 1), (Timer timer) => setState(() { @@ -105,7 +132,6 @@ class DataBaseDownloadPageState extends State { tlatest = tnu; tnu = 0; })); - await SyncManager.checkSync(); await dio.download( SyncManager.getLatestDB().getDBDownloadUrl(widget.dbType!), '${dir.path}/db.sql.7z', onReceiveProgress: (rec, total) { @@ -163,9 +189,18 @@ class DataBaseDownloadPageState extends State { await DataBaseManager.reloadInstance(); if (Settings.useOptimizeDatabase) { - await deleteUnused(); - - await indexing(); + try { + await deleteUnused(); + } catch(e1,st1){ + Logger.error('[deleteUnused] E: $e1\n' + '$st1'); + } + try { + await indexing(); + } catch(e1,st1){ + Logger.error('[indexing] E: $e1\n' + '$st1'); + } } if (widget.isSync == true) { @@ -181,6 +216,7 @@ class DataBaseDownloadPageState extends State { } catch (e, st) { Logger.error('[DBDownload] E: $e\n' '$st'); + if(_throw) throw e; } setState(() { @@ -189,7 +225,7 @@ class DataBaseDownloadPageState extends State { }); } - Future downloadFileIOS() async { + Future downloadFileIOSWith(String target, bool _throw) async { Dio dio = Dio(); int oneMega = 1024 * 1024; int nu = 0; @@ -202,6 +238,19 @@ class DataBaseDownloadPageState extends State { if (await File('$dir/data.db').exists()) { await File('$dir/data.db').delete(); } + switch(target){ + case 'latest': await SyncManager.checkSyncLatest(_throw); break; + case 'old': await SyncManager.checkSyncOld(_throw); break; + default: { + try { + await downloadFileIOSWith('latest', true); + } catch(e){ + await downloadFileIOSWith('old', false); + } + return; + } + } + Timer timer = Timer.periodic( const Duration(seconds: 1), (Timer timer) => setState(() { @@ -210,7 +259,6 @@ class DataBaseDownloadPageState extends State { tlatest = tnu; tnu = 0; })); - await SyncManager.checkSync(); await dio.download( SyncManager.getLatestDB().getDBDownloadUrliOS(widget.dbType!), '$dir/data.db', onReceiveProgress: (rec, total) { @@ -246,9 +294,18 @@ class DataBaseDownloadPageState extends State { await DataBaseManager.reloadInstance(); - if (Settings.useOptimizeDatabase) await deleteUnused(); - - await indexing(); + try { + if (Settings.useOptimizeDatabase) await deleteUnused(); + } catch(e1,st1){ + Logger.error('[deleteUnused] E: $e1\n' + '$st1'); + } + try { + await indexing(); + } catch(e1,st1){ + Logger.error('[indexing] E: $e1\n' + '$st1'); + } if (widget.isSync == true) { Navigator.pop(context); @@ -263,6 +320,7 @@ class DataBaseDownloadPageState extends State { } catch (e, st) { Logger.error('[DBDownload] E: $e\n' '$st'); + if(_throw) throw e; } setState(() { @@ -275,7 +333,6 @@ class DataBaseDownloadPageState extends State { var sql = HitomiManager.translate2query( '${Settings.includeTags} ${Settings.excludeTags.where((e) => e.trim().isNotEmpty).map((e) => '-$e').join(' ')}') .replaceAll(' AND ExistOnHitomi=1', ''); - await (await DataBaseManager.getInstance()).delete('HitomiColumnModel', 'NOT (${sql.substring(sql.indexOf('WHERE') + 6)})', []); } diff --git a/lib/pages/splash/splash_page.dart b/lib/pages/splash/splash_page.dart index b077f8fa3..eb9504348 100644 --- a/lib/pages/splash/splash_page.dart +++ b/lib/pages/splash/splash_page.dart @@ -243,7 +243,7 @@ class _SplashPageState extends State { if (connectivityResult != ConnectivityResult.none) { try { _changeMessage('check sync...'); - await SyncManager.checkSyncLatest(); + await SyncManager.checkSyncLatest(true); if (!SyncManager.firstSync && SyncManager.chunkRequire) { setState(() { diff --git a/lib/version/sync.dart b/lib/version/sync.dart index 081f4e9fc..61bf28d54 100644 --- a/lib/version/sync.dart +++ b/lib/version/sync.dart @@ -52,10 +52,19 @@ class SyncManager { static List? _rows; static int requestSize = 0; - static Future checkSyncLatest() async { + static Future checkSyncLatest(bool _throw) async { + await checkSync('master', _throw); + } + + static Future checkSyncOld(bool _throw) async { + await checkSync('d2bd5ae068efb26eb4689e5d6281a590e59fc4e2', _throw); + } + + + static Future checkSync(String branch, bool _throw) async { try { var ls = const LineSplitter(); - var infoRaw = (await http.get(syncInfoURL('master'))).body; + var infoRaw = (await http.get(syncInfoURL(branch))).body; var lines = ls.convert(infoRaw); final prefs = await SharedPreferences.getInstance(); @@ -125,86 +134,7 @@ class SyncManager { } catch (e, st) { Logger.error('[Sync-check] E: $e\n' '$st'); - } - } - - - static Future checkSync() async { - for(int i = 0;i < 100;i++){ - try { - var ls = const LineSplitter(); - var infoRaw = (await http.get(syncInfoURL('d2bd5ae068efb26eb4689e5d6281a590e59fc4e2'))).body; - var lines = ls.convert(infoRaw); - - final prefs = await SharedPreferences.getInstance(); - var latest = prefs.getInt('synclatest'); - var lastDB = prefs.getString('databasesync'); - - if (latest == null) { - syncRequire = firstSync = true; - latest = 0; - } else if (lastDB != null && - DateTime.now().difference(DateTime.parse(lastDB)).inDays > 7) { - syncRequire = true; - } - - // lines: [old ... latest] - // _rows: [latest ... old] - _rows = []; - - /* - syncversion은 - - ... - chunk 1640992742 https://github.com/violet-dev/chunk/releases/download/1640992742/data-637765895426855402.json 31519 - db 1640997991 https://github.com/violet-dev/db/releases/download/2022.01.01/rawdata - chunk 1640998430 https://github.com/violet-dev/chunk/releases/download/1640998430/data-637765952304854006.json 47546 - chunk 1640998430 https://github.com/violet-dev/chunk/releases/download/1640998430/data-637765952304854006.db 45056 - chunk 1641003001 https://github.com/violet-dev/chunk/releases/download/1641003001/data-637765998015030026.json 30911 - chunk 1641003001 https://github.com/violet-dev/chunk/releases/download/1641003001/data-637765998015030026.db 32768 - ... - - 와 같은 형식으로 아래쪽이 항상 최신 청크다. 따라서 reversed 탐색을 시도한다. - */ - for (var element in lines.reversed) { - if (element.startsWith('#')) continue; - - var split = element.split(' '); - var type = split[0]; - var timestamp = int.parse(split[1]); - var url = split[2]; - var size = 0; - if (type == 'chunk') size = int.parse(split[3]); - - // We require only json files when synchronize with chunk. - if (type == 'chunk' && !url.endsWith('.json')) continue; - - // - // 마지막으로 동기화한 시간보다 작은 경우 해당 청크는 무시한다. - // - if (type == 'chunk' && timestamp <= latest) continue; - - requestSize += size; - _rows!.add(SyncInfoRecord( - type: type, - timestamp: timestamp, - url: url, - size: size, - )); - } - - /* - 너무 많은 청크를 다운로드해야하는 경우 동기화를 추천한다. - 그 이유는 다운로드해야하는 파일이 너무 많아지기 때문이며, - 또한 데이터베이스의 무결성이 훼손될 가능성이 있기 때문이다. - */ - if (requestSize > ignoreUserAcceptThreshold) syncRequire = true; - if (_rows!.any((element) => element.type == 'chunk')) chunkRequire = true; - } catch (e, st) { - continue; - Logger.error('[Sync-check] E: $e\n' - '$st'); - } + if(_throw) throw e; } } From 7a78a1a88d469705360773599d3dada20ac84ec7 Mon Sep 17 00:00:00 2001 From: TaYaKi71751 Date: Sat, 9 Dec 2023 12:29:49 +0900 Subject: [PATCH 2/3] Fix flutter/flutter/issues/61514 This https://github.com/flutter/flutter/issues/61514 --- lib/pages/database_download/decompress.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/pages/database_download/decompress.dart b/lib/pages/database_download/decompress.dart index 29e21c1a0..6c710519d 100644 --- a/lib/pages/database_download/decompress.dart +++ b/lib/pages/database_download/decompress.dart @@ -86,6 +86,9 @@ class P7zip { final tempDir = await getTemporaryDirectory(); final libraryFile = File('${tempDir.path}/lib7zr.so'); + if(await libraryFile.exists()){ + await libraryFile.delete(); + } final createdFile = await libraryFile.create(); final openFile = await createdFile.open(mode: FileMode.write); final writtenFile = From 760a4ca091007f2323f811b57ff5e8ac5707c653 Mon Sep 17 00:00:00 2001 From: TaYaKi71751 Date: Sat, 9 Dec 2023 13:06:02 +0900 Subject: [PATCH 3/3] Fix checkOpen --- lib/database/database.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/database/database.dart b/lib/database/database.dart index dd4373388..44b22ff7c 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -48,7 +48,11 @@ class DataBaseManager { } Future checkOpen() async { - if (!db!.isOpen) db = await openDatabase(dbPath!); + if(db != null){ + if (!db!.isOpen) db = await openDatabase(dbPath!); + } else if(db == null){ + db = await openDatabase(dbPath!); + } } Future>> query(String str) async {