file_migration_service.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import 'dart:async';
  2. import 'dart:core';
  3. import 'dart:io';
  4. import 'package:logging/logging.dart';
  5. import 'package:photo_manager/photo_manager.dart';
  6. import 'package:photos/db/file_migration_db.dart';
  7. import 'package:photos/db/files_db.dart';
  8. import 'package:shared_preferences/shared_preferences.dart';
  9. class FileMigrationService {
  10. FilesDB _filesDB;
  11. FilesMigrationDB _filesMigrationDB;
  12. SharedPreferences _prefs;
  13. Logger _logger;
  14. static const isLocationMigrationComplete = "fm_isLocationMigrationComplete";
  15. static const isLocalImportDone = "fm_IsLocalImportDone";
  16. Completer<void> _existingMigration;
  17. FileMigrationService._privateConstructor() {
  18. _logger = Logger((FileMigrationService).toString());
  19. _filesDB = FilesDB.instance;
  20. _filesMigrationDB = FilesMigrationDB.instance;
  21. }
  22. Future<void> init() async {
  23. _prefs = await SharedPreferences.getInstance();
  24. }
  25. static FileMigrationService instance =
  26. FileMigrationService._privateConstructor();
  27. Future<bool> _markLocationMigrationAsCompleted() async {
  28. _logger.info('marking migration as completed');
  29. return _prefs.setBool(isLocationMigrationComplete, true);
  30. }
  31. bool isLocationMigrationCompleted() {
  32. return _prefs.get(isLocationMigrationComplete) ?? false;
  33. }
  34. Future<void> runMigration() async {
  35. if (_existingMigration != null) {
  36. _logger.info("migration is already in progress, skipping");
  37. return _existingMigration.future;
  38. }
  39. _logger.info("start migration");
  40. _existingMigration = Completer<void>();
  41. try {
  42. await _runMigrationForFilesWithMissingLocation();
  43. _existingMigration.complete();
  44. _existingMigration = null;
  45. } catch (e, s) {
  46. _logger.severe('failed to perform migration', e, s);
  47. _existingMigration.complete();
  48. _existingMigration = null;
  49. }
  50. }
  51. Future<void> _runMigrationForFilesWithMissingLocation() async {
  52. if (!Platform.isAndroid) {
  53. return;
  54. }
  55. // migration only needs to run if Android API Level is 29 or higher
  56. final int version = int.parse(await PhotoManager.systemVersion());
  57. final bool isMigrationRequired = version >= 29;
  58. if (isMigrationRequired) {
  59. await _importLocalFilesForMigration();
  60. final sTime = DateTime.now().microsecondsSinceEpoch;
  61. bool hasData = true;
  62. const int limitInBatch = 100;
  63. while (hasData) {
  64. final localIDsToProcess = await _filesMigrationDB
  65. .getLocalIDsForPotentialReUpload(limitInBatch);
  66. if (localIDsToProcess.isEmpty) {
  67. hasData = false;
  68. } else {
  69. await _checkAndMarkFilesForReUpload(localIDsToProcess);
  70. }
  71. }
  72. final eTime = DateTime.now().microsecondsSinceEpoch;
  73. final d = Duration(microseconds: eTime - sTime);
  74. _logger.info(
  75. 'filesWithMissingLocation migration completed in ${d.inSeconds.toString()} seconds',
  76. );
  77. }
  78. await _markLocationMigrationAsCompleted();
  79. }
  80. Future<void> _checkAndMarkFilesForReUpload(
  81. List<String> localIDsToProcess,
  82. ) async {
  83. _logger.info("files to process ${localIDsToProcess.length}");
  84. final localIDsWithLocation = <String>[];
  85. for (var localID in localIDsToProcess) {
  86. bool hasLocation = false;
  87. try {
  88. final assetEntity = await AssetEntity.fromId(localID);
  89. if (assetEntity == null) {
  90. continue;
  91. }
  92. final latLng = await assetEntity.latlngAsync();
  93. if ((latLng.longitude ?? 0.0) != 0.0 ||
  94. (latLng.longitude ?? 0.0) != 0.0) {
  95. _logger.finest(
  96. 'found lat/long ${latLng.longitude}/${latLng.longitude} for ${assetEntity.title} ${assetEntity.relativePath} with id : $localID',
  97. );
  98. hasLocation = true;
  99. }
  100. } catch (e, s) {
  101. _logger.severe('failed to get asset entity with id $localID', e, s);
  102. }
  103. if (hasLocation) {
  104. localIDsWithLocation.add(localID);
  105. }
  106. }
  107. _logger.info('marking ${localIDsWithLocation.length} files for re-upload');
  108. await _filesDB.markForReUploadIfLocationMissing(localIDsWithLocation);
  109. await _filesMigrationDB.deleteByLocalIDs(localIDsToProcess);
  110. }
  111. Future<void> _importLocalFilesForMigration() async {
  112. if (_prefs.containsKey(isLocalImportDone)) {
  113. return;
  114. }
  115. final sTime = DateTime.now().microsecondsSinceEpoch;
  116. _logger.info('importing files without location info');
  117. final fileLocalIDs = await _filesDB.getLocalFilesBackedUpWithoutLocation();
  118. await _filesMigrationDB.insertMultiple(fileLocalIDs);
  119. final eTime = DateTime.now().microsecondsSinceEpoch;
  120. final d = Duration(microseconds: eTime - sTime);
  121. _logger.info(
  122. 'importing completed, total files count ${fileLocalIDs.length} and took ${d.inSeconds.toString()} seconds',
  123. );
  124. _prefs.setBool(isLocalImportDone, true);
  125. }
  126. }