mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-12 20:47:42 +01:00
Improve album compatibility handling
This commit is contained in:
parent
8d1fbea069
commit
d4785b1f74
4 changed files with 72 additions and 85 deletions
|
@ -4,6 +4,7 @@ import 'package:idb_shim/idb.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:nc_photos/account.dart';
|
import 'package:nc_photos/account.dart';
|
||||||
import 'package:nc_photos/entity/album.dart';
|
import 'package:nc_photos/entity/album.dart';
|
||||||
|
import 'package:nc_photos/entity/album/upgrader.dart';
|
||||||
import 'package:nc_photos/entity/file.dart';
|
import 'package:nc_photos/entity/file.dart';
|
||||||
import 'package:nc_photos/mobile/platform.dart'
|
import 'package:nc_photos/mobile/platform.dart'
|
||||||
if (dart.library.html) 'package:nc_photos/web/platform.dart' as platform;
|
if (dart.library.html) 'package:nc_photos/web/platform.dart' as platform;
|
||||||
|
@ -134,7 +135,10 @@ class AppDbAlbumEntry {
|
||||||
return AppDbAlbumEntry(
|
return AppDbAlbumEntry(
|
||||||
json["path"],
|
json["path"],
|
||||||
json["index"],
|
json["index"],
|
||||||
Album.fromJson(json["album"].cast<String, dynamic>()),
|
Album.fromJson(
|
||||||
|
json["album"].cast<String, dynamic>(),
|
||||||
|
upgraderV1: AlbumUpgraderV1(),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import 'package:idb_sqflite/idb_sqflite.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:nc_photos/account.dart';
|
import 'package:nc_photos/account.dart';
|
||||||
import 'package:nc_photos/app_db.dart';
|
import 'package:nc_photos/app_db.dart';
|
||||||
|
import 'package:nc_photos/entity/album/upgrader.dart';
|
||||||
import 'package:nc_photos/entity/file.dart';
|
import 'package:nc_photos/entity/file.dart';
|
||||||
import 'package:nc_photos/entity/file/data_source.dart';
|
import 'package:nc_photos/entity/file/data_source.dart';
|
||||||
import 'package:nc_photos/exception.dart';
|
import 'package:nc_photos/exception.dart';
|
||||||
|
@ -131,34 +132,19 @@ class Album with EquatableMixin {
|
||||||
this.name = name ?? "",
|
this.name = name ?? "",
|
||||||
this.items = UnmodifiableListView(items);
|
this.items = UnmodifiableListView(items);
|
||||||
|
|
||||||
factory Album.versioned({
|
factory Album.fromJson(
|
||||||
int version,
|
Map<String, dynamic> json, {
|
||||||
DateTime lastUpdated,
|
AlbumUpgraderV1 upgraderV1,
|
||||||
@required String name,
|
|
||||||
@required List<AlbumItem> items,
|
|
||||||
File albumFile,
|
|
||||||
}) {
|
}) {
|
||||||
// there's only one version right now
|
final jsonVersion = json["version"];
|
||||||
if (version < 2) {
|
if (jsonVersion < 2) {
|
||||||
return Album(
|
json = upgraderV1?.call(json);
|
||||||
lastUpdated: lastUpdated,
|
if (json == null) {
|
||||||
name: name,
|
_log.info("[fromJson] Version $jsonVersion not compatible");
|
||||||
items: [],
|
return null;
|
||||||
albumFile: albumFile,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
return Album(
|
|
||||||
lastUpdated: lastUpdated,
|
|
||||||
name: name,
|
|
||||||
items: items,
|
|
||||||
albumFile: albumFile,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return Album(
|
||||||
factory Album.fromJson(Map<String, dynamic> json) {
|
|
||||||
return Album.versioned(
|
|
||||||
version: json["version"],
|
|
||||||
lastUpdated: json["lastUpdated"] == null
|
lastUpdated: json["lastUpdated"] == null
|
||||||
? null
|
? null
|
||||||
: DateTime.parse(json["lastUpdated"]),
|
: DateTime.parse(json["lastUpdated"]),
|
||||||
|
@ -292,8 +278,10 @@ class AlbumRemoteDataSource implements AlbumDataSource {
|
||||||
final fileRepo = FileRepo(FileWebdavDataSource());
|
final fileRepo = FileRepo(FileWebdavDataSource());
|
||||||
final data = await GetFileBinary(fileRepo)(account, albumFile);
|
final data = await GetFileBinary(fileRepo)(account, albumFile);
|
||||||
try {
|
try {
|
||||||
return Album.fromJson(jsonDecode(utf8.decode(data)))
|
return Album.fromJson(
|
||||||
.copyWith(albumFile: albumFile);
|
jsonDecode(utf8.decode(data)),
|
||||||
|
upgraderV1: AlbumUpgraderV1(),
|
||||||
|
).copyWith(albumFile: albumFile);
|
||||||
} catch (e, stacktrace) {
|
} catch (e, stacktrace) {
|
||||||
dynamic d = data;
|
dynamic d = data;
|
||||||
try {
|
try {
|
||||||
|
|
25
lib/entity/album/upgrader.dart
Normal file
25
lib/entity/album/upgrader.dart
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
abstract class AlbumUpgrader {
|
||||||
|
Map<String, dynamic> call(Map<String, dynamic> json);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Upgrade v1 Album to v2
|
||||||
|
class AlbumUpgraderV1 implements AlbumUpgrader {
|
||||||
|
AlbumUpgraderV1({
|
||||||
|
this.logFilePath,
|
||||||
|
});
|
||||||
|
|
||||||
|
Map<String, dynamic> call(Map<String, dynamic> json) {
|
||||||
|
// v1 album items are corrupted in one of the updates, drop it
|
||||||
|
_log.fine("[call] Upgrade v1 Album for file: $logFilePath");
|
||||||
|
final result = Map<String, dynamic>.from(json);
|
||||||
|
result["items"] = [];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// File path for logging only
|
||||||
|
final String logFilePath;
|
||||||
|
|
||||||
|
static final _log = Logger("entity.album.upgrader.AlbumUpgraderV1");
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import 'package:nc_photos/entity/album.dart';
|
import 'package:nc_photos/entity/album.dart';
|
||||||
|
import 'package:nc_photos/entity/album/upgrader.dart';
|
||||||
import 'package:nc_photos/entity/file.dart';
|
import 'package:nc_photos/entity/file.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
@ -250,62 +251,31 @@ void main() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group("versioned", () {
|
test("AlbumUpgraderV1", () {
|
||||||
test("v1", () {
|
final json = <String, dynamic>{
|
||||||
final album = Album.versioned(
|
"version": 1,
|
||||||
version: 1,
|
"lastUpdated": "2020-01-02T03:04:05.678901Z",
|
||||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
"items": [
|
||||||
name: "album",
|
<String, dynamic>{
|
||||||
items: [
|
"type": "file",
|
||||||
AlbumFileItem(
|
"content": <String, dynamic>{
|
||||||
file: File(path: "remote.php/dav/files/admin/test1.jpg"),
|
"file": <String, dynamic>{
|
||||||
),
|
"path": "remote.php/dav/files/admin/test1.jpg",
|
||||||
AlbumFileItem(
|
},
|
||||||
file: File(path: "remote.php/dav/files/admin/test2.jpg"),
|
},
|
||||||
),
|
},
|
||||||
],
|
],
|
||||||
albumFile: File(path: "remote.php/dav/files/admin/test1.jpg"),
|
"albumFile": <String, dynamic>{
|
||||||
);
|
"path": "remote.php/dav/files/admin/test1.json",
|
||||||
expect(
|
},
|
||||||
album,
|
};
|
||||||
Album(
|
expect(AlbumUpgraderV1()(json), <String, dynamic>{
|
||||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
"version": 1,
|
||||||
name: "album",
|
"lastUpdated": "2020-01-02T03:04:05.678901Z",
|
||||||
items: [],
|
"items": [],
|
||||||
albumFile: File(path: "remote.php/dav/files/admin/test1.jpg"),
|
"albumFile": <String, dynamic>{
|
||||||
));
|
"path": "remote.php/dav/files/admin/test1.json",
|
||||||
});
|
},
|
||||||
|
|
||||||
test("v2", () {
|
|
||||||
final album = Album.versioned(
|
|
||||||
version: 2,
|
|
||||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
|
||||||
name: "album",
|
|
||||||
items: [
|
|
||||||
AlbumFileItem(
|
|
||||||
file: File(path: "remote.php/dav/files/admin/test1.jpg"),
|
|
||||||
),
|
|
||||||
AlbumFileItem(
|
|
||||||
file: File(path: "remote.php/dav/files/admin/test2.jpg"),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
albumFile: File(path: "remote.php/dav/files/admin/test1.jpg"),
|
|
||||||
);
|
|
||||||
expect(
|
|
||||||
album,
|
|
||||||
Album(
|
|
||||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
|
||||||
name: "album",
|
|
||||||
items: [
|
|
||||||
AlbumFileItem(
|
|
||||||
file: File(path: "remote.php/dav/files/admin/test1.jpg"),
|
|
||||||
),
|
|
||||||
AlbumFileItem(
|
|
||||||
file: File(path: "remote.php/dav/files/admin/test2.jpg"),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
albumFile: File(path: "remote.php/dav/files/admin/test1.jpg"),
|
|
||||||
));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue