[mob][multipart] Avoid deleting multipart enc files if upload is in progress

This commit is contained in:
Neeraj Gupta 2024-05-08 17:04:39 +05:30
parent 92357f697d
commit f4845baa9b
3 changed files with 29 additions and 18 deletions

View file

@ -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;

View file

@ -384,26 +384,22 @@ class UploadLocksDB {
);
}
Future<bool> 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<Map<String, int>> 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 = <String, int>{};
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;
});
}

View file

@ -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');
}
}
}