Drop UserComment from both server and local

This commit is contained in:
Ming Ming 2021-10-27 03:51:42 +08:00
parent 83af36f2e7
commit 9010756a08
5 changed files with 117 additions and 9 deletions

View file

@ -30,6 +30,10 @@ class Exif with EquatableMixin {
JsonObj toJson() {
return Map.fromEntries(
// we are filtering out MakerNote here because it's generally very large
// and could exceed the 1MB cursor size limit on Android. Second, the
// content is proprietary and thus useless to us anyway
// UserComment is now also ignored as its size could be very large
data.entries
.where((e) => e.key != "MakerNote" && e.key != "UserComment")
.map((e) {
@ -54,13 +58,7 @@ class Exif with EquatableMixin {
factory Exif.fromJson(JsonObj json) {
return Exif(Map.fromEntries(
// we are filtering out MakerNote here because it's generally very large
// and could exceed the 1MB cursor size limit on Android. Second, the
// content is proprietary and thus useless to us anyway
// UserComment is now also ignored as its size could be very large
json.entries
.where((e) => e.key != "MakerNote" && e.key != "UserComment")
.map((e) {
json.entries.map((e) {
dynamic exifValue;
if (e.value is Map) {
exifValue = Rational.fromJson(e.value.cast<String, dynamic>());

View file

@ -95,6 +95,23 @@ class Metadata with EquatableMixin {
};
}
Metadata copyWith({
OrNull<DateTime>? lastUpdated,
String? fileEtag,
int? imageWidth,
int? imageHeight,
Exif? exif,
}) {
return Metadata(
lastUpdated:
lastUpdated == null ? null : (lastUpdated.obj ?? this.lastUpdated),
fileEtag: fileEtag ?? this.fileEtag,
imageWidth: imageWidth ?? this.imageWidth,
imageHeight: imageHeight ?? this.imageHeight,
exif: exif ?? this.exif,
);
}
@override
toString() {
var product = "$runtimeType {"

View file

@ -15,6 +15,7 @@ import 'package:nc_photos/or_null.dart';
import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util;
import 'package:nc_photos/string_extension.dart';
import 'package:nc_photos/touch_token_manager.dart';
import 'package:nc_photos/use_case/compat/v32.dart';
import 'package:nc_photos/use_case/ls_single_file.dart';
import 'package:path/path.dart' as path;
import 'package:quiver/iterables.dart';
@ -62,9 +63,9 @@ class FileWebdavDataSource implements FileDataSource {
}
final xml = XmlDocument.parse(response.body);
final files = WebdavFileParser()(xml);
var files = WebdavFileParser()(xml);
// _log.fine("[list] Parsed files: [$files]");
return files.where((element) => _validateFile(element)).map((e) {
files = files.where((element) => _validateFile(element)).map((e) {
if (e.metadata == null || e.metadata!.fileEtag == e.etag) {
return e;
} else {
@ -72,6 +73,9 @@ class FileWebdavDataSource implements FileDataSource {
return e.copyWith(metadata: OrNull(null));
}
}).toList();
await _compatUpgrade(account, files);
return files;
}
@override
@ -215,6 +219,21 @@ class FileWebdavDataSource implements FileDataSource {
}
}
Future<void> _compatUpgrade(Account account, List<File> files) async {
for (final f in files.where((element) => element.metadata?.exif != null)) {
if (CompatV32.isExifNeedMigration(f.metadata!.exif!)) {
final newExif = CompatV32.migrateExif(f.metadata!.exif!, f.path);
await updateProperty(
account,
f,
metadata: OrNull(f.metadata!.copyWith(
exif: newExif,
)),
);
}
}
}
static final _log = Logger("entity.file.data_source.FileWebdavDataSource");
}

View file

@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:logging/logging.dart';
import 'package:nc_photos/entity/exif.dart';
import 'package:nc_photos/type.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -36,5 +37,17 @@ class CompatV32 {
}
}
static bool isExifNeedMigration(Exif exif) =>
exif.data.containsKey("UserComment") ||
exif.data.containsKey("MakerNote");
static Exif migrateExif(Exif exif, String logFilename) {
_log.info("[migrateExif] Migrate EXIF for file: $logFilename");
final newData = Map.of(exif.data);
newData.removeWhere(
(key, value) => key == "UserComment" || key == "MakerNote");
return Exif(newData);
}
static final _log = Logger("use_case.compat.v32.CompatV32");
}

View file

@ -1,3 +1,4 @@
import 'package:nc_photos/entity/exif.dart';
import 'package:nc_photos/use_case/compat/v32.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart';
@ -35,5 +36,65 @@ void main() {
]);
expect(pref.containsKey("accounts"), false);
});
group("isExifNeedMigration", () {
test("w/ UserComment", () async {
final exif = Exif(<String, dynamic>{
"UserComment": [1, 2, 3],
});
expect(CompatV32.isExifNeedMigration(exif), true);
});
test("w/ MakerNote", () async {
final exif = Exif(<String, dynamic>{
"MakerNote": [1, 2, 3],
});
expect(CompatV32.isExifNeedMigration(exif), true);
});
test("w/o migration", () async {
final exif = Exif(<String, dynamic>{
"Maker": "Super",
});
expect(CompatV32.isExifNeedMigration(exif), false);
});
});
group("migrateExif", () {
test("w/ UserComment", () async {
final exif = Exif(<String, dynamic>{
"Maker": "Super",
"UserComment": [1, 2, 3],
});
expect(
CompatV32.migrateExif(exif, ""),
Exif(<String, dynamic>{
"Maker": "Super",
}));
});
test("w/ MakerNote", () async {
final exif = Exif(<String, dynamic>{
"Maker": "Super",
"MakerNote": [1, 2, 3],
});
expect(
CompatV32.migrateExif(exif, ""),
Exif(<String, dynamic>{
"Maker": "Super",
}));
});
test("w/o migration", () async {
final exif = Exif(<String, dynamic>{
"Maker": "Super",
});
expect(
CompatV32.migrateExif(exif, ""),
Exif(<String, dynamic>{
"Maker": "Super",
}));
});
});
});
}