diff --git a/mobile/lib/core/constants.dart b/mobile/lib/core/constants.dart index 63201412b..d8c2a6538 100644 --- a/mobile/lib/core/constants.dart +++ b/mobile/lib/core/constants.dart @@ -98,4 +98,4 @@ const blackThumbnailBase64 = '/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEB' 'AKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgD/9k='; const uploadTempFilePrefix = "upload_file_"; -final tempDirCleanUpInterval = const Duration(hours: 6).inMicroseconds; +final tempDirCleanUpInterval = const Duration(seconds: 6).inMicroseconds; diff --git a/mobile/lib/db/upload_locks_db.dart b/mobile/lib/db/upload_locks_db.dart index 107839c20..f79f291ed 100644 --- a/mobile/lib/db/upload_locks_db.dart +++ b/mobile/lib/db/upload_locks_db.dart @@ -384,26 +384,22 @@ class UploadLocksDB { ); } - Future isEncryptedPathSafeToDelete(String encryptedFileName) { - // If lastAttemptedAt exceeds 3 days or createdAt exceeds 7 days - final db = instance.database; - return db.then((db) async { + // getFileNameToLastAttemptedAtMap returns a map of encrypted file name to last attempted at time + Future> getFileNameToLastAttemptedAtMap() { + return instance.database.then((db) async { final rows = await db.query( _trackUploadTable.table, - where: '${_trackUploadTable.columnEncryptedFileName} = ?', - whereArgs: [encryptedFileName], + columns: [ + _trackUploadTable.columnEncryptedFileName, + _trackUploadTable.columnLastAttemptedAt, + ], ); - if (rows.isEmpty) { - return true; + final map = {}; + for (final row in rows) { + map[row[_trackUploadTable.columnEncryptedFileName] as String] = + row[_trackUploadTable.columnLastAttemptedAt] as int; } - final row = rows.first; - final lastAttemptedAt = - row[_trackUploadTable.columnLastAttemptedAt] as int?; - final createdAt = row[_trackUploadTable.columnCreatedAt] as int; - final now = DateTime.now().millisecondsSinceEpoch; - return (lastAttemptedAt == null || - now - lastAttemptedAt > 3 * 24 * 60 * 60 * 1000) && - now - createdAt > 7 * 24 * 60 * 60 * 1000; + return map; }); } diff --git a/mobile/lib/utils/file_uploader.dart b/mobile/lib/utils/file_uploader.dart index 2e4c178c2..572f7054f 100644 --- a/mobile/lib/utils/file_uploader.dart +++ b/mobile/lib/utils/file_uploader.dart @@ -327,8 +327,23 @@ class FileUploader { }); if (filesToDelete.isNotEmpty) { _logger.info('Deleting ${filesToDelete.length} stale upload files '); + final fileNameToLastAttempt = + await _uploadLocks.getFileNameToLastAttemptedAtMap(); for (final file in filesToDelete) { - await file.delete(); + final fileName = + file.path.split('/').last.replaceAll(uploadTempFilePrefix, ''); + final lastAttemptTime = fileNameToLastAttempt[fileName] != null + ? DateTime.fromMillisecondsSinceEpoch( + fileNameToLastAttempt[fileName]!, + ) + : null; + if (lastAttemptTime == null || + DateTime.now().difference(lastAttemptTime).inDays > 1) { + await file.delete(); + } else { + _logger.info( + 'Skipping file $fileName as it was attempted recently on $lastAttemptTime'); + } } }