mirror of
https://github.com/lifegpc/eh_downloader_flutter.git
synced 2026-07-08 01:30:28 +08:00
Add size property to database
This commit is contained in:
@@ -16,6 +16,7 @@ path TEXT,
|
|||||||
last_used INT,
|
last_used INT,
|
||||||
headers TEXT,
|
headers TEXT,
|
||||||
realUrl TEXT,
|
realUrl TEXT,
|
||||||
|
size INT,
|
||||||
PRIMARY KEY(url)
|
PRIMARY KEY(url)
|
||||||
);""";
|
);""";
|
||||||
const _allTables = ['images'];
|
const _allTables = ['images'];
|
||||||
@@ -29,6 +30,7 @@ class ImageCaches {
|
|||||||
String? _exeDir;
|
String? _exeDir;
|
||||||
final Set<String> _existingTable = {};
|
final Set<String> _existingTable = {};
|
||||||
bool _inited = false;
|
bool _inited = false;
|
||||||
|
static const version = 1;
|
||||||
ImageCaches();
|
ImageCaches();
|
||||||
Future<String?> _desktopFilePath() async {
|
Future<String?> _desktopFilePath() async {
|
||||||
final String? exe = await platformPath.getCurrentExe();
|
final String? exe = await platformPath.getCurrentExe();
|
||||||
@@ -70,6 +72,30 @@ class ImageCaches {
|
|||||||
await _updateExistsTable();
|
await _updateExistsTable();
|
||||||
final v = await _db!.getVersion();
|
final v = await _db!.getVersion();
|
||||||
_log.fine("Database version: $v");
|
_log.fine("Database version: $v");
|
||||||
|
if (v < version) {
|
||||||
|
bool needOptimized = false;
|
||||||
|
if (v < 1 && _existingTable.contains("images")) {
|
||||||
|
await _db!.execute("ALTER TABLE images ADD size INT;");
|
||||||
|
final re = await _db!.query("images", columns: ['url', 'path']);
|
||||||
|
for (final r in re) {
|
||||||
|
final f = _fs.file(r["path"] as String);
|
||||||
|
try {
|
||||||
|
final stats = await f.stat();
|
||||||
|
Map<String, Object?> map = {};
|
||||||
|
map["size"] = stats.size;
|
||||||
|
await _db!
|
||||||
|
.update("images", map, where: 'url = ?', whereArgs: [r["url"]]);
|
||||||
|
} catch (e) {
|
||||||
|
_log.warning("Failed to stat ${f.path}: $e");
|
||||||
|
await _db!
|
||||||
|
.delete("images", where: 'url = ?', whereArgs: [r["url"]]);
|
||||||
|
needOptimized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await _db!.setVersion(version);
|
||||||
|
if (needOptimized) await _optimize();
|
||||||
|
}
|
||||||
if (_allTables.length != _existingTable.length ||
|
if (_allTables.length != _existingTable.length ||
|
||||||
!_allTables.every((e) => _existingTable.contains(e))) {
|
!_allTables.every((e) => _existingTable.contains(e))) {
|
||||||
return false;
|
return false;
|
||||||
@@ -93,6 +119,10 @@ class ImageCaches {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _optimize() async {
|
||||||
|
await _db!.execute("VACUUM;");
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> init() async {
|
Future<void> init() async {
|
||||||
sqfliteFfiInit();
|
sqfliteFfiInit();
|
||||||
_db = await databaseFactoryFfi.openDatabase(await _filePath);
|
_db = await databaseFactoryFfi.openDatabase(await _filePath);
|
||||||
@@ -157,7 +187,7 @@ class ImageCaches {
|
|||||||
p = path.relative(p, from: _exeDir!);
|
p = path.relative(p, from: _exeDir!);
|
||||||
}
|
}
|
||||||
await _db!.rawInsert(
|
await _db!.rawInsert(
|
||||||
"INSERT OR REPLACE INTO images VALUES (?, ?, ?, ?, ?);",
|
"INSERT OR REPLACE INTO images VALUES (?, ?, ?, ?, ?, ?);",
|
||||||
[uri, p, lastUsed, header, realUri]);
|
[uri, p, lastUsed, header, realUri, data.length]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user