|
@@ -6,7 +6,6 @@ import 'dart:io';
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
import 'package:logging/logging.dart';
|
|
|
-import 'package:photo_manager/photo_manager.dart';
|
|
|
import 'package:photos/db/file_updation_db.dart';
|
|
|
import 'package:photos/db/files_db.dart';
|
|
|
import 'package:photos/models/file.dart' as ente;
|
|
@@ -16,7 +15,6 @@ import 'package:shared_preferences/shared_preferences.dart';
|
|
|
// LocalFileUpdateService tracks all the potential local file IDs which have
|
|
|
// changed/modified on the device and needed to be uploaded again.
|
|
|
class LocalFileUpdateService {
|
|
|
- FilesDB _filesDB;
|
|
|
FileUpdationDB _fileUpdationDB;
|
|
|
SharedPreferences _prefs;
|
|
|
Logger _logger;
|
|
@@ -26,7 +24,6 @@ class LocalFileUpdateService {
|
|
|
|
|
|
LocalFileUpdateService._privateConstructor() {
|
|
|
_logger = Logger((LocalFileUpdateService).toString());
|
|
|
- _filesDB = FilesDB.instance;
|
|
|
_fileUpdationDB = FileUpdationDB.instance;
|
|
|
}
|
|
|
|
|
@@ -37,11 +34,6 @@ class LocalFileUpdateService {
|
|
|
static LocalFileUpdateService instance =
|
|
|
LocalFileUpdateService._privateConstructor();
|
|
|
|
|
|
- Future<bool> _markLocationMigrationAsCompleted() async {
|
|
|
- _logger.info('marking migration as completed');
|
|
|
- return _prefs.setBool(isLocationMigrationComplete, true);
|
|
|
- }
|
|
|
-
|
|
|
bool isLocationMigrationCompleted() {
|
|
|
return _prefs.get(isLocationMigrationComplete) ?? false;
|
|
|
}
|
|
@@ -53,10 +45,6 @@ class LocalFileUpdateService {
|
|
|
}
|
|
|
_existingMigration = Completer<void>();
|
|
|
try {
|
|
|
- if (!isLocationMigrationCompleted() && Platform.isAndroid) {
|
|
|
- _logger.info("start migration for missing location");
|
|
|
- await _runMigrationForFilesWithMissingLocation();
|
|
|
- }
|
|
|
await _markFilesWhichAreActuallyUpdated();
|
|
|
} catch (e, s) {
|
|
|
_logger.severe('failed to perform migration', e, s);
|
|
@@ -72,27 +60,26 @@ class LocalFileUpdateService {
|
|
|
// then it marks the file for file update.
|
|
|
Future<void> _markFilesWhichAreActuallyUpdated() async {
|
|
|
final sTime = DateTime.now().microsecondsSinceEpoch;
|
|
|
- bool hasData = true;
|
|
|
- const int limitInBatch = 100;
|
|
|
- while (hasData) {
|
|
|
- final localIDsToProcess =
|
|
|
- await _fileUpdationDB.getLocalIDsForPotentialReUpload(
|
|
|
- limitInBatch,
|
|
|
- FileUpdationDB.modificationTimeUpdated,
|
|
|
+ // singleRunLimit indicates number of files to check during single
|
|
|
+ // invocation of this method. The limit act as a crude way to limit the
|
|
|
+ // resource consumed by the method
|
|
|
+ const int singleRunLimit = 10;
|
|
|
+ final localIDsToProcess =
|
|
|
+ await _fileUpdationDB.getLocalIDsForPotentialReUpload(
|
|
|
+ singleRunLimit,
|
|
|
+ FileUpdationDB.modificationTimeUpdated,
|
|
|
+ );
|
|
|
+ if (localIDsToProcess.isNotEmpty) {
|
|
|
+ await _checkAndMarkFilesWithDifferentHashForFileUpdate(
|
|
|
+ localIDsToProcess,
|
|
|
+ );
|
|
|
+ final eTime = DateTime.now().microsecondsSinceEpoch;
|
|
|
+ final d = Duration(microseconds: eTime - sTime);
|
|
|
+ _logger.info(
|
|
|
+ 'Performed hashCheck for ${localIDsToProcess.length} updated files '
|
|
|
+ 'completed in ${d.inSeconds.toString()} secs',
|
|
|
);
|
|
|
- if (localIDsToProcess.isEmpty) {
|
|
|
- hasData = false;
|
|
|
- } else {
|
|
|
- await _checkAndMarkFilesWithDifferentHashForFileUpdate(
|
|
|
- localIDsToProcess,
|
|
|
- );
|
|
|
- }
|
|
|
}
|
|
|
- final eTime = DateTime.now().microsecondsSinceEpoch;
|
|
|
- final d = Duration(microseconds: eTime - sTime);
|
|
|
- _logger.info(
|
|
|
- '_markFilesWhichAreActuallyUpdated migration completed in ${d.inSeconds.toString()} seconds',
|
|
|
- );
|
|
|
}
|
|
|
|
|
|
Future<void> _checkAndMarkFilesWithDifferentHashForFileUpdate(
|
|
@@ -152,91 +139,4 @@ class LocalFileUpdateService {
|
|
|
}
|
|
|
return mediaUploadData;
|
|
|
}
|
|
|
-
|
|
|
- Future<void> _runMigrationForFilesWithMissingLocation() async {
|
|
|
- if (!Platform.isAndroid) {
|
|
|
- return;
|
|
|
- }
|
|
|
- // migration only needs to run if Android API Level is 29 or higher
|
|
|
- final int version = int.parse(await PhotoManager.systemVersion());
|
|
|
- final bool isMigrationRequired = version >= 29;
|
|
|
- if (isMigrationRequired) {
|
|
|
- await _importLocalFilesForMigration();
|
|
|
- final sTime = DateTime.now().microsecondsSinceEpoch;
|
|
|
- bool hasData = true;
|
|
|
- const int limitInBatch = 100;
|
|
|
- while (hasData) {
|
|
|
- final localIDsToProcess =
|
|
|
- await _fileUpdationDB.getLocalIDsForPotentialReUpload(
|
|
|
- limitInBatch,
|
|
|
- FileUpdationDB.missingLocation,
|
|
|
- );
|
|
|
- if (localIDsToProcess.isEmpty) {
|
|
|
- hasData = false;
|
|
|
- } else {
|
|
|
- await _checkAndMarkFilesWithLocationForReUpload(localIDsToProcess);
|
|
|
- }
|
|
|
- }
|
|
|
- final eTime = DateTime.now().microsecondsSinceEpoch;
|
|
|
- final d = Duration(microseconds: eTime - sTime);
|
|
|
- _logger.info(
|
|
|
- 'filesWithMissingLocation migration completed in ${d.inSeconds.toString()} seconds',
|
|
|
- );
|
|
|
- }
|
|
|
- await _markLocationMigrationAsCompleted();
|
|
|
- }
|
|
|
-
|
|
|
- Future<void> _checkAndMarkFilesWithLocationForReUpload(
|
|
|
- List<String> localIDsToProcess,
|
|
|
- ) async {
|
|
|
- _logger.info("files to process ${localIDsToProcess.length}");
|
|
|
- final localIDsWithLocation = <String>[];
|
|
|
- for (var localID in localIDsToProcess) {
|
|
|
- bool hasLocation = false;
|
|
|
- try {
|
|
|
- final assetEntity = await AssetEntity.fromId(localID);
|
|
|
- if (assetEntity == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- final latLng = await assetEntity.latlngAsync();
|
|
|
- if ((latLng.longitude ?? 0.0) != 0.0 ||
|
|
|
- (latLng.longitude ?? 0.0) != 0.0) {
|
|
|
- _logger.finest(
|
|
|
- 'found lat/long ${latLng.longitude}/${latLng.longitude} for ${assetEntity.title} ${assetEntity.relativePath} with id : $localID',
|
|
|
- );
|
|
|
- hasLocation = true;
|
|
|
- }
|
|
|
- } catch (e, s) {
|
|
|
- _logger.severe('failed to get asset entity with id $localID', e, s);
|
|
|
- }
|
|
|
- if (hasLocation) {
|
|
|
- localIDsWithLocation.add(localID);
|
|
|
- }
|
|
|
- }
|
|
|
- _logger.info('marking ${localIDsWithLocation.length} files for re-upload');
|
|
|
- await _filesDB.markForReUploadIfLocationMissing(localIDsWithLocation);
|
|
|
- await _fileUpdationDB.deleteByLocalIDs(
|
|
|
- localIDsToProcess,
|
|
|
- FileUpdationDB.missingLocation,
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- Future<void> _importLocalFilesForMigration() async {
|
|
|
- if (_prefs.containsKey(isLocalImportDone)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- final sTime = DateTime.now().microsecondsSinceEpoch;
|
|
|
- _logger.info('importing files without location info');
|
|
|
- final fileLocalIDs = await _filesDB.getLocalFilesBackedUpWithoutLocation();
|
|
|
- await _fileUpdationDB.insertMultiple(
|
|
|
- fileLocalIDs,
|
|
|
- FileUpdationDB.missingLocation,
|
|
|
- );
|
|
|
- final eTime = DateTime.now().microsecondsSinceEpoch;
|
|
|
- final d = Duration(microseconds: eTime - sTime);
|
|
|
- _logger.info(
|
|
|
- 'importing completed, total files count ${fileLocalIDs.length} and took ${d.inSeconds.toString()} seconds',
|
|
|
- );
|
|
|
- await _prefs.setBool(isLocalImportDone, true);
|
|
|
- }
|
|
|
}
|