preparations for a separate attr update endpoint

feat/vaults
Tomáš Mládek 2022-01-09 21:18:19 +01:00
parent 052403bc05
commit 8ceb310d9e
No known key found for this signature in database
GPG Key ID: ED21612889E75EC5
1 changed files with 45 additions and 14 deletions

View File

@ -10,6 +10,7 @@ use actix_files::NamedFile;
use actix_web::error::{ErrorBadRequest, ErrorInternalServerError, ErrorNotFound};
use actix_web::http;
use actix_web::{delete, error, get, post, put, web, Either, Error, HttpResponse};
use anyhow::anyhow;
use anyhow::Result;
use futures_util::StreamExt;
use log::{debug, info, trace};
@ -164,28 +165,19 @@ pub async fn get_object(
Ok(HttpResponse::Ok().json(result.as_hash().map_err(ErrorInternalServerError)?))
}
const MAX_SIZE: usize = 1_000_000;
#[put("/api/obj")]
pub async fn put_object(
state: web::Data<State>,
mut payload: web::Payload,
) -> Result<HttpResponse, Error> {
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
let mut body = web::BytesMut::new();
while let Some(chunk) = payload.next().await {
let chunk = chunk?;
// limit max size of in-memory payload
if (body.len() + chunk.len()) > MAX_SIZE {
return Err(error::ErrorBadRequest("overflow."));
}
body.extend_from_slice(&chunk);
}
let body = load_body(&mut payload)
.await
.map_err(error::ErrorBadRequest)?;
let in_entry = serde_json::from_slice::<InEntry>(&body).map_err(ErrorBadRequest)?;
let entry = Entry::try_from(in_entry).map_err(ErrorInternalServerError)?;
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
let result_address = connection
.insert_entry(entry.clone())
.map_err(ErrorInternalServerError)?;
@ -217,6 +209,31 @@ pub async fn delete_object(
Ok(HttpResponse::Ok().finish())
}
// #[post("api/obj/{address_str}")]
// pub async fn update_attribute(
// state: web::Data<State>,
// address_str: web::Path<String>,
// mut payload: web::Payload,
// ) -> Result<HttpResponse, Error> {
// let body = load_body(&mut payload)
// .await
// .map_err(error::ErrorBadRequest)?;
// let entry_value = serde_json::from_slice::<EntryValue>(&body).map_err(ErrorBadRequest)?;
// let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
// connection
// .transaction::<_, anyhow::Error, _>(|| {
// let address = Address::decode(&decode(address_str.into_inner())?)?;
// let _ = connection.remove_object(address)?;
// Ok(())
// })
// .map_err(ErrorInternalServerError)?;
// Ok(HttpResponse::Ok().finish())
// }
#[get("/api/all/attributes")]
pub async fn get_all_attributes(state: web::Data<State>) -> Result<HttpResponse, Error> {
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
@ -349,3 +366,17 @@ pub async fn get_thumbnail(
}
Err(error::ErrorNotImplemented("Previews not enabled."))
}
const MAX_BODY_SIZE: usize = 1_000_000;
async fn load_body(payload: &mut web::Payload) -> Result<Vec<u8>> {
let mut body = web::BytesMut::new();
while let Some(chunk) = payload.next().await {
let chunk = chunk?;
if (body.len() + chunk.len()) > MAX_BODY_SIZE {
return Err(anyhow!("OVERFLOW"));
}
body.extend_from_slice(&chunk);
}
Ok(body.as_ref().to_vec())
}