[mob][config] Skip deleting temp encrypted files for upload

This commit is contained in:
Neeraj Gupta 2024-05-08 11:04:54 +05:30
parent 588bf74202
commit 2de5b641a7

View file

@ -103,20 +103,7 @@ class Configuration {
_documentsDirectory = (await getApplicationDocumentsDirectory()).path;
_tempDocumentsDirPath = _documentsDirectory + "/temp/";
final tempDocumentsDir = Directory(_tempDocumentsDirPath);
try {
final currentTime = DateTime.now().microsecondsSinceEpoch;
if (tempDocumentsDir.existsSync() &&
(_preferences.getInt(lastTempFolderClearTimeKey) ?? 0) <
(currentTime - kTempFolderDeletionTimeBuffer)) {
await tempDocumentsDir.delete(recursive: true);
await _preferences.setInt(lastTempFolderClearTimeKey, currentTime);
_logger.info("Cleared temp folder");
} else {
_logger.info("Skipping temp folder clear");
}
} catch (e) {
_logger.warning(e);
}
await _cleanUpStaleFiles(tempDocumentsDir);
tempDocumentsDir.createSync(recursive: true);
final tempDirectoryPath = (await getTemporaryDirectory()).path;
_thumbnailCacheDirectory = tempDirectoryPath + "/thumbnail-cache";
@ -144,6 +131,40 @@ class Configuration {
SuperLogging.setUserID(await _getOrCreateAnonymousUserID()).ignore();
}
// _cleanUpStaleFiles deletes all files in the temp directory that are older
// than kTempFolderDeletionTimeBuffer except the the temp encrypted files for upload.
// Those file are deleted by file uploader after the upload is complete or those
// files are not being used / tracked.
Future<void> _cleanUpStaleFiles(Directory tempDocumentsDir) async {
try {
final currentTime = DateTime.now().microsecondsSinceEpoch;
if (tempDocumentsDir.existsSync() &&
(_preferences.getInt(lastTempFolderClearTimeKey) ?? 0) <
(currentTime - kTempFolderDeletionTimeBuffer)) {
// list all files in the temp directory
final files = tempDocumentsDir.listSync();
for (final file in files) {
if (file is File) {
if (file.path.contains(uploadTempFilePrefix)) {
continue;
}
_logger.info("Deleting file: ${file.path}");
await file.delete();
} else if (file is Directory) {
await file.delete(recursive: true);
}
}
// await tempDocumentsDir.delete(recursive: true);
await _preferences.setInt(lastTempFolderClearTimeKey, currentTime);
_logger.info("Cleared temp folder");
} else {
_logger.info("Skipping temp folder clear");
}
} catch (e) {
_logger.warning(e);
}
}
Future<void> logout({bool autoLogout = false}) async {
if (SyncService.instance.isSyncInProgress()) {
SyncService.instance.stopSync();