Sync mapping from device albums to files table

This commit is contained in:
Neeraj Gupta 2022-08-26 16:30:22 +05:30
parent 22cdf59f6e
commit 7b043fff30
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
2 changed files with 67 additions and 0 deletions

View file

@ -801,6 +801,28 @@ class FilesDB {
return result;
}
// Sets the collectionID for the files with given LocalIDs if the
// corresponding file entries are not already mapped to some other collection
Future<int> setCollectionIDForUnMappedLocalFiles(
int collectionID,
Set<String> localIDs,
) async {
final db = await instance.database;
String inParam = "";
for (final localID in localIDs) {
inParam += "'" + localID + "',";
}
inParam = inParam.substring(0, inParam.length - 1);
return await db.rawUpdate(
'''
UPDATE $filesTable
SET $columnCollectionID = $collectionID
WHERE $columnLocalID IN ($inParam) AND ($columnCollectionID IS NULL OR
$columnCollectionID = -1);
''',
);
}
Future<int> getNumberOfUploadedFiles() async {
final db = await instance.database;
final rows = await db.query(

View file

@ -235,10 +235,16 @@ class RemoteSyncService {
B) Check if
*/
for (final eachDevicePath in devicePathCollections) {
debugPrint("Processing ${eachDevicePath.name}");
String pathID = eachDevicePath.id;
Set<String> unSyncedLocalIDs =
unSyncedPathIdToLocalIDs[eachDevicePath.id] ?? {};
if (unSyncedLocalIDs.isNotEmpty && eachDevicePath.collectionID != -1) {
await fileDb.setCollectionIDForUnMappedLocalFiles(
eachDevicePath.collectionID,
unSyncedLocalIDs,
);
// mark IDs as already synced if corresponding entry is present in
// the collection. This can happen when a user has marked a folder
// for sync, then un-synced it and again tries to mark if for sync.
@ -258,6 +264,45 @@ class RemoteSyncService {
);
unSyncedLocalIDs.removeAll(commonElements);
}
// At this point, the remaining unSyncedLocalIDs will need to create
// new file entries, where we can store mapping for localID and
// corresponding collection ID
if (unSyncedLocalIDs.isNotEmpty) {
debugPrint(
'Adding new entries for ${unSyncedLocalIDs.length} files'
' for ${eachDevicePath.name}',
);
var filesWithPotentialCollectionID =
await fileDb.getLocalFiles(unSyncedLocalIDs.toList());
List<File> newFilesToInsert = [];
final Set<String> fileFoundForLocalIDs = {};
for (var existingFile in filesWithPotentialCollectionID) {
String localID = existingFile.localID;
if (localID != null && !fileFoundForLocalIDs.contains(localID)) {
existingFile.collectionID = eachDevicePath.collectionID;
existingFile.uploadedFileID = -1;
existingFile.ownerID = null;
existingFile.generatedID = null;
newFilesToInsert.add(existingFile);
fileFoundForLocalIDs.add(localID);
}
}
await fileDb.insertMultiple(newFilesToInsert);
await fileDb.insertPathIDToLocalIDMapping(
{pathID: fileFoundForLocalIDs},
conflictAlgorithm: ConflictAlgorithm.replace,
syncStatus: true,
);
unSyncedLocalIDs.removeAll(fileFoundForLocalIDs);
}
if (unSyncedLocalIDs.isNotEmpty) {
_logger.warning(
"All localIDs should be synced, missed for "
"${unSyncedLocalIDs.length}",
);
}
}
}
}