Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path exception error while trying to delete cached files #1194

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 28 additions & 34 deletions uni/lib/controller/cleanup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uni/controller/local_storage/database/app_bus_stop_database.dart';
Expand All @@ -22,24 +23,19 @@ Future<void> cleanupStoredData(BuildContext context) async {
final faculties = PreferencesController.getUserFaculties();
await prefs.clear();

unawaited(
Future.wait([
AppLecturesDatabase().deleteLectures(),
AppExamsDatabase().deleteExams(),
AppCoursesDatabase().deleteCourses(),
AppUserDataDatabase().deleteUserData(),
AppLastUserInfoUpdateDatabase().deleteLastUpdate(),
AppBusStopDatabase().deleteBusStops(),
AppCourseUnitsDatabase().deleteCourseUnits(),
NetworkRouter.killSigarraAuthentication(faculties),
]),
);
await Future.wait([
AppLecturesDatabase().deleteLectures(),
AppExamsDatabase().deleteExams(),
AppCoursesDatabase().deleteCourses(),
AppUserDataDatabase().deleteUserData(),
AppLastUserInfoUpdateDatabase().deleteLastUpdate(),
AppBusStopDatabase().deleteBusStops(),
AppCourseUnitsDatabase().deleteCourseUnits(),
NetworkRouter.killSigarraAuthentication(faculties),
]);

final path = (await getApplicationDocumentsDirectory()).path;
final directory = Directory(path);
if (directory.existsSync()) {
directory.deleteSync(recursive: true);
}
final toCleanDirectory = await getApplicationDocumentsDirectory();
await cleanDirectory(toCleanDirectory, DateTime.now());
}

Future<void> cleanupCachedFiles() async {
Expand All @@ -53,26 +49,24 @@ Future<void> cleanupCachedFiles() async {

final toCleanDirectory = await getApplicationDocumentsDirectory();
final threshold = DateTime.now().subtract(const Duration(days: 30));
final directories = toCleanDirectory.listSync(followLinks: false);

for (final directory in directories) {
if (directory is Directory) {
final files = directory.listSync(recursive: true, followLinks: false);
await cleanDirectory(toCleanDirectory, threshold);

final oldFiles = files.where((file) {
try {
final fileDate = File(file.path).lastModifiedSync();
return fileDate.isBefore(threshold);
} catch (e) {
return false;
}
});
await PreferencesController.setLastCleanUpDate(DateTime.now());
}

for (final file in oldFiles) {
await File(file.path).delete();
}
Future<void> cleanDirectory(Directory directory, DateTime threshold) async {
final entities = directory.listSync(recursive: true, followLinks: false);
final toDeleteEntities = entities.whereType<File>().where((file) {
try {
final fileDate = file.lastModifiedSync();
return fileDate.isBefore(threshold) && path.extension(file.path) != '.db';
} catch (e) {
return false;
}
}
});

await PreferencesController.setLastCleanUpDate(DateTime.now());
for (final entity in toDeleteEntities) {
entity.deleteSync();
}
}
Loading