mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 21:06:31 +03:00
simplify location parsing
This commit is contained in:
@@ -766,14 +766,11 @@ impl<'a> MimeParser<'a> {
|
|||||||
// XXX what if somebody sends eg an "location-highlights.kml"
|
// XXX what if somebody sends eg an "location-highlights.kml"
|
||||||
// attachment unrelated to location streaming?
|
// attachment unrelated to location streaming?
|
||||||
if filename.starts_with("location") || filename.starts_with("message") {
|
if filename.starts_with("location") || filename.starts_with("message") {
|
||||||
let d = std::string::String::from_utf8_lossy(&decoded_data);
|
let parsed = location::Kml::parse(self.context, decoded_data)
|
||||||
let parsed = match location::Kml::parse(self.context, &d) {
|
.map_err(|err| {
|
||||||
Ok(res) => Some(res),
|
|
||||||
Err(err) => {
|
|
||||||
warn!(self.context, "failed to parse kml part: {}", err);
|
warn!(self.context, "failed to parse kml part: {}", err);
|
||||||
None
|
})
|
||||||
}
|
.ok();
|
||||||
};
|
|
||||||
if filename.starts_with("location") {
|
if filename.starts_with("location") {
|
||||||
self.location_kml = parsed;
|
self.location_kml = parsed;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -62,14 +62,11 @@ impl Kml {
|
|||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(context: &Context, content: impl AsRef<str>) -> Result<Self, Error> {
|
pub fn parse(context: &Context, content: &[u8]) -> Result<Self, Error> {
|
||||||
ensure!(
|
ensure!(content.len() <= (1024 * 1024), "kml-file too large");
|
||||||
content.as_ref().len() <= (1024 * 1024),
|
|
||||||
"A kml-files with {} bytes is larger than reasonably expected.",
|
|
||||||
content.as_ref().len()
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut reader = quick_xml::Reader::from_str(content.as_ref());
|
let to_parse = String::from_utf8_lossy(content);
|
||||||
|
let mut reader = quick_xml::Reader::from_str(&to_parse);
|
||||||
reader.trim_text(true);
|
reader.trim_text(true);
|
||||||
|
|
||||||
let mut kml = Kml::new();
|
let mut kml = Kml::new();
|
||||||
@@ -674,7 +671,7 @@ mod tests {
|
|||||||
let xml =
|
let xml =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"user@example.org\">\n<Placemark><Timestamp><when>2019-03-06T21:09:57Z</when></Timestamp><Point><coordinates accuracy=\"32.000000\">9.423110,53.790302</coordinates></Point></Placemark>\n<PlaceMARK>\n<Timestamp><WHEN > \n\t2018-12-13T22:11:12Z\t</WHEN></Timestamp><Point><coordinates aCCuracy=\"2.500000\"> 19.423110 \t , \n 63.790302\n </coordinates></Point></PlaceMARK>\n</Document>\n</kml>";
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"user@example.org\">\n<Placemark><Timestamp><when>2019-03-06T21:09:57Z</when></Timestamp><Point><coordinates accuracy=\"32.000000\">9.423110,53.790302</coordinates></Point></Placemark>\n<PlaceMARK>\n<Timestamp><WHEN > \n\t2018-12-13T22:11:12Z\t</WHEN></Timestamp><Point><coordinates aCCuracy=\"2.500000\"> 19.423110 \t , \n 63.790302\n </coordinates></Point></PlaceMARK>\n</Document>\n</kml>";
|
||||||
|
|
||||||
let kml = Kml::parse(&context.ctx, &xml).expect("parsing failed");
|
let kml = Kml::parse(&context.ctx, &(xml.as_bytes())).expect("parsing failed");
|
||||||
|
|
||||||
assert!(kml.addr.is_some());
|
assert!(kml.addr.is_some());
|
||||||
assert_eq!(kml.addr.as_ref().unwrap(), "user@example.org",);
|
assert_eq!(kml.addr.as_ref().unwrap(), "user@example.org",);
|
||||||
|
|||||||
@@ -503,10 +503,6 @@ impl<'a> MimeFactory<'a> {
|
|||||||
if location::is_sending_locations_to_chat(context, self.msg.chat_id) {
|
if location::is_sending_locations_to_chat(context, self.msg.chat_id) {
|
||||||
match location::get_kml(context, self.msg.chat_id) {
|
match location::get_kml(context, self.msg.chat_id) {
|
||||||
Ok((kml_content, last_added_location_id)) => {
|
Ok((kml_content, last_added_location_id)) => {
|
||||||
info!(
|
|
||||||
context,
|
|
||||||
"adding location.kml to mime message: {}", kml_content
|
|
||||||
);
|
|
||||||
wrapmime::add_filename_part(
|
wrapmime::add_filename_part(
|
||||||
message,
|
message,
|
||||||
"location.kml",
|
"location.kml",
|
||||||
|
|||||||
Reference in New Issue
Block a user