ente/lib/services/file_migration_service.dart

142 lines
5 KiB
Dart
Raw Normal View History

2022-06-08 08:49:23 +00:00
import 'dart:async';
import 'dart:core';
import 'dart:io';
import 'package:logging/logging.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/db/file_migration_db.dart';
import 'package:photos/db/files_db.dart';
import 'package:shared_preferences/shared_preferences.dart';
2022-07-29 06:06:03 +00:00
// LocalFileUpdateService tracks all the potential local file IDs which have
// changed/modified on the device and needed to be uploaded again.
2022-06-08 08:49:23 +00:00
class FileMigrationService {
FilesDB _filesDB;
FilesMigrationDB _filesMigrationDB;
2022-06-08 09:30:21 +00:00
SharedPreferences _prefs;
2022-06-08 08:49:23 +00:00
Logger _logger;
static const isLocationMigrationComplete = "fm_isLocationMigrationComplete";
static const isLocalImportDone = "fm_IsLocalImportDone";
Completer<void> _existingMigration;
FileMigrationService._privateConstructor() {
_logger = Logger((FileMigrationService).toString());
_filesDB = FilesDB.instance;
_filesMigrationDB = FilesMigrationDB.instance;
2022-06-08 09:30:21 +00:00
}
Future<void> init() async {
_prefs = await SharedPreferences.getInstance();
2022-06-08 08:49:23 +00:00
}
static FileMigrationService instance =
FileMigrationService._privateConstructor();
Future<bool> _markLocationMigrationAsCompleted() async {
_logger.info('marking migration as completed');
2022-06-08 09:30:21 +00:00
return _prefs.setBool(isLocationMigrationComplete, true);
2022-06-08 08:49:23 +00:00
}
2022-06-08 09:30:21 +00:00
bool isLocationMigrationCompleted() {
return _prefs.get(isLocationMigrationComplete) ?? false;
2022-06-08 08:49:23 +00:00
}
2022-06-08 09:20:03 +00:00
Future<void> runMigration() async {
2022-06-08 08:49:23 +00:00
if (_existingMigration != null) {
_logger.info("migration is already in progress, skipping");
2022-06-08 08:49:23 +00:00
return _existingMigration.future;
}
_existingMigration = Completer<void>();
try {
2022-07-29 06:06:03 +00:00
if (!isLocationMigrationCompleted() && Platform.isAndroid) {
_logger.info("start migration for missing location");
await _runMigrationForFilesWithMissingLocation();
}
2022-06-08 08:49:23 +00:00
_existingMigration.complete();
_existingMigration = null;
} catch (e, s) {
_logger.severe('failed to perform migration', e, s);
_existingMigration.complete();
_existingMigration = null;
}
}
2022-06-08 09:20:03 +00:00
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());
bool isMigrationRequired = version >= 29;
if (isMigrationRequired) {
await _importLocalFilesForMigration();
final sTime = DateTime.now().microsecondsSinceEpoch;
2022-06-08 14:17:19 +00:00
bool hasData = true;
2022-07-04 06:02:17 +00:00
const int limitInBatch = 100;
while (hasData) {
2022-06-08 14:17:19 +00:00
var localIDsToProcess = await _filesMigrationDB
.getLocalIDsForPotentialReUpload(limitInBatch);
if (localIDsToProcess.isEmpty) {
hasData = false;
} else {
await _checkAndMarkFilesForReUpload(localIDsToProcess);
}
2022-06-08 09:20:03 +00:00
}
final eTime = DateTime.now().microsecondsSinceEpoch;
final d = Duration(microseconds: eTime - sTime);
_logger.info(
2022-06-11 08:23:52 +00:00
'filesWithMissingLocation migration completed in ${d.inSeconds.toString()} seconds',
);
2022-06-08 09:20:03 +00:00
}
await _markLocationMigrationAsCompleted();
}
2022-06-08 08:49:23 +00:00
Future<void> _checkAndMarkFilesForReUpload(
2022-06-11 08:23:52 +00:00
List<String> localIDsToProcess,
) async {
_logger.info("files to process ${localIDsToProcess.length}");
2022-06-08 08:49:23 +00:00
var localIDsWithLocation = <String>[];
for (var localID in localIDsToProcess) {
bool hasLocation = false;
try {
var assetEntity = await AssetEntity.fromId(localID);
if (assetEntity == null) {
continue;
}
var latLng = await assetEntity.latlngAsync();
2022-06-08 09:20:03 +00:00
if ((latLng.longitude ?? 0.0) != 0.0 ||
(latLng.longitude ?? 0.0) != 0.0) {
2022-06-08 08:49:23 +00:00
_logger.finest(
2022-06-11 08:23:52 +00:00
'found lat/long ${latLng.longitude}/${latLng.longitude} for ${assetEntity.title} ${assetEntity.relativePath} with id : $localID',
);
2022-06-08 08:49:23 +00:00
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');
2022-06-08 10:29:29 +00:00
await _filesDB.markForReUploadIfLocationMissing(localIDsWithLocation);
2022-06-08 09:30:21 +00:00
await _filesMigrationDB.deleteByLocalIDs(localIDsToProcess);
2022-06-08 08:49:23 +00:00
}
Future<void> _importLocalFilesForMigration() async {
2022-06-08 09:30:21 +00:00
if (_prefs.containsKey(isLocalImportDone)) {
2022-06-08 08:49:23 +00:00
return;
}
2022-06-08 09:20:03 +00:00
final sTime = DateTime.now().microsecondsSinceEpoch;
2022-06-08 08:49:23 +00:00
_logger.info('importing files without location info');
var fileLocalIDs = await _filesDB.getLocalFilesBackedUpWithoutLocation();
await _filesMigrationDB.insertMultiple(fileLocalIDs);
2022-06-08 09:20:03 +00:00
final eTime = DateTime.now().microsecondsSinceEpoch;
final d = Duration(microseconds: eTime - sTime);
_logger.info(
2022-06-11 08:23:52 +00:00
'importing completed, total files count ${fileLocalIDs.length} and took ${d.inSeconds.toString()} seconds',
);
2022-06-08 09:30:21 +00:00
_prefs.setBool(isLocalImportDone, true);
2022-06-08 08:49:23 +00:00
}
}