Skip to content

Commit 56d70b5

Browse files
authored
chore: delete resources that failed checksum validation (#959)
* Delete resources which failed checksum validation * Add unit test
1 parent 69acdc9 commit 56d70b5

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

flutter/lib/resources/cache_manager.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ class CacheManager {
7373
}
7474
}
7575

76+
Future<void> deleteFiles(List<String> resources) async {
77+
for (final resource in resources) {
78+
final filePath = get(resource);
79+
if (filePath == null) continue;
80+
final file = File(filePath);
81+
if (await file.exists()) await file.delete();
82+
print('Deleted resource $resource stored at ${file.path}');
83+
}
84+
}
85+
7686
Future<void> purgeOutdatedCache(int atLeastDaysOld) async {
7787
var currentResources = <String>[];
7888
for (var r in _resourcesMap.values) {

flutter/lib/resources/resource_manager.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ class ResourceManager {
129129

130130
final checksumFailed = await validateResourcesChecksum(resources);
131131
if (checksumFailed.isNotEmpty) {
132-
final mismatchedPaths = checksumFailed.map((e) => '\n${e.path}').join();
133-
throw 'Checksum validation failed for: $mismatchedPaths';
132+
final checksumFailedPathString =
133+
checksumFailed.map((e) => '\n${e.path}').join();
134+
final checksumFailedPaths = checksumFailed.map((e) => e.path).toList();
135+
await cacheManager.deleteFiles(checksumFailedPaths);
136+
throw 'Checksum validation failed for: $checksumFailedPathString. \nPlease download the missing files again.';
134137
}
135138

136139
// delete downloaded archives to free up disk space

flutter/unit_test/resources/cache_manager_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ void main() async {
5959
expect(manager.getArchive(txtRemotePath), isNull);
6060
});
6161

62+
test('deleteFiles', () async {
63+
await manager.deleteFiles(paths);
64+
final txtLocalPath = manager.get(txtRemotePath);
65+
expect(txtLocalPath, isNotNull);
66+
expect(await File(txtLocalPath!).exists(), isFalse,
67+
reason: 'file should not exist at path [$txtLocalPath]');
68+
});
69+
6270
test('deleteLoadedResources', () async {
6371
await manager.deleteLoadedResources(paths);
6472

0 commit comments

Comments
 (0)