Prevent duplicate uploads during reinstallations

This commit is contained in:
Vishnu Mohandas 2021-01-02 14:34:36 +05:30
parent c4cc453541
commit b699a94b58
3 changed files with 28 additions and 11 deletions

View file

@ -425,18 +425,16 @@ class FilesDB {
String title,
String deviceFolder,
int creationTime,
int modificationTime,
) async {
final db = await instance.database;
final rows = await db.query(
table,
where: '''$columnTitle=? AND $columnDeviceFolder=? AND
$columnCreationTime=? AND $columnModificationTime=?''',
$columnCreationTime=?''',
whereArgs: [
title,
deviceFolder,
creationTime,
modificationTime,
],
);
if (rows.isNotEmpty) {

View file

@ -203,12 +203,15 @@ class SyncService {
_collectionsService.getCollectionSyncTime(collectionID),
_diffLimit,
);
if (diff.isNotEmpty) {
await _storeDiff(diff, collectionID);
_logger.info("Updated files in collection " + collectionID.toString());
if (diff.updatedFiles.isNotEmpty) {
await _storeDiff(diff.updatedFiles, collectionID);
_logger.info("Updated " +
diff.updatedFiles.length.toString() +
" files in collection " +
collectionID.toString());
FileRepository.instance.reloadFiles();
Bus.instance.fire(CollectionUpdatedEvent(collectionID: collectionID));
if (diff.length == _diffLimit) {
if (diff.fetchCount == _diffLimit) {
return await _fetchEncryptedFilesDiff(collectionID);
}
}
@ -289,10 +292,12 @@ class SyncService {
Future _storeDiff(List<File> diff, int collectionID) async {
for (File file in diff) {
final existingFiles = await _db.getMatchingFiles(file.title,
file.deviceFolder, file.creationTime, file.modificationTime);
final existingFiles = await _db.getMatchingFiles(
file.title, file.deviceFolder, file.creationTime);
if (existingFiles == null) {
// File uploaded from a different device
_logger.info("Could not find a matching file for " +
file.uploadedFileID.toString());
file.localID = null;
await _db.insert(file);
} else {
@ -303,6 +308,12 @@ class SyncService {
existingFiles.length == 1 && existingFiles[0].collectionID == null;
if (wasUploadedOnAPreviousInstallation) {
file.generatedID = existingFiles[0].generatedID;
if (file.modificationTime != existingFiles[0].modificationTime) {
// File was updated since the app was uninstalled
_logger.info("Updated since last installation: " +
file.uploadedFileID.toString());
file.updationTime = null;
}
await _db.update(file);
} else {
bool foundMatchingCollection = false;

View file

@ -18,7 +18,7 @@ class DiffFetcher {
final _logger = Logger("FileDownloader");
final _dio = Network.instance.getDio();
Future<List<File>> getEncryptedFilesDiff(
Future<Diff> getEncryptedFilesDiff(
int collectionID, int sinceTime, int limit) async {
return _dio
.get(
@ -75,10 +75,18 @@ class DiffFetcher {
file.applyMetadata(metadata);
files.add(file);
}
return Diff(files, diff.length);
} else {
Bus.instance.fire(RemoteSyncEvent(false));
return Diff(List<File>(), 0);
}
return files;
});
}
}
class Diff {
final List<File> updatedFiles;
final int fetchCount;
Diff(this.updatedFiles, this.fetchCount);
}