/raw/ endpoint does not take plain hash but an encode, add Entry.as_json()

feat/vaults
Tomáš Mládek 2020-09-13 14:28:58 +02:00
parent b245b50e15
commit 267bca92bf
2 changed files with 45 additions and 42 deletions

View File

@ -1,8 +1,6 @@
use std::convert::TryFrom;
use std::io::{Cursor, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;
use crate::addressing::Address;
use crate::hash::{decode, hash, Hash, Hashable};
use crate::models;
use actix::prelude::*;
use actix_derive::Message;
use anyhow::{anyhow, Result};
@ -10,12 +8,13 @@ use diesel::prelude::*;
use diesel::r2d2::{self, ConnectionManager};
use diesel::sqlite::SqliteConnection;
use log::debug;
use crate::addressing::Address;
use crate::hash::{decode, encode, hash, Hash, Hashable};
use crate::models;
use serde::export::Formatter;
use serde_json::json;
use std::convert::TryFrom;
use std::fs;
use std::io::{Cursor, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct Entry {
@ -52,6 +51,20 @@ impl TryFrom<models::Entry> for Entry {
}
}
impl Entry {
pub fn as_json(&self) -> serde_json::Value {
json!({
"target": self.target.to_string(),
"key": self.key,
"value": match &self.value {
EntryValue::Value(value) => ("VALUE", value.clone()),
EntryValue::Address(address) => ("ADDR", json!(address.to_string())),
EntryValue::Invalid => ("INVALID", json!("INVALID")),
}
})
}
}
impl std::fmt::Display for InnerEntry {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} | {} | {}", self.target, self.key, self.value)
@ -178,9 +191,8 @@ impl Handler<InsertFile> for DbExecutor {
let connection = &self.0.get()?;
debug!(
"Inserting {} ({}) @ {}...",
"Inserting {} ({})...",
&msg.file.path,
encode(&msg.file.hash),
Address::Hash(Hash((&msg.file.hash).clone()))
);

View File

@ -1,14 +1,13 @@
use crate::addressing::Address;
use crate::database::{Entry, EntryValue};
use crate::hash::{decode, encode, Hash};
use crate::database::Entry;
use crate::hash::{decode, encode};
use actix::prelude::*;
use actix_files::NamedFile;
use actix_web::error::ErrorInternalServerError;
use actix_web::error::{ErrorBadRequest, ErrorInternalServerError};
use actix_web::{error, get, post, web, Error, HttpResponse};
use anyhow::Result;
use log::debug;
use serde::Deserialize;
use serde_json::json;
use std::collections::HashMap;
use std::path::PathBuf;
@ -21,21 +20,25 @@ pub struct State {
#[get("/raw/{hash}")]
pub async fn get_raw(state: web::Data<State>, hash: web::Path<String>) -> Result<NamedFile, Error> {
let response = state
.db
.send(crate::database::RetrieveByHash {
hash: Hash(decode(hash.into_inner()).map_err(error::ErrorInternalServerError)?),
})
.await?;
let address = Address::decode(&decode(hash.into_inner()).map_err(ErrorInternalServerError)?)
.map_err(ErrorInternalServerError)?;
if let Address::Hash(hash) = address {
let response = state
.db
.send(crate::database::RetrieveByHash { hash })
.await?;
debug!("{:?}", response);
debug!("{:?}", response);
match response {
Ok(result) => match result {
Some(path) => Ok(NamedFile::open(path)?),
None => Err(error::ErrorNotFound("NOT FOUND")),
},
Err(e) => Err(error::ErrorInternalServerError(e)),
match response {
Ok(result) => match result {
Some(path) => Ok(NamedFile::open(path)?),
None => Err(error::ErrorNotFound("NOT FOUND")),
},
Err(e) => Err(error::ErrorInternalServerError(e)),
}
} else {
Err(ErrorBadRequest("Address does not refer to a file."))
}
}
@ -56,21 +59,9 @@ pub async fn get_object(
debug!("{:?}", response);
let entries = response.map_err(error::ErrorInternalServerError)?;
let mut result: HashMap<String, serde_json::Value> = HashMap::new();
for entry in entries {
result.insert(
encode(entry.identity.0),
json!({
"target": entry.target.to_string(),
"key": entry.key,
"value": match entry.value {
EntryValue::Value(value) => ("VALUE", value),
EntryValue::Address(address) => ("ADDR", json!(address.to_string())),
EntryValue::Invalid => ("INVALID", json!("INVALID")),
}
}),
);
for entry in response.map_err(error::ErrorInternalServerError)? {
result.insert(encode(&entry.identity.0), entry.as_json());
}
Ok(HttpResponse::Ok().json(result))
}