Have Media db return optional content_type, conversion fixes

next
Devin Ragotzy 2020-11-18 08:36:12 -05:00 committed by Timo Kösters
parent bb24f6ad90
commit b6d721374f
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
5 changed files with 47 additions and 31 deletions

View File

@ -66,7 +66,7 @@ pub async fn get_content_route(
{ {
Ok(get_content::Response { Ok(get_content::Response {
file, file,
content_type: Some(content_type), content_type,
content_disposition: filename, content_disposition: filename,
} }
.into()) .into())
@ -116,11 +116,7 @@ pub async fn get_content_thumbnail_route(
.try_into() .try_into()
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
)? { )? {
Ok(get_content_thumbnail::Response { Ok(get_content_thumbnail::Response { file, content_type }.into())
file,
content_type: Some(content_type),
}
.into())
} else if &*body.server_name != db.globals.server_name() && body.allow_remote { } else if &*body.server_name != db.globals.server_name() && body.allow_remote {
let get_thumbnail_response = server_server::send_request( let get_thumbnail_response = server_server::send_request(
&db.globals, &db.globals,

View File

@ -510,8 +510,7 @@ async fn join_room_by_id_helper(
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
); );
// TODO fixup CanonicalJsonValue // Convert `serde_json;:Value` to `CanonicalJsonObj` for hashing/signing
// use that instead of serde_json::Map... maybe?
let mut canon_json_stub: BTreeMap<_, ruma::signatures::CanonicalJsonValue> = let mut canon_json_stub: BTreeMap<_, ruma::signatures::CanonicalJsonValue> =
serde_json::from_value(join_event_stub_value).expect("json Value is canonical JSON"); serde_json::from_value(join_event_stub_value).expect("json Value is canonical JSON");

View File

@ -5,7 +5,7 @@ use std::mem;
pub struct FileMeta { pub struct FileMeta {
pub filename: Option<String>, pub filename: Option<String>,
pub content_type: String, pub content_type: Option<String>,
pub file: Vec<u8>, pub file: Vec<u8>,
} }
@ -83,12 +83,14 @@ impl Media {
let (key, file) = r?; let (key, file) = r?;
let mut parts = key.rsplit(|&b| b == 0xff); let mut parts = key.rsplit(|&b| b == 0xff);
let content_type = utils::string_from_bytes( let content_type = parts
parts
.next() .next()
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?, .map(|bytes| {
) Ok::<_, Error>(utils::string_from_bytes(bytes).map_err(|_| {
.map_err(|_| Error::bad_database("Content type in mediaid_file is invalid unicode."))?; Error::bad_database("Content type in mediaid_file is invalid unicode.")
})?)
})
.transpose()?;
let filename_bytes = parts let filename_bytes = parts
.next() .next()
@ -158,12 +160,14 @@ impl Media {
let (key, file) = r?; let (key, file) = r?;
let mut parts = key.rsplit(|&b| b == 0xff); let mut parts = key.rsplit(|&b| b == 0xff);
let content_type = utils::string_from_bytes( let content_type = parts
parts
.next() .next()
.ok_or_else(|| Error::bad_database("Invalid Media ID in db"))?, .map(|bytes| {
) Ok::<_, Error>(utils::string_from_bytes(bytes).map_err(|_| {
.map_err(|_| Error::bad_database("Content type in mediaid_file is invalid unicode."))?; Error::bad_database("Content type in mediaid_file is invalid unicode.")
})?)
})
.transpose()?;
let filename_bytes = parts let filename_bytes = parts
.next() .next()
@ -189,12 +193,14 @@ impl Media {
let (key, file) = r?; let (key, file) = r?;
let mut parts = key.rsplit(|&b| b == 0xff); let mut parts = key.rsplit(|&b| b == 0xff);
let content_type = utils::string_from_bytes( let content_type = parts
parts
.next() .next()
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?, .map(|bytes| {
) Ok::<_, Error>(utils::string_from_bytes(bytes).map_err(|_| {
.map_err(|_| Error::bad_database("Content type in mediaid_file is invalid unicode."))?; Error::bad_database("Content type in mediaid_file is invalid unicode.")
})?)
})
.transpose()?;
let filename_bytes = parts let filename_bytes = parts
.next() .next()

View File

@ -499,7 +499,6 @@ impl Rooms {
Ok(()) Ok(())
} }
#[allow(clippy::too_many_arguments)]
/// Creates a new persisted data unit and adds it to a room. /// Creates a new persisted data unit and adds it to a room.
/// ///
/// By this point the incoming event should be fully authenticated, no auth happens /// By this point the incoming event should be fully authenticated, no auth happens
@ -856,9 +855,8 @@ impl Rooms {
}; };
// Hash and sign // Hash and sign
let mut pdu_json: BTreeMap<String, ruma::serde::CanonicalJsonValue> = let mut pdu_json =
serde_json::from_value(serde_json::json!(&pdu)) utils::to_canonical_object(&pdu).expect("event is valid, we just created it");
.expect("event is valid, we just created it");
pdu_json.remove("event_id"); pdu_json.remove("event_id");

View File

@ -1,6 +1,7 @@
use argon2::{Config, Variant}; use argon2::{Config, Variant};
use cmp::Ordering; use cmp::Ordering;
use rand::prelude::*; use rand::prelude::*;
use ruma::serde::{try_from_json_map, CanonicalJsonError, CanonicalJsonObject};
use sled::IVec; use sled::IVec;
use std::{ use std::{
cmp, cmp,
@ -94,3 +95,19 @@ pub fn common_elements(
.all(|b| b) .all(|b| b)
})) }))
} }
/// Fallible conversion from any value that implements `Serialize` to a `CanonicalJsonObject`.
///
/// `value` must serialize to an `serde_json::Value::Object`.
pub fn to_canonical_object<T: serde::Serialize>(
value: T,
) -> Result<CanonicalJsonObject, CanonicalJsonError> {
use serde::ser::Error;
match serde_json::to_value(value).map_err(CanonicalJsonError::SerDe)? {
serde_json::Value::Object(map) => try_from_json_map(map),
_ => Err(CanonicalJsonError::SerDe(serde_json::Error::custom(
"Value must be an object",
))),
}
}