Add group image endpoint

This commit is contained in:
Niels Andriesse 2021-03-29 16:11:56 +11:00
parent eeed203282
commit 192e3b4ac1
2 changed files with 40 additions and 0 deletions

View File

@ -200,6 +200,35 @@ pub async fn get_file(
return Ok(json);
}
pub async fn get_group_image(
room_id: &str, auth_token: &str, pool: &storage::DatabaseConnectionPool,
) -> Result<Response, Rejection> {
// Check authorization level
let (has_authorization_level, _) =
has_authorization_level(auth_token, AuthorizationLevel::Basic, pool).await?;
if !has_authorization_level {
return Err(warp::reject::custom(Error::Unauthorized));
}
// Try to read the file
let raw_path = format!("files/{}", room_id);
let path = Path::new(&raw_path);
let bytes = match fs::read(path) {
Ok(bytes) => bytes,
Err(e) => {
println!("Couldn't read file due to error: {}.", e);
return Err(warp::reject::custom(Error::ValidationFailed));
}
};
// Base64 encode the result
let base64_encoded_bytes = base64::encode(bytes);
// Return
let json = GenericStringResponse {
status_code: StatusCode::OK.as_u16(),
result: base64_encoded_bytes,
};
return Ok(warp::reply::json(&json).into_response());
}
// Authentication
pub async fn get_auth_token_challenge(

View File

@ -114,6 +114,17 @@ async fn handle_get_request(
.map(|json| warp::reply::json(&json).into_response());
}
match path {
"group_image" => {
reject_if_file_server_mode(path)?;
let room_id = match get_room_id(&rpc_call) {
Some(room_id) => room_id,
None => {
println!("Missing room ID.");
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
return handlers::get_group_image(&room_id, &auth_token, &pool).await;
}
"messages" => {
reject_if_file_server_mode(path)?;
return handlers::get_messages(query_params, &auth_token, &pool).await;