remote_sync_service.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:logging/logging.dart';
  4. import 'package:photos/core/configuration.dart';
  5. import 'package:photos/core/errors.dart';
  6. import 'package:photos/core/event_bus.dart';
  7. import 'package:photos/db/files_db.dart';
  8. import 'package:photos/events/collection_updated_event.dart';
  9. import 'package:photos/events/local_photos_updated_event.dart';
  10. import 'package:photos/events/sync_status_update_event.dart';
  11. import 'package:photos/models/file.dart';
  12. import 'package:photos/models/file_type.dart';
  13. import 'package:photos/services/collections_service.dart';
  14. import 'package:photos/services/local_sync_service.dart';
  15. import 'package:photos/utils/diff_fetcher.dart';
  16. import 'package:photos/utils/file_uploader.dart';
  17. import 'package:photos/utils/file_util.dart';
  18. class RemoteSyncService {
  19. final _logger = Logger("RemoteSyncService");
  20. final _db = FilesDB.instance;
  21. final _uploader = FileUploader.instance;
  22. final _collectionsService = CollectionsService.instance;
  23. final _diffFetcher = DiffFetcher();
  24. int _completedUploads = 0;
  25. static const kDiffLimit = 2500;
  26. static final RemoteSyncService instance =
  27. RemoteSyncService._privateConstructor();
  28. RemoteSyncService._privateConstructor();
  29. Future<void> init() async {}
  30. Future<void> sync({bool silently = false}) async {
  31. if (!Configuration.instance.hasConfiguredAccount()) {
  32. _logger.info("Skipping remote sync since account is not configured");
  33. return;
  34. }
  35. await _collectionsService.sync();
  36. final updatedCollections =
  37. await _collectionsService.getCollectionsToBeSynced();
  38. if (updatedCollections.isNotEmpty && !silently) {
  39. Bus.instance.fire(SyncStatusUpdate(SyncStatus.applying_remote_diff));
  40. }
  41. for (final c in updatedCollections) {
  42. await _syncCollectionDiff(c.id);
  43. _collectionsService.setCollectionSyncTime(c.id, c.updationTime);
  44. }
  45. bool hasUploadedFiles = await _uploadDiff();
  46. if (hasUploadedFiles) {
  47. sync(silently: true);
  48. }
  49. }
  50. Future<void> _syncCollectionDiff(int collectionID) async {
  51. final diff = await _diffFetcher.getEncryptedFilesDiff(
  52. collectionID,
  53. _collectionsService.getCollectionSyncTime(collectionID),
  54. kDiffLimit,
  55. );
  56. if (diff.updatedFiles.isNotEmpty) {
  57. await _storeDiff(diff.updatedFiles, collectionID);
  58. _logger.info("Updated " +
  59. diff.updatedFiles.length.toString() +
  60. " files in collection " +
  61. collectionID.toString());
  62. Bus.instance.fire(LocalPhotosUpdatedEvent(diff.updatedFiles));
  63. Bus.instance
  64. .fire(CollectionUpdatedEvent(collectionID, diff.updatedFiles));
  65. if (diff.fetchCount == kDiffLimit) {
  66. return await _syncCollectionDiff(collectionID);
  67. }
  68. }
  69. }
  70. Future<bool> _uploadDiff() async {
  71. final foldersToBackUp = Configuration.instance.getPathsToBackUp();
  72. final hasSelectedAllFoldersForBackup =
  73. Configuration.instance.hasSelectedAllFoldersForBackup();
  74. List<File> filesToBeUploaded;
  75. if (hasSelectedAllFoldersForBackup ||
  76. (LocalSyncService.instance.hasGrantedLimitedPermissions() &&
  77. foldersToBackUp.isEmpty)) {
  78. filesToBeUploaded = await _db.getAllLocalFiles();
  79. } else {
  80. filesToBeUploaded =
  81. await _db.getFilesToBeUploadedWithinFolders(foldersToBackUp);
  82. }
  83. if (!Configuration.instance.shouldBackupVideos()) {
  84. filesToBeUploaded
  85. .removeWhere((element) => element.fileType == FileType.video);
  86. }
  87. _logger.info(
  88. filesToBeUploaded.length.toString() + " new files to be uploaded.");
  89. final updatedFileIDs = await _db.getUploadedFileIDsToBeUpdated();
  90. _logger.info(updatedFileIDs.length.toString() + " files updated.");
  91. final editedFiles = await _db.getEditedRemoteFiles();
  92. _logger.info(editedFiles.length.toString() + " files edited.");
  93. _completedUploads = 0;
  94. int toBeUploaded =
  95. filesToBeUploaded.length + updatedFileIDs.length + editedFiles.length;
  96. if (toBeUploaded > 0) {
  97. Bus.instance.fire(SyncStatusUpdate(SyncStatus.preparing_for_upload));
  98. }
  99. final List<Future> futures = [];
  100. for (final uploadedFileID in updatedFileIDs) {
  101. final file = await _db.getUploadedFileInAnyCollection(uploadedFileID);
  102. final future = _uploader
  103. .upload(file, file.collectionID)
  104. .then((uploadedFile) => _onFileUploaded(uploadedFile));
  105. futures.add(future);
  106. }
  107. for (final file in filesToBeUploaded) {
  108. final collectionID = (await CollectionsService.instance
  109. .getOrCreateForPath(file.deviceFolder))
  110. .id;
  111. final future = _uploader
  112. .upload(file, collectionID)
  113. .then((uploadedFile) => _onFileUploaded(uploadedFile));
  114. futures.add(future);
  115. }
  116. for (final file in editedFiles) {
  117. final future = _uploader
  118. .upload(file, file.collectionID)
  119. .then((uploadedFile) => _onFileUploaded(uploadedFile));
  120. futures.add(future);
  121. }
  122. try {
  123. await Future.wait(futures);
  124. } on InvalidFileError {
  125. // Do nothing
  126. } on FileSystemException {
  127. // Do nothing since it's caused mostly due to concurrency issues
  128. // when the foreground app deletes temporary files, interrupting a background
  129. // upload
  130. } on LockAlreadyAcquiredError {
  131. // Do nothing
  132. } on SilentlyCancelUploadsError {
  133. // Do nothing
  134. } on UserCancelledUploadError {
  135. // Do nothing
  136. } catch (e) {
  137. rethrow;
  138. }
  139. return _completedUploads > 0;
  140. }
  141. Future<void> _onFileUploaded(File file) async {
  142. Bus.instance.fire(CollectionUpdatedEvent(file.collectionID, [file]));
  143. _completedUploads++;
  144. final toBeUploadedInThisSession =
  145. FileUploader.instance.getCurrentSessionUploadCount();
  146. if (toBeUploadedInThisSession == 0) {
  147. return;
  148. }
  149. if (_completedUploads > toBeUploadedInThisSession ||
  150. _completedUploads < 0 ||
  151. toBeUploadedInThisSession < 0) {
  152. _logger.severe(
  153. "Incorrect sync status",
  154. InvalidSyncStatusError("Tried to report " +
  155. _completedUploads.toString() +
  156. " as uploaded out of " +
  157. toBeUploadedInThisSession.toString()));
  158. return;
  159. }
  160. Bus.instance.fire(SyncStatusUpdate(SyncStatus.in_progress,
  161. completed: _completedUploads, total: toBeUploadedInThisSession));
  162. }
  163. Future _storeDiff(List<File> diff, int collectionID) async {
  164. int existing = 0,
  165. updated = 0,
  166. remote = 0,
  167. localButUpdatedOnRemote = 0,
  168. localButAddedToNewCollectionOnRemote = 0;
  169. List<File> toBeInserted = [];
  170. for (File file in diff) {
  171. final existingFiles = file.deviceFolder == null
  172. ? null
  173. : await _db.getMatchingFiles(file.title, file.deviceFolder);
  174. if (existingFiles == null) {
  175. // File uploaded from a different device
  176. file.localID = null;
  177. toBeInserted.add(file);
  178. remote++;
  179. } else {
  180. // File exists on device
  181. file.localID = existingFiles[0]
  182. .localID; // File should ideally have the same localID
  183. bool wasUploadedOnAPreviousInstallation =
  184. existingFiles.length == 1 && existingFiles[0].collectionID == null;
  185. if (wasUploadedOnAPreviousInstallation) {
  186. file.generatedID = existingFiles[0].generatedID;
  187. if (file.modificationTime != existingFiles[0].modificationTime) {
  188. // File was updated since the app was uninstalled
  189. _logger.info("Updated since last installation: " +
  190. file.uploadedFileID.toString());
  191. file.updationTime = null;
  192. updated++;
  193. } else {
  194. existing++;
  195. }
  196. toBeInserted.add(file);
  197. } else {
  198. bool foundMatchingCollection = false;
  199. for (final existingFile in existingFiles) {
  200. if (file.collectionID == existingFile.collectionID &&
  201. file.uploadedFileID == existingFile.uploadedFileID) {
  202. // File was updated on remote
  203. foundMatchingCollection = true;
  204. file.generatedID = existingFile.generatedID;
  205. toBeInserted.add(file);
  206. clearCache(file);
  207. localButUpdatedOnRemote++;
  208. break;
  209. }
  210. }
  211. if (!foundMatchingCollection) {
  212. // Added to a new collection
  213. toBeInserted.add(file);
  214. localButAddedToNewCollectionOnRemote++;
  215. }
  216. }
  217. }
  218. }
  219. await _db.insertMultiple(toBeInserted);
  220. if (toBeInserted.isNotEmpty) {
  221. await _collectionsService.setCollectionSyncTime(
  222. collectionID, toBeInserted[toBeInserted.length - 1].updationTime);
  223. }
  224. _logger.info(
  225. "Diff to be deduplicated was: " +
  226. diff.length.toString() +
  227. " out of which \n" +
  228. existing.toString() +
  229. " was uploaded from device, \n" +
  230. updated.toString() +
  231. " was uploaded from device, but has been updated since and should be reuploaded, \n" +
  232. remote.toString() +
  233. " was uploaded from remote, \n" +
  234. localButUpdatedOnRemote.toString() +
  235. " was uploaded from device but updated on remote, and \n" +
  236. localButAddedToNewCollectionOnRemote.toString() +
  237. " was uploaded from device but added to a new collection on remote.",
  238. );
  239. }
  240. }