fixed parsing & db save on startup

This commit is contained in:
Thibault Deckers 2019-08-11 22:20:26 +09:00
parent e4da59a624
commit 27bf3f4dad
5 changed files with 87 additions and 61 deletions

View file

@ -147,44 +147,47 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
private void getCatalogMetadata(MethodCall call, MethodChannel.Result result) { private void getCatalogMetadata(MethodCall call, MethodChannel.Result result) {
String path = call.argument("path"); String path = call.argument("path");
String mimeType = call.argument("mimeType");
try (InputStream is = new FileInputStream(path)) { try (InputStream is = new FileInputStream(path)) {
Metadata metadata = ImageMetadataReader.readMetadata(is);
Map<String, Object> metadataMap = new HashMap<>(); Map<String, Object> metadataMap = new HashMap<>();
if (!Constants.MIME_MP2TS.equalsIgnoreCase(mimeType)) {
Metadata metadata = ImageMetadataReader.readMetadata(is);
// EXIF Sub-IFD // EXIF Sub-IFD
ExifSubIFDDirectory exifSubDir = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class); ExifSubIFDDirectory exifSubDir = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
if (exifSubDir != null) { if (exifSubDir != null) {
if (exifSubDir.containsTag(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)) { if (exifSubDir.containsTag(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)) {
metadataMap.put("dateMillis", exifSubDir.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL, null, TimeZone.getDefault()).getTime()); metadataMap.put("dateMillis", exifSubDir.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL, null, TimeZone.getDefault()).getTime());
} }
} }
// GPS // GPS
GpsDirectory gpsDir = metadata.getFirstDirectoryOfType(GpsDirectory.class); GpsDirectory gpsDir = metadata.getFirstDirectoryOfType(GpsDirectory.class);
if (gpsDir != null) { if (gpsDir != null) {
GeoLocation geoLocation = gpsDir.getGeoLocation(); GeoLocation geoLocation = gpsDir.getGeoLocation();
if (geoLocation != null) { if (geoLocation != null) {
metadataMap.put("latitude", geoLocation.getLatitude()); metadataMap.put("latitude", geoLocation.getLatitude());
metadataMap.put("longitude", geoLocation.getLongitude()); metadataMap.put("longitude", geoLocation.getLongitude());
} }
} }
// XMP // XMP
XmpDirectory xmpDir = metadata.getFirstDirectoryOfType(XmpDirectory.class); XmpDirectory xmpDir = metadata.getFirstDirectoryOfType(XmpDirectory.class);
if (xmpDir != null) { if (xmpDir != null) {
XMPMeta xmpMeta = xmpDir.getXMPMeta(); XMPMeta xmpMeta = xmpDir.getXMPMeta();
try { try {
if (xmpMeta.doesPropertyExist(XMP_DC_SCHEMA_NS, XMP_SUBJECT_PROP_NAME)) { if (xmpMeta.doesPropertyExist(XMP_DC_SCHEMA_NS, XMP_SUBJECT_PROP_NAME)) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int count = xmpMeta.countArrayItems(XMP_DC_SCHEMA_NS, XMP_SUBJECT_PROP_NAME); int count = xmpMeta.countArrayItems(XMP_DC_SCHEMA_NS, XMP_SUBJECT_PROP_NAME);
for (int i = 1; i < count + 1; i++) { for (int i = 1; i < count + 1; i++) {
XMPProperty item = xmpMeta.getArrayItem(XMP_DC_SCHEMA_NS, XMP_SUBJECT_PROP_NAME, i); XMPProperty item = xmpMeta.getArrayItem(XMP_DC_SCHEMA_NS, XMP_SUBJECT_PROP_NAME, i);
sb.append(";").append(item.getValue()); sb.append(";").append(item.getValue());
} }
metadataMap.put("xmpSubjects", sb.toString()); metadataMap.put("xmpSubjects", sb.toString());
}
} catch (XMPException e) {
e.printStackTrace();
} }
} catch (XMPException e) {
e.printStackTrace();
} }
} }

View file

