encode/decode -> b58_encode/decode
parent
2f4f307b4d
commit
fa957794e5
|
@ -1,4 +1,4 @@
|
|||
use crate::util::hash::{decode, encode, Hash, Hashable};
|
||||
use crate::util::hash::{b58_decode, b58_encode, Hash, Hashable};
|
||||
use anyhow::{anyhow, Result};
|
||||
use serde::de::Visitor;
|
||||
use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
@ -73,7 +73,7 @@ impl Serialize for Address {
|
|||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(encode(self.encode().map_err(ser::Error::custom)?).as_str())
|
||||
serializer.serialize_str(b58_encode(self.encode().map_err(ser::Error::custom)?).as_str())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -90,7 +90,7 @@ impl<'de> Visitor<'de> for AddressVisitor {
|
|||
where
|
||||
E: de::Error,
|
||||
{
|
||||
let bytes = decode(str).map_err(de::Error::custom)?;
|
||||
let bytes = b58_decode(str).map_err(de::Error::custom)?;
|
||||
Address::decode(bytes.as_ref()).map_err(de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
@ -108,13 +108,13 @@ impl FromStr for Address {
|
|||
type Err = anyhow::Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Address::decode(decode(s)?.as_ref())
|
||||
Address::decode(b58_decode(s)?.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Address {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", encode(self.encode().map_err(|_| std::fmt::Error)?))
|
||||
write!(f, "{}", b58_encode(self.encode().map_err(|_| std::fmt::Error)?))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::addressing::{Address, Addressable};
|
||||
use crate::database::inner::models;
|
||||
use crate::util::hash::{decode, hash, Hash, Hashable};
|
||||
use crate::util::hash::{b58_decode, hash, Hash, Hashable};
|
||||
use anyhow::{anyhow, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::convert::TryFrom;
|
||||
|
@ -184,7 +184,7 @@ impl std::str::FromStr for EntryValue {
|
|||
}
|
||||
}
|
||||
("O", content) => {
|
||||
if let Ok(addr) = decode(content).and_then(|v| Address::decode(&v)) {
|
||||
if let Ok(addr) = b58_decode(content).and_then(|v| Address::decode(&v)) {
|
||||
Ok(EntryValue::Address(addr))
|
||||
} else {
|
||||
Ok(EntryValue::Invalid)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::util::hash::Hash;
|
||||
use crate::{database::UpEndDatabase, util::hash::encode};
|
||||
use crate::{database::UpEndDatabase, util::hash::b58_encode};
|
||||
use anyhow::{anyhow, Result};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
|
@ -42,7 +42,7 @@ impl PreviewStore {
|
|||
if let Some(path) = locks.get(hash) {
|
||||
path.clone()
|
||||
} else {
|
||||
let thumbpath = self.path.join(encode(hash));
|
||||
let thumbpath = self.path.join(b58_encode(hash));
|
||||
let path = Arc::new(Mutex::new(thumbpath));
|
||||
locks.insert(hash.clone(), path.clone());
|
||||
path
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::database::lang::Query;
|
|||
use crate::database::UpEndDatabase;
|
||||
use crate::filesystem::add_file;
|
||||
use crate::previews::PreviewStore;
|
||||
use crate::util::hash::{decode, encode, Hashable};
|
||||
use crate::util::hash::{b58_decode, b58_encode, Hashable};
|
||||
use crate::util::jobs::JobContainer;
|
||||
use actix_files::NamedFile;
|
||||
use actix_multipart::Multipart;
|
||||
|
@ -50,7 +50,7 @@ pub async fn get_raw(
|
|||
web::Query(query): web::Query<RawRequest>,
|
||||
hash: web::Path<String>,
|
||||
) -> Result<Either<NamedFile, HttpResponse>, Error> {
|
||||
let address = Address::decode(&decode(hash.into_inner()).map_err(ErrorInternalServerError)?)
|
||||
let address = Address::decode(&b58_decode(hash.into_inner()).map_err(ErrorInternalServerError)?)
|
||||
.map_err(ErrorInternalServerError)?;
|
||||
if let Address::Hash(hash) = address {
|
||||
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
|
||||
|
@ -135,7 +135,7 @@ pub async fn get_query(
|
|||
let mut result: HashMap<String, Entry> = HashMap::new();
|
||||
for entry in entries {
|
||||
result.insert(
|
||||
encode(
|
||||
b58_encode(
|
||||
entry
|
||||
.address()
|
||||
.map_err(ErrorInternalServerError)?
|
||||
|
@ -157,7 +157,7 @@ impl EntriesAsHash for Vec<Entry> {
|
|||
fn as_hash(&self) -> Result<HashMap<String, &Entry>> {
|
||||
let mut result: HashMap<String, &Entry> = HashMap::new();
|
||||
for entry in self {
|
||||
result.insert(encode(entry.address()?.encode()?), entry);
|
||||
result.insert(b58_encode(entry.address()?.encode()?), entry);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
|
@ -172,7 +172,7 @@ pub async fn get_object(
|
|||
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
|
||||
let result: Vec<Entry> = connection
|
||||
.retrieve_object(
|
||||
Address::decode(&decode(address_str.into_inner()).map_err(ErrorBadRequest)?)
|
||||
Address::decode(&b58_decode(address_str.into_inner()).map_err(ErrorBadRequest)?)
|
||||
.map_err(ErrorBadRequest)?,
|
||||
)
|
||||
.map_err(ErrorInternalServerError)?;
|
||||
|
@ -223,7 +223,7 @@ pub async fn put_object(
|
|||
.map_err(ErrorInternalServerError)?;
|
||||
|
||||
if existing_files.is_empty() {
|
||||
let addr_str = encode(address.encode().map_err(ErrorInternalServerError)?);
|
||||
let addr_str = b58_encode(address.encode().map_err(ErrorInternalServerError)?);
|
||||
let final_name = if let Some(filename) = filename {
|
||||
format!("{addr_str}_{filename}")
|
||||
} else {
|
||||
|
@ -256,7 +256,7 @@ pub async fn put_object(
|
|||
|
||||
Ok(HttpResponse::Ok().json(
|
||||
[(
|
||||
encode(result_address.encode().map_err(ErrorInternalServerError)?),
|
||||
b58_encode(result_address.encode().map_err(ErrorInternalServerError)?),
|
||||
entry,
|
||||
)]
|
||||
.iter()
|
||||
|
@ -273,7 +273,7 @@ pub async fn delete_object(
|
|||
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
|
||||
let _ = connection
|
||||
.remove_object(
|
||||
Address::decode(&decode(address_str.into_inner()).map_err(ErrorBadRequest)?)
|
||||
Address::decode(&b58_decode(address_str.into_inner()).map_err(ErrorBadRequest)?)
|
||||
.map_err(ErrorInternalServerError)?,
|
||||
)
|
||||
.map_err(ErrorInternalServerError)?;
|
||||
|
@ -370,7 +370,7 @@ pub async fn get_file(
|
|||
state: web::Data<State>,
|
||||
hash: web::Path<String>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let address = Address::decode(&decode(hash.into_inner()).map_err(ErrorInternalServerError)?)
|
||||
let address = Address::decode(&b58_decode(hash.into_inner()).map_err(ErrorInternalServerError)?)
|
||||
.map_err(ErrorInternalServerError)?;
|
||||
|
||||
if let Address::Hash(hash) = address {
|
||||
|
@ -417,7 +417,7 @@ pub async fn get_thumbnail(
|
|||
#[cfg(feature = "previews")]
|
||||
if let Some(preview_store) = &state.preview_store {
|
||||
let hash = hash.into_inner();
|
||||
let address = Address::decode(&decode(&hash).map_err(ErrorInternalServerError)?)
|
||||
let address = Address::decode(&b58_decode(&hash).map_err(ErrorInternalServerError)?)
|
||||
.map_err(ErrorInternalServerError)?;
|
||||
if let Address::Hash(address_hash) = address {
|
||||
let preview_result = preview_store
|
||||
|
|
|
@ -32,7 +32,7 @@ impl Serialize for Hash {
|
|||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(
|
||||
encode(
|
||||
b58_encode(
|
||||
Address::Hash(self.clone())
|
||||
.encode()
|
||||
.map_err(ser::Error::custom)?,
|
||||
|
@ -84,12 +84,12 @@ pub fn hash<T: AsRef<[u8]>>(input: T) -> Hash {
|
|||
Hash(Vec::from(result))
|
||||
}
|
||||
|
||||
pub fn encode<T: AsRef<[u8]>>(vec: T) -> String {
|
||||
pub fn b58_encode<T: AsRef<[u8]>>(vec: T) -> String {
|
||||
// multibase base58
|
||||
format!("z{}", bs58::encode(vec).into_string())
|
||||
}
|
||||
|
||||
pub fn decode<T: AsRef<str>>(string: T) -> Result<Vec<u8>> {
|
||||
pub fn b58_decode<T: AsRef<str>>(string: T) -> Result<Vec<u8>> {
|
||||
let string = string.as_ref();
|
||||
let (base, data) = string.split_at(1);
|
||||
|
||||
|
@ -106,14 +106,14 @@ pub fn decode<T: AsRef<str>>(string: T) -> Result<Vec<u8>> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::util::hash::{decode, encode};
|
||||
use crate::util::hash::{b58_decode, b58_encode};
|
||||
|
||||
#[test]
|
||||
fn test_encode_decode() {
|
||||
let content = "Hello, World!".as_bytes();
|
||||
|
||||
let encoded = encode(content);
|
||||
let decoded = decode(encoded);
|
||||
let encoded = b58_encode(content);
|
||||
let decoded = b58_decode(encoded);
|
||||
|
||||
assert!(decoded.is_ok());
|
||||
|
||||
|
|
Loading…
Reference in New Issue