radicle: Move DataUri to common::DataUri

This commit is contained in:
Sebastian Martinez
2023-12-22 15:09:20 +01:00
parent 2cbfb6dd53
commit f0edd11e58
+3 -48
View File
@@ -9,19 +9,18 @@ use axum::http::Method;
use axum::response::{IntoResponse, Json};
use axum::routing::get;
use axum::Router;
use base64::prelude::{Engine, BASE64_STANDARD};
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::sync::RwLock;
use tower_http::cors::{self, CorsLayer};
use radicle::cob::{issue, Uri};
use radicle::cob::{patch, Embed};
use radicle::cob::issue;
use radicle::cob::patch;
use radicle::identity::{DocAt, Id};
use radicle::node::policy::Scope;
use radicle::node::routing::Store;
use radicle::node::{Handle, NodeId};
use radicle::storage::{Oid, ReadRepository, ReadStorage};
use radicle::storage::{ReadRepository, ReadStorage};
use radicle::{Node, Profile};
mod error;
@@ -217,32 +216,6 @@ mod project {
}
}
/// A `data:` URI.
#[derive(Debug, Clone)]
pub struct DataUri(Vec<u8>);
impl From<DataUri> for Vec<u8> {
fn from(value: DataUri) -> Self {
value.0
}
}
impl TryFrom<&Uri> for DataUri {
type Error = Uri;
fn try_from(value: &Uri) -> Result<Self, Self::Error> {
if let Some(data_uri) = value.as_str().strip_prefix("data:") {
let (_, uri_data) = data_uri.split_once(',').ok_or(value.clone())?;
let uri_data = BASE64_STANDARD
.decode(uri_data)
.map_err(|_| value.clone())?;
return Ok(DataUri(uri_data));
}
Err(value.clone())
}
}
/// Announce refs to the network for the given RID.
pub fn announce_refs(mut node: Node, rid: Id) -> Result<(), Error> {
match node.announce_refs(rid) {
@@ -251,21 +224,3 @@ pub fn announce_refs(mut node: Node, rid: Id) -> Result<(), Error> {
Err(e) => Err(e.into()),
}
}
/// Resolve an embed with a URI to one with actual data.
pub fn resolve_embed(repo: &impl ReadRepository, embed: Embed<Uri>) -> Option<Embed<Vec<u8>>> {
DataUri::try_from(&embed.content)
.ok()
.map(|content| Embed {
name: embed.name.clone(),
content: content.into(),
})
.or_else(|| {
Oid::try_from(&embed.content).ok().and_then(|oid| {
repo.blob(oid).ok().map(|blob| Embed {
name: embed.name,
content: blob.content().to_vec(),
})
})
})
}