@ -10,6 +10,7 @@ public class Constants {
public static final String MIME_VIDEO = "video"; public static final String MIME_VIDEO = "video";
public static final String MIME_GIF = "image/gif"; public static final String MIME_GIF = "image/gif";
public static final String MIME_MP2TS = "video/mp2ts";
// video metadata keys, from android.media.MediaMetadataRetriever // video metadata keys, from android.media.MediaMetadataRetriever

View file

@ -1,5 +1,6 @@
import 'package:aves/model/image_decode_service.dart'; import 'package:aves/model/image_decode_service.dart';
import 'package:aves/model/image_entry.dart'; import 'package:aves/model/image_entry.dart';
import 'package:aves/model/image_metadata.dart';
import 'package:aves/model/metadata_db.dart'; import 'package:aves/model/metadata_db.dart';
import 'package:aves/widgets/album/all_collection_page.dart'; import 'package:aves/widgets/album/all_collection_page.dart';
import 'package:aves/widgets/common/fake_app_bar.dart'; import 'package:aves/widgets/common/fake_app_bar.dart';
@ -47,10 +48,11 @@ class _HomePageState extends State<HomePage> {
eventChannel.receiveBroadcastStream().cast<Map>().listen( eventChannel.receiveBroadcastStream().cast<Map>().listen(
(entryMap) => setState(() => entries.add(ImageEntry.fromMap(entryMap))), (entryMap) => setState(() => entries.add(ImageEntry.fromMap(entryMap))),
onDone: () { onDone: () async {
debugPrint('mediastore stream done'); debugPrint('mediastore stream done');
setState(() {}); setState(() {});
catalogEntries(); await catalogEntries();
await locateEntries();
}, },
onError: (error) => debugPrint('mediastore stream error=$error'), onError: (error) => debugPrint('mediastore stream error=$error'),
); );
@ -68,20 +70,36 @@ class _HomePageState extends State<HomePage> {
} }
catalogEntries() async { catalogEntries() async {
debugPrint('$runtimeType catalogEntries cataloging start'); debugPrint('$runtimeType catalogEntries start');
await Future.forEach<ImageEntry>(entries, (entry) async { final start = DateTime.now();
final uncataloguedEntries = entries.where((entry) => !entry.isCataloged);
final newMetadata = List<CatalogMetadata>();
await Future.forEach<ImageEntry>(uncataloguedEntries, (entry) async {
await entry.catalog(); await entry.catalog();
newMetadata.add(entry.catalogMetadata);
}); });
debugPrint('$runtimeType catalogEntries cataloging complete'); debugPrint('$runtimeType catalogEntries complete in ${DateTime.now().difference(start).inSeconds}s with ${newMetadata.length} new entries');
// sort with more accurate date // sort with more accurate date
entries.sort((a, b) => b.bestDate.compareTo(a.bestDate)); entries.sort((a, b) => b.bestDate.compareTo(a.bestDate));
setState(() {}); setState(() {});
debugPrint('$runtimeType catalogEntries locating start'); metadataDb.saveMetadata(List.unmodifiable(newMetadata));
await Future.forEach<ImageEntry>(entries, (entry) async { }
locateEntries() async {
debugPrint('$runtimeType locateEntries start');
final start = DateTime.now();
final unlocatedEntries = entries.where((entry) => !entry.isLocated);
final newAddresses = List<AddressDetails>();
await Future.forEach<ImageEntry>(unlocatedEntries, (entry) async {
await entry.locate(); await entry.locate();
newAddresses.add(entry.addressDetails);
if (newAddresses.length >= 50) {
metadataDb.saveAddresses(List.unmodifiable(newAddresses));
newAddresses.clear();
}
}); });
debugPrint('$runtimeType catalogEntries locating done'); debugPrint('$runtimeType locateEntries complete in ${DateTime.now().difference(start).inSeconds}s with ${newAddresses.length} new addresses');
} }
} }

View file

@ -51,14 +51,18 @@ class MetadataDb {
return null; return null;
} }
insertMetadata(CatalogMetadata metadata) async { saveMetadata(Iterable<CatalogMetadata> metadataEntries) async {
// debugPrint('$runtimeType insertMetadata metadata=$metadata'); if (metadataEntries == null || metadataEntries.isEmpty) return;
final start = DateTime.now();
final db = await _database; final db = await _database;
await db.insert( final batch = db.batch();
metadataTable, metadataEntries.where((metadata) => metadata != null).forEach((metadata) => batch.insert(
metadata.toMap(), metadataTable,
conflictAlgorithm: ConflictAlgorithm.replace, metadata.toMap(),
); conflictAlgorithm: ConflictAlgorithm.replace,
));
await batch.commit(noResult: true);
debugPrint('$runtimeType saveMetadata complete in ${DateTime.now().difference(start).inMilliseconds}ms with ${metadataEntries.length} entries');
} }
Future<List<AddressDetails>> getAllAddresses() async { Future<List<AddressDetails>> getAllAddresses() async {
@ -78,13 +82,17 @@ class MetadataDb {
return null; return null;
} }
insertAddress(AddressDetails metadata) async { saveAddresses(Iterable<AddressDetails> addresses) async {
// debugPrint('$runtimeType insertAddress metadata=$metadata'); if (addresses == null || addresses.isEmpty) return;
final start = DateTime.now();
final db = await _database; final db = await _database;
await db.insert( final batch = db.batch();
addressTable, addresses.where((address) => address != null).forEach((address) => batch.insert(
metadata.toMap(), addressTable,
conflictAlgorithm: ConflictAlgorithm.replace, address.toMap(),
); conflictAlgorithm: ConflictAlgorithm.replace,
));
await batch.commit(noResult: true);
debugPrint('$runtimeType saveAddresses complete in ${DateTime.now().difference(start).inMilliseconds}ms with ${addresses.length} entries');
} }
} }

View file

@ -1,6 +1,5 @@
import 'package:aves/model/image_entry.dart'; import 'package:aves/model/image_entry.dart';
import 'package:aves/model/image_metadata.dart'; import 'package:aves/model/image_metadata.dart';
import 'package:aves/model/metadata_db.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -22,7 +21,6 @@ class MetadataService {
} }
static Future<CatalogMetadata> getCatalogMetadata(ImageEntry entry) async { static Future<CatalogMetadata> getCatalogMetadata(ImageEntry entry) async {
CatalogMetadata metadata;
try { try {
// return map with: // return map with:
// 'dateMillis': date taken in milliseconds since Epoch (long) // 'dateMillis': date taken in milliseconds since Epoch (long)
@ -34,9 +32,7 @@ class MetadataService {
'path': entry.path, 'path': entry.path,
}) as Map; }) as Map;
result['contentId'] = entry.contentId; result['contentId'] = entry.contentId;
metadata = CatalogMetadata.fromMap(result); return CatalogMetadata.fromMap(result);
metadataDb.insertMetadata(metadata);
return metadata;
} on PlatformException catch (e) { } on PlatformException catch (e) {
debugPrint('getCatalogMetadata failed with exception=${e.message}'); debugPrint('getCatalogMetadata failed with exception=${e.message}');
} }