ente/lib/services/sync_service.dart

231 lines
7.4 KiB
Dart
Raw Normal View History

2020-03-30 14:28:46 +00:00
import 'dart:async';
import 'dart:io';
2020-11-16 16:35:16 +00:00
import 'package:connectivity/connectivity.dart';
import 'package:dio/dio.dart';
2020-05-02 16:28:54 +00:00
import 'package:logging/logging.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/constants.dart';
2021-02-26 09:21:47 +00:00
import 'package:photos/core/errors.dart';
import 'package:photos/core/event_bus.dart';
2020-11-19 18:22:30 +00:00
import 'package:photos/core/network.dart';
2021-06-28 06:40:19 +00:00
import 'package:photos/db/files_db.dart';
2021-05-29 17:01:59 +00:00
import 'package:photos/events/permission_granted_event.dart';
2021-02-02 16:35:38 +00:00
import 'package:photos/events/subscription_purchased_event.dart';
import 'package:photos/events/sync_status_update_event.dart';
2021-03-17 22:08:13 +00:00
import 'package:photos/events/trigger_logout_event.dart';
2021-06-28 06:40:19 +00:00
import 'package:photos/models/backup_status.dart';
import 'package:photos/models/file_type.dart';
import 'package:photos/services/local_sync_service.dart';
import 'package:photos/services/notification_service.dart';
import 'package:photos/services/remote_sync_service.dart';
2020-10-03 17:58:26 +00:00
import 'package:photos/utils/file_uploader.dart';
2020-03-24 19:59:36 +00:00
import 'package:shared_preferences/shared_preferences.dart';
2020-04-30 15:09:41 +00:00
2020-10-03 17:58:26 +00:00
class SyncService {
final _logger = Logger("SyncService");
final _localSyncService = LocalSyncService.instance;
final _remoteSyncService = RemoteSyncService.instance;
2020-11-19 18:22:30 +00:00
final _dio = Network.instance.getDio();
2020-10-21 16:20:41 +00:00
final _uploader = FileUploader.instance;
bool _syncStopRequested = false;
2021-03-17 21:07:17 +00:00
Completer<bool> _existingSync;
SharedPreferences _prefs;
2020-11-16 16:35:16 +00:00
SyncStatusUpdate _lastSyncStatusEvent;
2020-04-27 13:02:29 +00:00
static const kLastStorageLimitExceededNotificationPushTime =
"last_storage_limit_exceeded_notification_push_time";
2020-03-24 19:59:36 +00:00
2020-10-03 17:58:26 +00:00
SyncService._privateConstructor() {
2021-02-02 16:35:38 +00:00
Bus.instance.on<SubscriptionPurchasedEvent>().listen((event) {
2021-03-02 07:20:21 +00:00
_uploader.clearQueue(SilentlyCancelUploadsError());
2020-04-30 15:09:41 +00:00
sync();
});
2020-11-16 16:35:16 +00:00
Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
_logger.info("Connectivity change detected " + result.toString());
2021-05-19 15:49:04 +00:00
if (Configuration.instance.hasConfiguredAccount()) {
sync();
}
2020-11-16 16:35:16 +00:00
});
Bus.instance.on<SyncStatusUpdate>().listen((event) {
2021-03-22 07:34:59 +00:00
_logger.info("Sync status received " + event.toString());
2020-11-16 16:35:16 +00:00
_lastSyncStatusEvent = event;
});
2020-04-30 15:09:41 +00:00
}
2020-10-03 17:58:26 +00:00
static final SyncService instance = SyncService._privateConstructor();
2020-03-28 13:56:06 +00:00
Future<void> init() async {
_prefs = await SharedPreferences.getInstance();
if (Platform.isIOS) {
_logger.info("Clearing file cache");
await PhotoManager.clearFileCache();
_logger.info("Cleared file cache");
}
}
2021-03-17 21:07:17 +00:00
Future<bool> existingSync() async {
return _existingSync.future;
}
Future<bool> sync() async {
_syncStopRequested = false;
2021-03-01 23:37:24 +00:00
if (_existingSync != null) {
2020-05-02 16:28:54 +00:00
_logger.warning("Sync already in progress, skipping.");
2021-03-01 23:37:24 +00:00
return _existingSync.future;
2020-04-27 13:02:29 +00:00
}
2021-03-17 21:07:17 +00:00
_existingSync = Completer<bool>();
2021-03-01 23:37:24 +00:00
bool successful = false;
try {
await _doSync();
2021-03-25 19:06:01 +00:00
if (_lastSyncStatusEvent != null &&
_lastSyncStatusEvent.status !=
SyncStatus.completed_first_gallery_import &&
_lastSyncStatusEvent.status != SyncStatus.completed_backup) {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.completed_backup));
}
2021-03-01 23:37:24 +00:00
successful = true;
} on WiFiUnavailableError {
_logger.warning("Not uploading over mobile data");
Bus.instance.fire(
2021-03-02 07:20:21 +00:00
SyncStatusUpdate(SyncStatus.paused, reason: "waiting for WiFi..."));
2021-03-01 23:37:24 +00:00
} on SyncStopRequestedError {
_syncStopRequested = false;
2021-04-19 10:58:07 +00:00
Bus.instance.fire(
SyncStatusUpdate(SyncStatus.completed_backup, wasStopped: true));
2021-03-01 23:37:24 +00:00
} on NoActiveSubscriptionError {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error,
error: NoActiveSubscriptionError()));
} on StorageLimitExceededError {
_showStorageLimitExceededNotification();
2021-03-01 23:37:24 +00:00
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error,
error: StorageLimitExceededError()));
2021-03-17 22:08:13 +00:00
} on UnauthorizedError {
_logger.info("Logging user out");
Bus.instance.fire(TriggerLogoutEvent());
2021-03-01 23:37:24 +00:00
} catch (e, s) {
2021-05-02 16:05:36 +00:00
if (e is DioError) {
if (e.type == DioErrorType.connectTimeout ||
e.type == DioErrorType.sendTimeout ||
e.type == DioErrorType.receiveTimeout ||
e.type == DioErrorType.other) {
2021-03-07 05:25:07 +00:00
Bus.instance.fire(SyncStatusUpdate(SyncStatus.paused,
reason: "waiting for network..."));
2021-11-15 14:01:04 +00:00
_logger.severe("unable to connect", e, StackTrace.current);
2021-03-17 21:07:17 +00:00
return false;
2021-03-07 05:25:07 +00:00
}
2021-03-02 07:20:21 +00:00
}
2021-11-15 14:01:04 +00:00
_logger.severe("backup failed", e, StackTrace.current);
2021-06-12 10:33:48 +00:00
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));
2021-07-22 18:41:58 +00:00
rethrow;
2021-03-01 23:37:24 +00:00
} finally {
2021-03-17 21:07:17 +00:00
_existingSync.complete(successful);
2021-03-01 23:37:24 +00:00
_existingSync = null;
2021-03-25 19:06:01 +00:00
_lastSyncStatusEvent = null;
2021-03-01 23:37:24 +00:00
_logger.info("Syncing completed");
}
2021-03-17 21:07:17 +00:00
return successful;
}
2020-04-30 15:09:41 +00:00
void stopSync() {
_logger.info("Sync stop requested");
_syncStopRequested = true;
}
bool shouldStopSync() {
return _syncStopRequested;
}
2020-11-12 16:32:10 +00:00
bool isSyncInProgress() {
2021-03-01 23:37:24 +00:00
return _existingSync != null;
2020-11-12 16:32:10 +00:00
}
2020-11-16 16:35:16 +00:00
SyncStatusUpdate getLastSyncStatusEvent() {
return _lastSyncStatusEvent;
}
Future<void> onPermissionGranted(PermissionState state) async {
2021-07-01 09:29:25 +00:00
_logger.info("Permission granted " + state.toString());
await _localSyncService.onPermissionGranted(state);
2021-05-29 17:01:59 +00:00
Bus.instance.fire(PermissionGrantedEvent());
2021-03-12 08:40:36 +00:00
_doSync();
}
void onFoldersSet(Set<String> paths) {
_uploader.removeFromQueueWhere((file) {
return !paths.contains(file.deviceFolder);
}, UserCancelledUploadError());
}
void onVideoBackupPaused() {
_uploader.removeFromQueueWhere((file) {
return file.fileType == FileType.video;
}, UserCancelledUploadError());
}
2021-04-27 20:49:00 +00:00
Future<void> deleteFilesOnServer(List<int> fileIDs) async {
2021-06-14 19:10:28 +00:00
return await _dio.post(
Configuration.instance.getHttpEndpoint() + "/files/delete",
options: Options(
headers: {
"X-Auth-Token": Configuration.instance.getToken(),
},
),
data: {
"fileIDs": fileIDs,
},
);
2020-03-29 14:04:26 +00:00
}
2021-06-28 06:40:19 +00:00
Future<BackupStatus> getBackupStatus() async {
final ids = await FilesDB.instance.getBackedUpIDs();
final size = await _getFileSize(ids.uploadedIDs);
return BackupStatus(ids.localIDs, size);
}
Future<int> _getFileSize(List<int> fileIDs) async {
try {
final response = await _dio.post(
Configuration.instance.getHttpEndpoint() + "/files/size",
options: Options(
headers: {
"X-Auth-Token": Configuration.instance.getToken(),
},
),
data: {
"fileIDs": fileIDs,
},
);
return response.data["size"];
} catch (e) {
_logger.severe(e);
2021-07-22 18:41:58 +00:00
rethrow;
2021-06-28 06:40:19 +00:00
}
}
2021-06-14 16:27:50 +00:00
Future<void> _doSync() async {
await _localSyncService.sync();
if (_localSyncService.hasCompletedFirstImport()) {
await _remoteSyncService.sync();
2021-06-15 11:57:33 +00:00
final shouldSync = await _localSyncService.syncAll();
if (shouldSync) {
await _remoteSyncService.sync();
}
2021-06-14 16:27:50 +00:00
}
}
void _showStorageLimitExceededNotification() async {
final lastNotificationShownTime =
_prefs.getInt(kLastStorageLimitExceededNotificationPushTime) ?? 0;
final now = DateTime.now().microsecondsSinceEpoch;
2021-07-21 20:47:43 +00:00
if ((now - lastNotificationShownTime) > kMicroSecondsInDay) {
await _prefs.setInt(kLastStorageLimitExceededNotificationPushTime, now);
NotificationService.instance.showNotification(
"storage limit exceeded", "sorry, we had to pause your backups");
}
}
2020-03-24 19:59:36 +00:00
}