httpd: Parse git URIs into Embed

Currently we only handle data Uris, but when a user sends a comment that
has a existing git Oid, due to editing a comment we also need to handle
this case.

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2023-10-30 15:41:34 +01:00
committed by cloudhead
parent 472f38bacf
commit c1a026fc81
3 changed files with 72 additions and 59 deletions
+20 -2
View File
@@ -15,11 +15,11 @@ use serde_json::json;
use tokio::sync::RwLock;
use tower_http::cors::{self, CorsLayer};
use radicle::cob::patch;
use radicle::cob::{issue, Uri};
use radicle::cob::{patch, Embed};
use radicle::identity::{DocAt, Id};
use radicle::node::routing::Store;
use radicle::storage::{ReadRepository, ReadStorage};
use radicle::storage::{Oid, ReadRepository, ReadStorage};
use radicle::Profile;
mod error;
@@ -228,3 +228,21 @@ impl TryFrom<&Uri> for DataUri {
Err(value.clone())
}
}
/// 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(),
})
})
})
}