mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-22 08:46:18 +01:00
Refactor: make webdav parser more generic
This commit is contained in:
parent
313d5f3dcb
commit
4c88070a7e
3 changed files with 177 additions and 164 deletions
|
@ -62,7 +62,7 @@ class FileWebdavDataSource implements FileDataSource {
|
|||
}
|
||||
|
||||
final xml = XmlDocument.parse(response.body);
|
||||
var files = WebdavFileParser()(xml);
|
||||
var files = WebdavResponseParser().parseFiles(xml);
|
||||
// _log.fine("[list] Parsed files: [$files]");
|
||||
files = files.where((element) => _validateFile(element)).map((e) {
|
||||
if (e.metadata == null || e.metadata!.fileEtag == e.etag) {
|
||||
|
|
|
@ -7,8 +7,12 @@ import 'package:nc_photos/entity/file.dart';
|
|||
import 'package:nc_photos/string_extension.dart';
|
||||
import 'package:xml/xml.dart';
|
||||
|
||||
class WebdavFileParser {
|
||||
List<File> call(XmlDocument xml) {
|
||||
class WebdavResponseParser {
|
||||
List<File> parseFiles(XmlDocument xml) => _parse<File>(xml, _toFile);
|
||||
|
||||
Map<String, String> get namespaces => _namespaces;
|
||||
|
||||
List<T> _parse<T>(XmlDocument xml, T Function(XmlElement) mapper) {
|
||||
_namespaces = _parseNamespaces(xml);
|
||||
final body = () {
|
||||
try {
|
||||
|
@ -16,28 +20,26 @@ class WebdavFileParser {
|
|||
element.matchQualifiedName("multistatus",
|
||||
prefix: "DAV:", namespaces: _namespaces));
|
||||
} catch (_) {
|
||||
_log.shout("[call] Missing element: multistatus");
|
||||
_log.shout("[_parse] Missing element: multistatus");
|
||||
rethrow;
|
||||
}
|
||||
}();
|
||||
return body.children
|
||||
.whereType<XmlElement>()
|
||||
.where((element) => element.matchQualifiedName("response",
|
||||
.where((e) => e.matchQualifiedName("response",
|
||||
prefix: "DAV:", namespaces: _namespaces))
|
||||
.map((element) {
|
||||
.map((e) {
|
||||
try {
|
||||
return _toFile(element);
|
||||
} catch (e, stacktrace) {
|
||||
_log.shout("[call] Failed parsing XML", e, stacktrace);
|
||||
return mapper(e);
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout("[_parse] Failed parsing XML", e, stackTrace);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.whereType<File>()
|
||||
.whereType<T>()
|
||||
.toList();
|
||||
}
|
||||
|
||||
Map<String, String> get namespaces => _namespaces;
|
||||
|
||||
Map<String, String> _parseNamespaces(XmlDocument xml) {
|
||||
final namespaces = <String, String>{};
|
||||
final xmlContent = xml.descendants.whereType<XmlElement>().firstWhere(
|
||||
|
@ -91,7 +93,7 @@ class WebdavFileParser {
|
|||
(element) => element.matchQualifiedName("prop",
|
||||
prefix: "DAV:", namespaces: _namespaces));
|
||||
final propParser =
|
||||
_PropParser(namespaces: _namespaces, logFilePath: path);
|
||||
_FilePropParser(namespaces: _namespaces, logFilePath: path);
|
||||
propParser.parse(prop);
|
||||
contentLength = propParser.contentLength;
|
||||
contentType = propParser.contentType;
|
||||
|
@ -149,8 +151,8 @@ class WebdavFileParser {
|
|||
Logger("entity.webdav_response_parser.WebdavResponseParser");
|
||||
}
|
||||
|
||||
class _PropParser {
|
||||
_PropParser({
|
||||
class _FilePropParser {
|
||||
_FilePropParser({
|
||||
this.namespaces = const {},
|
||||
this.logFilePath,
|
||||
});
|
||||
|
|
|
@ -5,8 +5,21 @@ import 'package:xml/xml.dart';
|
|||
|
||||
void main() {
|
||||
group("WebdavFileParser", () {
|
||||
test("file", () {
|
||||
final xml = XmlDocument.parse("""
|
||||
group("parseFiles", () {
|
||||
test("file", _files);
|
||||
test("file w/ 404 properties", _files404props);
|
||||
test("file w/ metadata", _filesMetadata);
|
||||
test("file w/ is-archived", _filesIsArchived);
|
||||
test("file w/ override-date-time", _filesOverrideDateTime);
|
||||
test("multiple files", _filesMultiple);
|
||||
test("directory", _filesDir);
|
||||
test("nextcloud hosted in subdir", _filesServerHostedInSubdir);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void _files() {
|
||||
final xml = XmlDocument.parse("""
|
||||
<?xml version="1.0"?>
|
||||
<d:multistatus xmlns:d="DAV:"
|
||||
xmlns:s="http://sabredav.org/ns"
|
||||
|
@ -29,23 +42,23 @@ void main() {
|
|||
</d:response>
|
||||
</d:multistatus>
|
||||
""");
|
||||
final results = WebdavFileParser()(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud intro.mp4",
|
||||
contentLength: 3963036,
|
||||
contentType: "video/mp4",
|
||||
etag: "1324f58d4d5c8d81bed6e4ed9d5ea862",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
),
|
||||
]);
|
||||
});
|
||||
final results = WebdavResponseParser().parseFiles(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud intro.mp4",
|
||||
contentLength: 3963036,
|
||||
contentType: "video/mp4",
|
||||
etag: "1324f58d4d5c8d81bed6e4ed9d5ea862",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
test("file w/ 404 properties", () {
|
||||
final xml = XmlDocument.parse("""
|
||||
void _files404props() {
|
||||
final xml = XmlDocument.parse("""
|
||||
<?xml version="1.0"?>
|
||||
<d:multistatus xmlns:d="DAV:"
|
||||
xmlns:s="http://sabredav.org/ns"
|
||||
|
@ -75,23 +88,23 @@ void main() {
|
|||
</d:response>
|
||||
</d:multistatus>
|
||||
""");
|
||||
final results = WebdavFileParser()(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud intro.mp4",
|
||||
contentLength: 3963036,
|
||||
contentType: "video/mp4",
|
||||
etag: "1324f58d4d5c8d81bed6e4ed9d5ea862",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
),
|
||||
]);
|
||||
});
|
||||
final results = WebdavResponseParser().parseFiles(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud intro.mp4",
|
||||
contentLength: 3963036,
|
||||
contentType: "video/mp4",
|
||||
etag: "1324f58d4d5c8d81bed6e4ed9d5ea862",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
test("file w/ metadata", () {
|
||||
final xml = XmlDocument.parse("""
|
||||
void _filesMetadata() {
|
||||
final xml = XmlDocument.parse("""
|
||||
<?xml version="1.0"?>
|
||||
<d:multistatus xmlns:d="DAV:"
|
||||
xmlns:s="http://sabredav.org/ns"
|
||||
|
@ -115,29 +128,29 @@ void main() {
|
|||
</d:response>
|
||||
</d:multistatus>
|
||||
""");
|
||||
final results = WebdavFileParser()(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Photos/Nextcloud community.jpg",
|
||||
contentLength: 797325,
|
||||
contentType: "image/jpeg",
|
||||
etag: "8950e39a034e369237d9285e2d815a50",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: true,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
metadata: Metadata(
|
||||
lastUpdated: DateTime.utc(2021, 1, 2, 3, 4, 5, 678),
|
||||
fileEtag: "8950e39a034e369237d9285e2d815a50",
|
||||
imageWidth: 3000,
|
||||
imageHeight: 2000,
|
||||
),
|
||||
),
|
||||
]);
|
||||
});
|
||||
final results = WebdavResponseParser().parseFiles(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Photos/Nextcloud community.jpg",
|
||||
contentLength: 797325,
|
||||
contentType: "image/jpeg",
|
||||
etag: "8950e39a034e369237d9285e2d815a50",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: true,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
metadata: Metadata(
|
||||
lastUpdated: DateTime.utc(2021, 1, 2, 3, 4, 5, 678),
|
||||
fileEtag: "8950e39a034e369237d9285e2d815a50",
|
||||
imageWidth: 3000,
|
||||
imageHeight: 2000,
|
||||
),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
test("file w/ is-archived", () {
|
||||
final xml = XmlDocument.parse("""
|
||||
void _filesIsArchived() {
|
||||
final xml = XmlDocument.parse("""
|
||||
<?xml version="1.0"?>
|
||||
<d:multistatus xmlns:d="DAV:"
|
||||
xmlns:s="http://sabredav.org/ns"
|
||||
|
@ -160,23 +173,23 @@ void main() {
|
|||
</d:response>
|
||||
</d:multistatus>
|
||||
""");
|
||||
final results = WebdavFileParser()(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Photos/Nextcloud community.jpg",
|
||||
contentLength: 797325,
|
||||
contentType: "image/jpeg",
|
||||
etag: "8950e39a034e369237d9285e2d815a50",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: true,
|
||||
isCollection: false,
|
||||
isArchived: true,
|
||||
),
|
||||
]);
|
||||
});
|
||||
final results = WebdavResponseParser().parseFiles(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Photos/Nextcloud community.jpg",
|
||||
contentLength: 797325,
|
||||
contentType: "image/jpeg",
|
||||
etag: "8950e39a034e369237d9285e2d815a50",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: true,
|
||||
isCollection: false,
|
||||
isArchived: true,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
test("file w/ override-date-time", () {
|
||||
final xml = XmlDocument.parse("""
|
||||
void _filesOverrideDateTime() {
|
||||
final xml = XmlDocument.parse("""
|
||||
<?xml version="1.0"?>
|
||||
<d:multistatus xmlns:d="DAV:"
|
||||
xmlns:s="http://sabredav.org/ns"
|
||||
|
@ -199,23 +212,23 @@ void main() {
|
|||
</d:response>
|
||||
</d:multistatus>
|
||||
""");
|
||||
final results = WebdavFileParser()(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Photos/Nextcloud community.jpg",
|
||||
contentLength: 797325,
|
||||
contentType: "image/jpeg",
|
||||
etag: "8950e39a034e369237d9285e2d815a50",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: true,
|
||||
isCollection: false,
|
||||
overrideDateTime: DateTime.utc(2021, 1, 2, 3, 4, 5),
|
||||
),
|
||||
]);
|
||||
});
|
||||
final results = WebdavResponseParser().parseFiles(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Photos/Nextcloud community.jpg",
|
||||
contentLength: 797325,
|
||||
contentType: "image/jpeg",
|
||||
etag: "8950e39a034e369237d9285e2d815a50",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: true,
|
||||
isCollection: false,
|
||||
overrideDateTime: DateTime.utc(2021, 1, 2, 3, 4, 5),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
test("multiple files", () {
|
||||
final xml = XmlDocument.parse("""
|
||||
void _filesMultiple() {
|
||||
final xml = XmlDocument.parse("""
|
||||
<?xml version="1.0"?>
|
||||
<d:multistatus xmlns:d="DAV:"
|
||||
xmlns:s="http://sabredav.org/ns"
|
||||
|
@ -254,39 +267,39 @@ void main() {
|
|||
</d:response>
|
||||
</d:multistatus>
|
||||
""");
|
||||
final results = WebdavFileParser()(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud intro.mp4",
|
||||
contentLength: 3963036,
|
||||
contentType: "video/mp4",
|
||||
etag: "1324f58d4d5c8d81bed6e4ed9d5ea862",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
),
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud.png",
|
||||
contentLength: 50598,
|
||||
contentType: "image/png",
|
||||
etag: "48689d5b17c449d9db492ffe8f7ab8a6",
|
||||
lastModified: DateTime.utc(2021, 1, 2, 3, 4, 5),
|
||||
hasPreview: true,
|
||||
fileId: 124,
|
||||
isCollection: false,
|
||||
metadata: Metadata(
|
||||
fileEtag: "48689d5b17c449d9db492ffe8f7ab8a6",
|
||||
imageWidth: 500,
|
||||
imageHeight: 500,
|
||||
lastUpdated: DateTime.utc(2021, 1, 2, 3, 4, 5, 678),
|
||||
),
|
||||
),
|
||||
]);
|
||||
});
|
||||
final results = WebdavResponseParser().parseFiles(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud intro.mp4",
|
||||
contentLength: 3963036,
|
||||
contentType: "video/mp4",
|
||||
etag: "1324f58d4d5c8d81bed6e4ed9d5ea862",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
),
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud.png",
|
||||
contentLength: 50598,
|
||||
contentType: "image/png",
|
||||
etag: "48689d5b17c449d9db492ffe8f7ab8a6",
|
||||
lastModified: DateTime.utc(2021, 1, 2, 3, 4, 5),
|
||||
hasPreview: true,
|
||||
fileId: 124,
|
||||
isCollection: false,
|
||||
metadata: Metadata(
|
||||
fileEtag: "48689d5b17c449d9db492ffe8f7ab8a6",
|
||||
imageWidth: 500,
|
||||
imageHeight: 500,
|
||||
lastUpdated: DateTime.utc(2021, 1, 2, 3, 4, 5, 678),
|
||||
),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
test("directory", () {
|
||||
final xml = XmlDocument.parse("""
|
||||
void _filesDir() {
|
||||
final xml = XmlDocument.parse("""
|
||||
<?xml version="1.0"?>
|
||||
<d:multistatus xmlns:d="DAV:"
|
||||
xmlns:s="http://sabredav.org/ns"
|
||||
|
@ -317,21 +330,21 @@ void main() {
|
|||
</d:response>
|
||||
</d:multistatus>
|
||||
""");
|
||||
final results = WebdavFileParser()(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Photos",
|
||||
etag: "123456789abcd",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
isCollection: true,
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
),
|
||||
]);
|
||||
});
|
||||
final results = WebdavResponseParser().parseFiles(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Photos",
|
||||
etag: "123456789abcd",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
isCollection: true,
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
test("nextcloud hosed in subdir", () {
|
||||
final xml = XmlDocument.parse("""
|
||||
void _filesServerHostedInSubdir() {
|
||||
final xml = XmlDocument.parse("""
|
||||
<?xml version="1.0"?>
|
||||
<d:multistatus xmlns:d="DAV:"
|
||||
xmlns:s="http://sabredav.org/ns"
|
||||
|
@ -354,19 +367,17 @@ void main() {
|
|||
</d:response>
|
||||
</d:multistatus>
|
||||
""");
|
||||
final results = WebdavFileParser()(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud intro.mp4",
|
||||
contentLength: 3963036,
|
||||
contentType: "video/mp4",
|
||||
etag: "1324f58d4d5c8d81bed6e4ed9d5ea862",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
),
|
||||
]);
|
||||
});
|
||||
});
|
||||
final results = WebdavResponseParser().parseFiles(xml);
|
||||
expect(results, [
|
||||
File(
|
||||
path: "remote.php/dav/files/admin/Nextcloud intro.mp4",
|
||||
contentLength: 3963036,
|
||||
contentType: "video/mp4",
|
||||
etag: "1324f58d4d5c8d81bed6e4ed9d5ea862",
|
||||
lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4),
|
||||
hasPreview: false,
|
||||
fileId: 123,
|
||||
isCollection: false,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue