浏览代码

Process older photos

Vishnu 4 年之前
父节点
当前提交
d3ee26db0c
共有 3 个文件被更改,包括 94 次插入13 次删除
  1. 27 9
      lib/services/local_sync_service.dart
  2. 4 0
      lib/services/sync_service.dart
  3. 63 4
      lib/utils/file_sync_util.dart

+ 27 - 9
lib/services/local_sync_service.dart

@@ -101,16 +101,34 @@ class LocalSyncService {
     final endTime = DateTime.now().microsecondsSinceEpoch;
     final endTime = DateTime.now().microsecondsSinceEpoch;
     final duration = Duration(microseconds: endTime - startTime);
     final duration = Duration(microseconds: endTime - startTime);
     _logger.info("Load took " + duration.inMilliseconds.toString() + "ms");
     _logger.info("Load took " + duration.inMilliseconds.toString() + "ms");
+  }
 
 
-    // final sTime = DateTime.now().microsecondsSinceEpoch;
-    // final ids = await getAllLocalIDs();
-    // final eTime = DateTime.now().microsecondsSinceEpoch;
-    // final d = Duration(microseconds: eTime - sTime);
-    // _logger.info("Loading " +
-    //     ids.length.toString() +
-    //     " took " +
-    //     d.inMilliseconds.toString() +
-    //     "ms");
+  Future<bool> syncAll() async {
+    final sTime = DateTime.now().microsecondsSinceEpoch;
+    final localAssets = await getAllLocalAssets();
+    final eTime = DateTime.now().microsecondsSinceEpoch;
+    final d = Duration(microseconds: eTime - sTime);
+    _logger.info("Loading from the beginning returned " +
+        localAssets.length.toString() +
+        " assets and took " +
+        d.inMilliseconds.toString() +
+        "ms");
+    final existingIDs = await _db.getExistingLocalFileIDs();
+    final List<LocalAsset> unsyncedAssets = [];
+    for (final asset in localAssets) {
+      if (!existingIDs.contains(asset.id)) {
+        unsyncedAssets.add(asset);
+      }
+    }
+    if (unsyncedAssets.isEmpty) {
+      return false;
+    }
+    final unsyncedFiles = await convertToFiles(unsyncedAssets, _computer);
+    await _db.insertMultiple(unsyncedFiles);
+    _logger.info(
+        "Inserted " + unsyncedFiles.length.toString() + " unsynced files.");
+    Bus.instance.fire(LocalPhotosUpdatedEvent(unsyncedFiles));
+    return true;
   }
   }
 
 
   Future<void> trackEditedFile(File file) async {
   Future<void> trackEditedFile(File file) async {

+ 4 - 0
lib/services/sync_service.dart

@@ -180,6 +180,10 @@ class SyncService {
     await _localSyncService.sync();
     await _localSyncService.sync();
     if (_localSyncService.hasCompletedFirstImport()) {
     if (_localSyncService.hasCompletedFirstImport()) {
       await _remoteSyncService.sync();
       await _remoteSyncService.sync();
+      final shouldSync = await _localSyncService.syncAll();
+      if (shouldSync) {
+        await _remoteSyncService.sync();
+      }
     }
     }
   }
   }
 
 

+ 63 - 4
lib/utils/file_sync_util.dart

@@ -1,5 +1,7 @@
+import 'dart:convert';
 import 'dart:math';
 import 'dart:math';
 
 
+import 'package:collection/collection.dart';
 import 'package:computer/computer.dart';
 import 'package:computer/computer.dart';
 import 'package:logging/logging.dart';
 import 'package:logging/logging.dart';
 import 'package:photo_manager/photo_manager.dart';
 import 'package:photo_manager/photo_manager.dart';
@@ -27,20 +29,49 @@ Future<List<File>> getDeviceFiles(
   return files;
   return files;
 }
 }
 
 
-Future<Set<String>> getAllLocalIDs() async {
+Future<List<LocalAsset>> getAllLocalAssets() async {
   final filterOptionGroup = FilterOptionGroup();
   final filterOptionGroup = FilterOptionGroup();
   final assetPaths = await PhotoManager.getAssetPathList(
   final assetPaths = await PhotoManager.getAssetPathList(
     hasAll: true,
     hasAll: true,
     type: RequestType.common,
     type: RequestType.common,
     filterOption: filterOptionGroup,
     filterOption: filterOptionGroup,
   );
   );
-  final ids = new Set<String>();
+  final List<LocalAsset> assets = [];
   for (final assetPath in assetPaths) {
   for (final assetPath in assetPaths) {
     for (final asset in await assetPath.assetList) {
     for (final asset in await assetPath.assetList) {
-      ids.add(asset.id);
+      assets.add(LocalAsset(asset.id, assetPath.name));
     }
     }
   }
   }
-  return ids;
+  return assets;
+}
+
+Future<List<File>> convertToFiles(
+    List<LocalAsset> assets, Computer computer) async {
+  final List<LocalAsset> recents = [];
+  final List<LocalAssetEntity> entities = [];
+  for (final asset in assets) {
+    if (asset.path == "Recent" || asset.path == "Recents") {
+      recents.add(asset);
+    } else {
+      entities.add(
+          LocalAssetEntity(await AssetEntity.fromId(asset.id), asset.path));
+    }
+  }
+  // Ignore duplicate items in recents
+  for (final recent in recents) {
+    bool presentInOthers = false;
+    for (final entity in entities) {
+      if (recent.id == entity.entity.id) {
+        presentInOthers = true;
+        break;
+      }
+    }
+    if (!presentInOthers) {
+      entities.add(
+          LocalAssetEntity(await AssetEntity.fromId(recent.id), recent.path));
+    }
+  }
+  return await computer.compute(_getFilesFromAssets, param: entities);
 }
 }
 
 
 Future<List<AssetPathEntity>> _getGalleryList(
 Future<List<AssetPathEntity>> _getGalleryList(
@@ -100,3 +131,31 @@ Future<List<File>> _getFiles(Map<String, dynamic> args) async {
   }
   }
   return files;
   return files;
 }
 }
+
+Future<List<File>> _getFilesFromAssets(List<LocalAssetEntity> assets) async {
+  final List<File> files = [];
+  for (final asset in assets) {
+    files.add(await File.fromAsset(
+      asset.path,
+      asset.entity,
+    ));
+  }
+  return files;
+}
+
+class LocalAsset {
+  final String id;
+  final String path;
+
+  LocalAsset(
+    this.id,
+    this.path,
+  );
+}
+
+class LocalAssetEntity {
+  final AssetEntity entity;
+  final String path;
+
+  LocalAssetEntity(this.entity, this.path);
+}