-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
235 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::collections::HashMap; | ||
use std::hash::{DefaultHasher, Hash, Hasher}; | ||
|
||
use anyhow::Result; | ||
|
||
use super::metadata_service::MetadataService; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct FakeMetadataService { | ||
data: HashMap<String, Vec<u8>>, | ||
} | ||
|
||
#[allow(async_fn_in_trait)] | ||
impl MetadataService for FakeMetadataService { | ||
async fn upload(&mut self, data: Vec<u8>) -> Result<String> { | ||
let mut hasher = DefaultHasher::new(); | ||
data.hash(&mut hasher); | ||
let hash = hasher.finish(); | ||
|
||
let uri = format!("ipfs://{:x}", hash); | ||
self.data.insert(uri.clone(), data); | ||
|
||
Ok(uri) | ||
} | ||
|
||
#[cfg(test)] | ||
async fn get(&self, uri: String) -> Result<Vec<u8>> { | ||
Ok(self.data.get(&uri).cloned().unwrap_or(Vec::<u8>::new())) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::io::Cursor; | ||
|
||
use anyhow::Result; | ||
#[cfg(test)] | ||
use futures::TryStreamExt; | ||
use ipfs_api_backend_hyper::{IpfsApi, TryFromUri}; | ||
|
||
use super::metadata_service::MetadataService; | ||
|
||
pub struct IpfsMetadataService { | ||
client: ipfs_api_backend_hyper::IpfsClient, | ||
} | ||
|
||
// impl required by clippy | ||
impl std::fmt::Debug for IpfsMetadataService { | ||
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl IpfsMetadataService { | ||
pub fn new(client_url: &str, username: &str, password: &str) -> Result<Self> { | ||
Ok(Self { | ||
client: ipfs_api_backend_hyper::IpfsClient::from_str(client_url)? | ||
.with_credentials(username, password), | ||
}) | ||
} | ||
} | ||
|
||
#[allow(async_fn_in_trait)] | ||
impl MetadataService for IpfsMetadataService { | ||
async fn upload(&mut self, data: Vec<u8>) -> Result<String> { | ||
let reader = Cursor::new(data); | ||
let response = self.client.add(reader).await?; | ||
Ok(format!("ipfs://{}", response.hash)) | ||
} | ||
|
||
#[cfg(test)] | ||
async fn get(&self, uri: String) -> Result<Vec<u8>> { | ||
let res = self | ||
.client | ||
.cat(&uri.replace("ipfs://", "")) | ||
.map_ok(|chunk| chunk.to_vec()) | ||
.try_concat() | ||
.await?; | ||
Ok(res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use anyhow::Result; | ||
|
||
#[allow(async_fn_in_trait)] | ||
pub trait MetadataService: std::marker::Send + std::marker::Sync + std::marker::Unpin { | ||
async fn upload(&mut self, data: Vec<u8>) -> Result<String>; | ||
|
||
#[cfg(test)] | ||
async fn get(&self, uri: String) -> Result<Vec<u8>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::hash::{DefaultHasher, Hash, Hasher}; | ||
|
||
use anyhow::Result; | ||
use serde_json::json; | ||
use starknet_crypto::Felt; | ||
|
||
use super::metadata_service::MetadataService; | ||
use crate::config::metadata_config::{ResourceMetadata, WorldMetadata}; | ||
use crate::uri::Uri; | ||
|
||
/// Helper function to compute metadata hash using the Hash trait impl. | ||
fn compute_metadata_hash<T>(data: T) -> u64 | ||
where | ||
T: Hash, | ||
{ | ||
let mut hasher = DefaultHasher::new(); | ||
data.hash(&mut hasher); | ||
hasher.finish() | ||
} | ||
|
||
async fn upload_uri(uri: &Option<Uri>, service: &mut impl MetadataService) -> Result<Option<Uri>> { | ||
if let Some(Uri::File(path)) = uri { | ||
let data = std::fs::read(path)?; | ||
let uploaded_uri = Uri::Ipfs(service.upload(data).await?); | ||
Ok(Some(uploaded_uri)) | ||
} else { | ||
Ok(uri.clone()) | ||
} | ||
} | ||
|
||
#[allow(async_fn_in_trait)] | ||
pub trait MetadataStorage { | ||
async fn upload(&self, service: &mut impl MetadataService) -> Result<String>; | ||
|
||
async fn upload_if_changed( | ||
&self, | ||
service: &mut impl MetadataService, | ||
current_hash: Felt, | ||
) -> Result<Option<(String, Felt)>> | ||
where | ||
Self: std::hash::Hash, | ||
{ | ||
let new_hash = compute_metadata_hash(self); | ||
let new_hash = Felt::from_raw([0, 0, 0, new_hash]); | ||
|
||
if new_hash != current_hash { | ||
let new_uri = self.upload(service).await?; | ||
return Ok(Some((new_uri, new_hash))); | ||
} | ||
|
||
Ok(None) | ||
} | ||
} | ||
|
||
#[allow(async_fn_in_trait)] | ||
impl MetadataStorage for WorldMetadata { | ||
async fn upload(&self, service: &mut impl MetadataService) -> Result<String> { | ||
let mut meta = self.clone(); | ||
|
||
meta.icon_uri = upload_uri(&self.icon_uri, service).await?; | ||
meta.cover_uri = upload_uri(&self.cover_uri, service).await?; | ||
|
||
let serialized = json!(meta).to_string(); | ||
service.upload(serialized.as_bytes().to_vec()).await | ||
} | ||
} | ||
|
||
#[allow(async_fn_in_trait)] | ||
impl MetadataStorage for ResourceMetadata { | ||
async fn upload(&self, service: &mut impl MetadataService) -> Result<String> { | ||
let mut meta = self.clone(); | ||
|
||
meta.icon_uri = upload_uri(&self.icon_uri, service).await?; | ||
|
||
let serialized = json!(meta).to_string(); | ||
service.upload(serialized.as_bytes().to_vec()).await | ||
} | ||
} |
Oops, something went wrong.