fix: generalize xml parser logic

This commit is contained in:
Prateek Sunal 2024-04-07 12:10:20 +05:30
parent 2b9db5cbfb
commit 780ec7f9d6
2 changed files with 27 additions and 17 deletions

View file

@ -11,11 +11,22 @@ import "package:photos/utils/xml_parser_util.dart";
final _enteDio = NetworkClient.instance.enteDio;
final _dio = NetworkClient.instance.getDio();
class PartETag {
class PartETag extends XmlParsableObject {
final int partNumber;
final String eTag;
PartETag(this.partNumber, this.eTag);
@override
String get elementName => "Part";
@override
Map<String, dynamic> toMap() {
return {
"PartNumber": partNumber,
"ETag": eTag,
};
}
}
class MultipartUploadURLs {
@ -59,7 +70,7 @@ Future<MultipartUploadURLs> getMultipartUploadURLs(int count) async {
}
}
Future<void> putMultipartFile(
Future<String> putMultipartFile(
MultipartUploadURLs urls,
File encryptedFile,
) async {
@ -68,6 +79,8 @@ Future<void> putMultipartFile(
// complete the multipart upload
await completeMultipartUpload(etags, urls.completeURL);
return urls.objectKey;
}
Future<Map<int, String>> uploadParts(

View file

@ -1,9 +1,14 @@
// ignore_for_file: implementation_imports
import "package:photos/utils/multipart_upload_util.dart";
import "package:xml/src/xml/entities/named_entities.dart";
import "package:xml/xml.dart";
// used for classes that can be converted to xml
abstract class XmlParsableObject {
Map<String, dynamic> toMap();
String get elementName;
}
// for converting the response to xml
String convertJs2Xml(Map<String, dynamic> json) {
final builder = XmlBuilder();
@ -15,6 +20,7 @@ String convertJs2Xml(Map<String, dynamic> json) {
);
}
// for building the xml node tree recursively
void buildXml(XmlBuilder builder, dynamic node) {
if (node is Map<String, dynamic>) {
node.forEach((key, value) {
@ -24,22 +30,11 @@ void buildXml(XmlBuilder builder, dynamic node) {
for (var item in node) {
buildXml(builder, item);
}
} else if (node is PartETag) {
} else if (node is XmlParsableObject) {
builder.element(
"Part",
node.elementName,
nest: () {
builder.element(
"PartNumber",
nest: () {
buildXml(builder, node.partNumber);
},
);
builder.element(
"ETag",
nest: () {
buildXml(builder, node.eTag);
},
);
buildXml(builder, node.toMap());
},
);
} else {
@ -47,6 +42,8 @@ void buildXml(XmlBuilder builder, dynamic node) {
}
}
// for removing the &quot; from the xml string response.
// couldn't find better way to do this
XmlEntityMapping defaultMyEntityMapping = MyXmlDefaultEntityMapping.xml();
class MyXmlDefaultEntityMapping extends XmlDefaultEntityMapping {