|
@@ -52,6 +52,14 @@ class RemoteSyncService {
|
|
|
|
|
|
Future<void> init() async {
|
|
Future<void> init() async {
|
|
_prefs = await SharedPreferences.getInstance();
|
|
_prefs = await SharedPreferences.getInstance();
|
|
|
|
+
|
|
|
|
+ Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) async {
|
|
|
|
+ if (event.type == EventType.addedOrUpdated) {
|
|
|
|
+ if (_existingSync == null) {
|
|
|
|
+ sync();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
Future<void> sync({bool silently = false}) async {
|
|
Future<void> sync({bool silently = false}) async {
|
|
@@ -65,20 +73,8 @@ class RemoteSyncService {
|
|
}
|
|
}
|
|
_existingSync = Completer<void>();
|
|
_existingSync = Completer<void>();
|
|
|
|
|
|
- bool isFirstSync = !_collectionsService.hasSyncedCollections();
|
|
|
|
-
|
|
|
|
try {
|
|
try {
|
|
- await _collectionsService.sync();
|
|
|
|
-
|
|
|
|
- if (isFirstSync || _hasReSynced()) {
|
|
|
|
- await _syncUpdatedCollections(silently);
|
|
|
|
- } else {
|
|
|
|
- final syncSinceTime = _getSinceTimeForReSync();
|
|
|
|
- await _resyncAllCollectionsSinceTime(syncSinceTime);
|
|
|
|
- }
|
|
|
|
- if (!_hasReSynced()) {
|
|
|
|
- await _markReSyncAsDone();
|
|
|
|
- }
|
|
|
|
|
|
+ await _pullDiff(silently);
|
|
// sync trash but consume error during initial launch.
|
|
// sync trash but consume error during initial launch.
|
|
// this is to ensure that we don't pause upload due to any error during
|
|
// this is to ensure that we don't pause upload due to any error during
|
|
// the trash sync. Impact: We may end up re-uploading a file which was
|
|
// the trash sync. Impact: We may end up re-uploading a file which was
|
|
@@ -86,13 +82,23 @@ class RemoteSyncService {
|
|
await TrashSyncService.instance
|
|
await TrashSyncService.instance
|
|
.syncTrash()
|
|
.syncTrash()
|
|
.onError((e, s) => _logger.severe('trash sync failed', e, s));
|
|
.onError((e, s) => _logger.severe('trash sync failed', e, s));
|
|
- bool hasUploadedFiles = await _uploadDiff();
|
|
|
|
- _existingSync.complete();
|
|
|
|
- _existingSync = null;
|
|
|
|
- if (hasUploadedFiles && !_shouldThrottleSync()) {
|
|
|
|
- // Skipping a resync to ensure that files that were ignored in this
|
|
|
|
- // session are not processed now
|
|
|
|
- sync(silently: true);
|
|
|
|
|
|
+ final filesToBeUploaded = await _getFilesToBeUploaded();
|
|
|
|
+ final hasUploadedFiles = await _uploadFiles(filesToBeUploaded);
|
|
|
|
+ if (hasUploadedFiles) {
|
|
|
|
+ await _pullDiff(true);
|
|
|
|
+ _existingSync.complete();
|
|
|
|
+ _existingSync = null;
|
|
|
|
+ final hasMoreFilesToBackup = (await _getFilesToBeUploaded()).isNotEmpty;
|
|
|
|
+ if (hasMoreFilesToBackup && !_shouldThrottleSync()) {
|
|
|
|
+ // Skipping a resync to ensure that files that were ignored in this
|
|
|
|
+ // session are not processed now
|
|
|
|
+ sync();
|
|
|
|
+ } else {
|
|
|
|
+ Bus.instance.fire(SyncStatusUpdate(SyncStatus.completed_backup));
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ _existingSync.complete();
|
|
|
|
+ _existingSync = null;
|
|
}
|
|
}
|
|
} catch (e, s) {
|
|
} catch (e, s) {
|
|
_logger.severe("Error executing remote sync ", e, s);
|
|
_logger.severe("Error executing remote sync ", e, s);
|
|
@@ -101,6 +107,21 @@ class RemoteSyncService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ Future<void> _pullDiff(bool silently) async {
|
|
|
|
+ final isFirstSync = !_collectionsService.hasSyncedCollections();
|
|
|
|
+ await _collectionsService.sync();
|
|
|
|
+
|
|
|
|
+ if (isFirstSync || _hasReSynced()) {
|
|
|
|
+ await _syncUpdatedCollections(silently);
|
|
|
|
+ } else {
|
|
|
|
+ final syncSinceTime = _getSinceTimeForReSync();
|
|
|
|
+ await _resyncAllCollectionsSinceTime(syncSinceTime);
|
|
|
|
+ }
|
|
|
|
+ if (!_hasReSynced()) {
|
|
|
|
+ await _markReSyncAsDone();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
Future<void> _syncUpdatedCollections(bool silently) async {
|
|
Future<void> _syncUpdatedCollections(bool silently) async {
|
|
final updatedCollections =
|
|
final updatedCollections =
|
|
await _collectionsService.getCollectionsToBeSynced();
|
|
await _collectionsService.getCollectionsToBeSynced();
|
|
@@ -159,7 +180,7 @@ class RemoteSyncService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- Future<bool> _uploadDiff() async {
|
|
|
|
|
|
+ Future<List<File>> _getFilesToBeUploaded() async {
|
|
final foldersToBackUp = Configuration.instance.getPathsToBackUp();
|
|
final foldersToBackUp = Configuration.instance.getPathsToBackUp();
|
|
List<File> filesToBeUploaded;
|
|
List<File> filesToBeUploaded;
|
|
if (LocalSyncService.instance.hasGrantedLimitedPermissions() &&
|
|
if (LocalSyncService.instance.hasGrantedLimitedPermissions() &&
|
|
@@ -186,7 +207,10 @@ class RemoteSyncService {
|
|
_moveVideosToEnd(filesToBeUploaded);
|
|
_moveVideosToEnd(filesToBeUploaded);
|
|
_logger.info(
|
|
_logger.info(
|
|
filesToBeUploaded.length.toString() + " new files to be uploaded.");
|
|
filesToBeUploaded.length.toString() + " new files to be uploaded.");
|
|
|
|
+ return filesToBeUploaded;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ Future<bool> _uploadFiles(List<File> filesToBeUploaded) async {
|
|
final updatedFileIDs = await _db.getUploadedFileIDsToBeUpdated();
|
|
final updatedFileIDs = await _db.getUploadedFileIDsToBeUpdated();
|
|
_logger.info(updatedFileIDs.length.toString() + " files updated.");
|
|
_logger.info(updatedFileIDs.length.toString() + " files updated.");
|
|
|
|
|