Skip to content

Commit b6ec07d

Browse files
committed
drop corrupted db (PR by @turekj: Baseflow#407)
1 parent 4199a1c commit b6ec07d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ class CacheObjectProvider extends CacheInfoRepository
2525
if (!shouldOpenOnNewConnection()) {
2626
return openCompleter!.future;
2727
}
28+
2829
final path = await _getPath();
30+
2931
await File(path).parent.create(recursive: true);
32+
await _dropCorruptedDb(path);
33+
3034
db = await openDatabase(path, version: 3,
3135
onCreate: (Database db, int version) async {
3236
await db.execute('''
@@ -214,4 +218,41 @@ class CacheObjectProvider extends CacheInfoRepository
214218
}
215219
}
216220
}
221+
222+
static Future<void> _dropCorruptedDb(String path) async {
223+
bool exists = false;
224+
225+
try {
226+
exists = await File(path).exists();
227+
} on FileSystemException {
228+
// We can not read the file, so we assume it does not exist.
229+
} catch (e) {
230+
// We can not read the file, so we assume it does not exist.
231+
}
232+
233+
if (!exists) {
234+
return;
235+
}
236+
237+
Database? pragmaDb;
238+
bool isIntegral = true;
239+
240+
try {
241+
pragmaDb = await openDatabase(path);
242+
final check = await pragmaDb.rawQuery("pragma integrity_check");
243+
isIntegral = check.length == 1 && check[0].values.first == "ok";
244+
} on DatabaseException {
245+
isIntegral = false;
246+
} catch (e) {
247+
isIntegral = false;
248+
} finally {
249+
if (pragmaDb != null) {
250+
await pragmaDb.close();
251+
}
252+
}
253+
254+
if (!isIntegral) {
255+
await deleteDatabase(path);
256+
}
257+
}
217258
}

0 commit comments

Comments
 (0)