Process older photos

This commit is contained in:
Vishnu 2021-06-15 17:27:33 +05:30
parent 3f6a847212
commit d3ee26db0c
3 changed files with 94 additions and 13 deletions

View file

@ -101,16 +101,34 @@ class LocalSyncService {
final endTime = DateTime.now().microsecondsSinceEpoch;
final duration = Duration(microseconds: endTime - startTime);
_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 {

View file

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

View file

@ -1,5 +1,7 @@
import 'dart:convert';
import 'dart:math';
import 'package:collection/collection.dart';
import 'package:computer/computer.dart';
import 'package:logging/logging.dart';
import 'package:photo_manager/photo_manager.dart';
@ -27,20 +29,49 @@ Future<List<File>> getDeviceFiles(
return files;
}
Future<Set<String>> getAllLocalIDs() async {
Future<List<LocalAsset>> getAllLocalAssets() async {
final filterOptionGroup = FilterOptionGroup();
final assetPaths = await PhotoManager.getAssetPathList(
hasAll: true,
type: RequestType.common,
filterOption: filterOptionGroup,
);
final ids = new Set<String>();
final List<LocalAsset> assets = [];
for (final assetPath in assetPaths) {
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(
@ -100,3 +131,31 @@ Future<List<File>> _getFiles(Map<String, dynamic> args) async {
}
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);
}