ente/lib/services/sync_service.dart

367 lines
13 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';
2020-11-02 10:57:55 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
2020-05-02 16:28:54 +00:00
import 'package:logging/logging.dart';
import 'package:photos/core/cache/thumbnail_cache_manager.dart';
import 'package:photos/core/cache/video_cache_manager.dart';
import 'package:photos/core/event_bus.dart';
2020-11-19 18:22:30 +00:00
import 'package:photos/core/network.dart';
2020-07-20 11:03:09 +00:00
import 'package:photos/db/files_db.dart';
import 'package:photos/events/collection_updated_event.dart';
2020-11-16 16:35:16 +00:00
import 'package:photos/events/sync_status_update_event.dart';
import 'package:photos/events/user_authenticated_event.dart';
2020-11-02 10:57:55 +00:00
import 'package:photos/models/file_type.dart';
import 'package:photos/services/collections_service.dart';
import 'package:photos/utils/date_time_util.dart';
2020-10-03 17:58:26 +00:00
import 'package:photos/utils/file_downloader.dart';
import 'package:photos/repositories/file_repository.dart';
2020-03-24 19:59:36 +00:00
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/utils/file_sync_util.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';
import 'package:dio/dio.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file.dart';
2020-03-26 14:39:31 +00:00
import 'package:photos/core/configuration.dart';
2020-04-30 15:09:41 +00:00
2020-10-03 17:58:26 +00:00
class SyncService {
final _logger = Logger("SyncService");
2020-11-19 18:22:30 +00:00
final _dio = Network.instance.getDio();
2020-07-20 11:03:09 +00:00
final _db = FilesDB.instance;
2020-10-21 16:20:41 +00:00
final _uploader = FileUploader.instance;
2020-10-28 15:45:05 +00:00
final _collectionsService = CollectionsService.instance;
final _downloader = DiffFetcher();
2020-04-30 15:09:41 +00:00
bool _isSyncInProgress = false;
bool _syncStopRequested = false;
Future<void> _existingSync;
SharedPreferences _prefs;
2020-11-16 16:35:16 +00:00
SyncStatusUpdate _lastSyncStatusEvent;
2020-04-27 13:02:29 +00:00
2020-08-11 23:04:16 +00:00
static final _dbUpdationTimeKey = "db_updation_time";
2020-12-03 22:19:25 +00:00
static final _diffLimit = 200;
2020-03-24 19:59:36 +00:00
2020-10-03 17:58:26 +00:00
SyncService._privateConstructor() {
2020-04-30 15:09:41 +00:00
Bus.instance.on<UserAuthenticatedEvent>().listen((event) {
sync();
});
2020-11-16 16:35:16 +00:00
Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
_logger.info("Connectivity change detected " + result.toString());
sync();
});
Bus.instance.on<SyncStatusUpdate>().listen((event) {
_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");
}
}
2020-04-30 15:09:41 +00:00
Future<void> sync() async {
_syncStopRequested = false;
2020-04-30 15:09:41 +00:00
if (_isSyncInProgress) {
2020-05-02 16:28:54 +00:00
_logger.warning("Sync already in progress, skipping.");
return _existingSync;
2020-04-27 13:02:29 +00:00
}
2020-04-30 15:09:41 +00:00
_isSyncInProgress = true;
_existingSync = Future<void>(() async {
_logger.info("Syncing...");
try {
await _doSync();
if (_lastSyncStatusEvent != null) {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.completed));
}
2020-11-16 16:35:16 +00:00
} on WiFiUnavailableError {
_logger.warning("Not uploading over mobile data");
Bus.instance.fire(
SyncStatusUpdate(SyncStatus.paused, reason: "Waiting for WiFi..."));
2020-10-28 15:25:32 +00:00
} catch (e, s) {
_logger.severe(e, s);
2020-11-16 16:35:16 +00:00
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));
} finally {
_isSyncInProgress = false;
}
});
return _existingSync;
}
2020-04-30 15:09:41 +00:00
void stopSync() {
_logger.info("Sync stop requested");
_syncStopRequested = true;
}
bool shouldStopSync() {
return _syncStopRequested;
}
bool hasScannedDisk() {
2020-08-11 23:04:16 +00:00
return _prefs.containsKey(_dbUpdationTimeKey);
2020-06-15 19:57:48 +00:00
}
2020-11-12 16:32:10 +00:00
bool isSyncInProgress() {
return _isSyncInProgress;
}
2020-11-16 16:35:16 +00:00
SyncStatusUpdate getLastSyncStatusEvent() {
return _lastSyncStatusEvent;
}
Future<void> _doSync() async {
2020-12-07 23:03:25 +00:00
final existingLocalFileIDs = await _db.getExistingLocalFileIDs();
final syncStartTime = DateTime.now().microsecondsSinceEpoch;
2020-07-27 21:07:56 +00:00
final result = await PhotoManager.requestPermission();
if (!result) {
_logger.severe("Did not get permission");
2020-12-07 23:03:25 +00:00
await _prefs.setInt(_dbUpdationTimeKey, syncStartTime);
await FileRepository.instance.reloadFiles();
return await syncWithRemote();
2020-07-27 21:07:56 +00:00
}
final lastDBUpdationTime = _prefs.getInt(_dbUpdationTimeKey);
if (lastDBUpdationTime != null && lastDBUpdationTime != 0) {
await _loadAndStorePhotos(
2020-12-01 08:56:37 +00:00
lastDBUpdationTime, syncStartTime, existingLocalFileIDs);
} else {
// Load from 0 - 01.01.2010
var startTime = 0;
var toYear = 2010;
var toTime = DateTime(toYear).microsecondsSinceEpoch;
while (toTime < syncStartTime) {
await _loadAndStorePhotos(startTime, toTime, existingLocalFileIDs);
startTime = toTime;
toYear++;
toTime = DateTime(toYear).microsecondsSinceEpoch;
2020-03-30 14:28:46 +00:00
}
await _loadAndStorePhotos(startTime, syncStartTime, existingLocalFileIDs);
2020-10-30 21:07:20 +00:00
}
2020-12-07 23:03:25 +00:00
await FileRepository.instance.reloadFiles();
2020-10-30 20:37:21 +00:00
await syncWithRemote();
2020-04-24 12:40:24 +00:00
}
Future<void> _loadAndStorePhotos(
int fromTime, int toTime, Set<String> existingLocalFileIDs) async {
_logger.info("Loading photos from " +
getMonthAndYear(DateTime.fromMicrosecondsSinceEpoch(fromTime)) +
" to " +
getMonthAndYear(DateTime.fromMicrosecondsSinceEpoch(toTime)));
final files = await getDeviceFiles(fromTime, toTime);
if (files.isNotEmpty) {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.applying_local_diff));
_logger.info("Fetched " + files.length.toString() + " files.");
final updatedFiles =
files.where((file) => existingLocalFileIDs.contains(file.localID));
_logger.info(updatedFiles.length.toString() + " files were updated.");
for (final file in updatedFiles) {
await _db.updateUploadedFile(
file.localID,
file.title,
file.location,
file.creationTime,
file.modificationTime,
null,
);
}
files.removeWhere((file) => existingLocalFileIDs.contains(file.localID));
await _db.insertMultiple(files);
_logger.info("Inserted " + files.length.toString() + " files.");
await FileRepository.instance.reloadFiles();
}
await _prefs.setInt(_dbUpdationTimeKey, toTime);
}
2020-12-03 22:30:10 +00:00
Future<void> syncWithRemote({bool silently = false}) async {
if (!Configuration.instance.hasConfiguredAccount()) {
return Future.error("Account not configured yet");
}
final updatedCollections = await _collectionsService.sync();
2020-12-03 22:30:10 +00:00
if (updatedCollections.isNotEmpty && !silently) {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.applying_remote_diff));
}
for (final collection in updatedCollections) {
2020-10-28 15:45:05 +00:00
await _fetchEncryptedFilesDiff(collection.id);
}
await deleteFilesOnServer();
2020-12-03 22:19:25 +00:00
bool hasUploadedFiles = await _uploadDiff();
if (hasUploadedFiles) {
2020-12-03 22:30:10 +00:00
syncWithRemote(silently: true);
2020-12-03 22:19:25 +00:00
}
}
2020-10-28 15:45:05 +00:00
Future<void> _fetchEncryptedFilesDiff(int collectionID) async {
final diff = await _downloader.getEncryptedFilesDiff(
2020-10-28 15:45:05 +00:00
collectionID,
_collectionsService.getCollectionSyncTime(collectionID),
2020-10-28 15:45:05 +00:00
_diffLimit,
);
if (diff.updatedFiles.isNotEmpty) {
await _storeDiff(diff.updatedFiles, collectionID);
_logger.info("Updated " +
diff.updatedFiles.length.toString() +
" files in collection " +
collectionID.toString());
2020-08-11 23:04:16 +00:00
FileRepository.instance.reloadFiles();
Bus.instance.fire(CollectionUpdatedEvent(collectionID: collectionID));
if (diff.fetchCount == _diffLimit) {
2020-10-28 15:45:05 +00:00
return await _fetchEncryptedFilesDiff(collectionID);
2020-08-11 23:04:16 +00:00
}
}
}
2020-12-03 22:19:25 +00:00
Future<bool> _uploadDiff() async {
final foldersToBackUp = Configuration.instance.getPathsToBackUp();
final filesToBeUploaded =
2020-09-17 18:48:25 +00:00
await _db.getFilesToBeUploadedWithinFolders(foldersToBackUp);
2020-11-12 16:32:10 +00:00
if (kDebugMode) {
filesToBeUploaded
.removeWhere((element) => element.fileType == FileType.video);
2020-11-12 16:32:10 +00:00
}
2021-01-02 08:02:29 +00:00
_logger.info(
filesToBeUploaded.length.toString() + " new files to be uploaded.");
final updatedFileIDs = await _db.getUploadedFileIDsToBeUpdated();
2021-01-02 08:02:29 +00:00
_logger.info(updatedFileIDs.length.toString() + " files updated.");
int uploadCounter = 0;
final totalUploads = filesToBeUploaded.length + updatedFileIDs.length;
if (totalUploads > 0) {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.preparing_for_upload));
}
2021-01-02 08:02:29 +00:00
final futures = List<Future>();
for (final uploadedFileID in updatedFileIDs) {
if (_syncStopRequested) {
_syncStopRequested = false;
Bus.instance
.fire(SyncStatusUpdate(SyncStatus.completed, wasStopped: true));
2020-12-03 22:19:25 +00:00
return false;
}
final file = await _db.getUploadedFileInAnyCollection(uploadedFileID);
final future = _uploader.upload(file, file.collectionID).then((value) {
uploadCounter++;
Bus.instance
.fire(CollectionUpdatedEvent(collectionID: file.collectionID));
Bus.instance.fire(SyncStatusUpdate(SyncStatus.in_progress,
completed: uploadCounter, total: totalUploads));
});
futures.add(future);
}
for (final file in filesToBeUploaded) {
if (_syncStopRequested) {
_syncStopRequested = false;
2020-11-15 07:19:51 +00:00
Bus.instance
.fire(SyncStatusUpdate(SyncStatus.completed, wasStopped: true));
2020-12-03 22:19:25 +00:00
return false;
}
2020-11-16 16:35:16 +00:00
final collectionID = (await CollectionsService.instance
.getOrCreateForPath(file.deviceFolder))
.id;
final future = _uploader.upload(file, collectionID).then((value) {
uploadCounter++;
2020-11-16 16:35:16 +00:00
Bus.instance
.fire(CollectionUpdatedEvent(collectionID: file.collectionID));
Bus.instance.fire(SyncStatusUpdate(SyncStatus.in_progress,
completed: uploadCounter, total: totalUploads));
2020-11-16 16:35:16 +00:00
});
futures.add(future);
2020-03-26 14:39:31 +00:00
}
try {
await Future.wait(futures);
2020-11-16 16:35:16 +00:00
} on InvalidFileError {
// Do nothing
} on WiFiUnavailableError {
throw WiFiUnavailableError();
} catch (e, s) {
2020-11-12 16:32:10 +00:00
_isSyncInProgress = false;
2020-11-15 07:19:51 +00:00
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));
_logger.severe("Error in syncing files", e, s);
}
2020-12-03 22:19:25 +00:00
return uploadCounter > 0;
2020-03-26 14:39:31 +00:00
}
2020-10-28 15:45:05 +00:00
Future _storeDiff(List<File> diff, int collectionID) async {
2020-06-19 23:03:26 +00:00
for (File file in diff) {
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 {
// File exists on device
file.localID = existingFiles[0]
.localID; // File should ideally have the same localID
bool wasUploadedOnAPreviousInstallation =
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;
for (final existingFile in existingFiles) {
if (file.collectionID == existingFile.collectionID &&
file.uploadedFileID == existingFile.uploadedFileID) {
foundMatchingCollection = true;
file.generatedID = existingFile.generatedID;
await _db.update(file);
if (file.fileType == FileType.video) {
VideoCacheManager().removeFile(file.getDownloadUrl());
} else {
DefaultCacheManager().removeFile(file.getDownloadUrl());
}
ThumbnailCacheManager().removeFile(file.getDownloadUrl());
break;
}
}
if (!foundMatchingCollection) {
// Added to a new collection
await _db.insert(file);
}
}
}
await _collectionsService.setCollectionSyncTime(
collectionID, file.updationTime);
2020-03-28 13:56:06 +00:00
}
}
2020-03-26 14:39:31 +00:00
Future<void> deleteFilesOnServer() async {
2020-10-30 23:25:28 +00:00
return _db.getDeletedFileIDs().then((ids) async {
for (int id in ids) {
await _deleteFileOnServer(id);
await _db.delete(id);
2020-04-12 12:38:49 +00:00
}
});
}
2020-10-30 23:25:28 +00:00
Future<void> _deleteFileOnServer(int fileID) async {
2020-05-17 10:18:09 +00:00
return _dio
.delete(
Configuration.instance.getHttpEndpoint() +
"/files/" +
2020-10-30 23:25:28 +00:00
fileID.toString(),
2020-05-17 10:18:09 +00:00
options: Options(
headers: {"X-Auth-Token": Configuration.instance.getToken()}),
)
.catchError((e) => _logger.severe(e));
2020-03-29 14:04:26 +00:00
}
2020-03-24 19:59:36 +00:00
}