Fix the title matching logic for remote HEIC files that was triggering duplicate entries
This commit is contained in:
parent
d178b0838d
commit
a99bfde90d
4 changed files with 36 additions and 13 deletions
|
@ -133,14 +133,21 @@ class PhotoDB {
|
|||
return _convertToPhotos(results);
|
||||
}
|
||||
|
||||
Future<Photo> getMatchingPhoto(String localId, String title,
|
||||
String deviceFolder, int createTimestamp) async {
|
||||
Future<Photo> getMatchingPhoto(
|
||||
String localId, String title, String deviceFolder, int createTimestamp,
|
||||
{String alternateTitle}) async {
|
||||
final db = await instance.database;
|
||||
final rows = await db.query(
|
||||
table,
|
||||
where:
|
||||
'$columnLocalId=? AND $columnTitle=? AND $columnDeviceFolder=? AND $columnCreateTimestamp=?',
|
||||
whereArgs: [localId, title, deviceFolder, createTimestamp],
|
||||
where: '''$columnLocalId=? AND ($columnTitle=? OR $columnTitle=?) AND
|
||||
$columnDeviceFolder=? AND $columnCreateTimestamp=?''',
|
||||
whereArgs: [
|
||||
localId,
|
||||
title,
|
||||
alternateTitle,
|
||||
deviceFolder,
|
||||
createTimestamp
|
||||
],
|
||||
);
|
||||
if (rows.isNotEmpty) {
|
||||
return _getPhotoFromRow(rows[0]);
|
||||
|
|
|
@ -5,6 +5,7 @@ import 'package:logging/logging.dart';
|
|||
|
||||
import 'package:photos/models/face.dart';
|
||||
import 'package:photos/models/photo.dart';
|
||||
import 'package:photos/utils/file_name_util.dart';
|
||||
|
||||
class FaceSearchManager {
|
||||
final _logger = Logger("FaceSearchManager");
|
||||
|
@ -48,7 +49,8 @@ class FaceSearchManager {
|
|||
for (Photo photo in result) {
|
||||
try {
|
||||
photos.add(await PhotoDB.instance.getMatchingPhoto(photo.localId,
|
||||
photo.title, photo.deviceFolder, photo.createTimestamp));
|
||||
photo.title, photo.deviceFolder, photo.createTimestamp,
|
||||
alternateTitle: getHEICFileNameForJPG(photo)));
|
||||
} catch (e) {
|
||||
// Not available locally
|
||||
photos.add(photo);
|
||||
|
|
|
@ -10,9 +10,9 @@ import 'package:photos/events/user_authenticated_event.dart';
|
|||
import 'package:photos/photo_repository.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:photo_manager/photo_manager.dart';
|
||||
import 'package:photos/utils/file_name_util.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:photos/models/photo.dart';
|
||||
|
||||
import 'package:photos/core/configuration.dart';
|
||||
|
@ -166,8 +166,7 @@ class PhotoSyncManager {
|
|||
}
|
||||
|
||||
Future<void> _downloadDiff(SharedPreferences prefs) async {
|
||||
int lastSyncTimestamp = _getLastSyncTimestamp(prefs);
|
||||
var diff = await _getDiff(lastSyncTimestamp, _diffLimit);
|
||||
var diff = await _getDiff(_getLastSyncTimestamp(prefs), _diffLimit);
|
||||
if (diff != null && diff.isNotEmpty) {
|
||||
await _storeDiff(diff, prefs);
|
||||
PhotoRepository.instance.reloadPhotos();
|
||||
|
@ -209,12 +208,15 @@ class PhotoSyncManager {
|
|||
for (Photo photo in diff) {
|
||||
try {
|
||||
var existingPhoto = await _db.getMatchingPhoto(photo.localId,
|
||||
photo.title, photo.deviceFolder, photo.createTimestamp);
|
||||
photo.title, photo.deviceFolder, photo.createTimestamp,
|
||||
alternateTitle: getHEICFileNameForJPG(photo));
|
||||
await _db.updatePhoto(existingPhoto.generatedId, photo.uploadedFileId,
|
||||
photo.remotePath, photo.updateTimestamp, photo.thumbnailPath);
|
||||
} catch (e) {
|
||||
await _db.insertPhoto(photo);
|
||||
}
|
||||
// _logger.info(
|
||||
// "Setting update timestamp to " + photo.updateTimestamp.toString());
|
||||
await prefs.setInt(_lastSyncTimestampKey, photo.updateTimestamp);
|
||||
}
|
||||
}
|
||||
|
@ -241,9 +243,7 @@ class PhotoSyncManager {
|
|||
}
|
||||
|
||||
Future<Photo> _uploadFile(Photo localPhoto) async {
|
||||
var title = extension(localPhoto.title) == ".HEIC"
|
||||
? basenameWithoutExtension(localPhoto.title) + ".JPG"
|
||||
: localPhoto.title;
|
||||
var title = getJPGFileNameForHEIC(localPhoto);
|
||||
var formData = FormData.fromMap({
|
||||
"file": MultipartFile.fromBytes((await localPhoto.getBytes()),
|
||||
filename: title),
|
||||
|
|
14
lib/utils/file_name_util.dart
Normal file
14
lib/utils/file_name_util.dart
Normal file
|
@ -0,0 +1,14 @@
|
|||
import 'package:photos/models/photo.dart';
|
||||
import 'package:path/path.dart';
|
||||
|
||||
String getJPGFileNameForHEIC(Photo photo) {
|
||||
return extension(photo.title) == ".HEIC"
|
||||
? basenameWithoutExtension(photo.title) + ".JPG"
|
||||
: photo.title;
|
||||
}
|
||||
|
||||
String getHEICFileNameForJPG(Photo photo) {
|
||||
return extension(photo.title) == ".JPG"
|
||||
? basenameWithoutExtension(photo.title) + ".HEIC"
|
||||
: photo.title;
|
||||
}
|
Loading…
Add table
Reference in a new issue