Skip to content

Commit

Permalink
fix: file manager
Browse files Browse the repository at this point in the history
  • Loading branch information
remiroyc committed Sep 19, 2023
1 parent d2b7a60 commit 1b00907
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
tmp/
.env
.aws
.aws
35 changes: 19 additions & 16 deletions crates/ark-metadata/src/file_manager.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fs::File;
use std::fs::{File, create_dir_all};
use std::io::prelude::*;
use std::path::Path;
use anyhow::Result;

use anyhow::{Result, Context};
use async_trait::async_trait;
use aws_sdk_s3 as s3;
use log::info;

#[cfg(any(test, feature = "mock"))]
use mockall::automock;
Expand All @@ -19,25 +20,27 @@ pub trait FileManager {
async fn save(&self, file: &FileInfo) -> Result<()>;
}

#[derive(Default)]
pub struct LocalFileManager;

impl LocalFileManager {
pub fn new() -> Self {
Self
}
}

#[async_trait]
impl FileManager for LocalFileManager {

async fn save(&self, file: &FileInfo) -> Result<()> {
let path = Path::new("./images/").join(&file.name);
let mut dest_file = File::create(&path)?;

// Écrire le contenu dans le fichier
dest_file.write_all(&file.content)?;
// Construct the path
let path = Path::new("./tmp").join(&file.name);

// Ensure directory exists
create_dir_all(path.parent().unwrap())
.context("Failed to create directory")?;

// Create and write to the file
let mut dest_file = File::create(&path)
.context("Failed to create file")?;

dest_file.write_all(&file.content)
.context("Failed to write to file")?;

println!("Saving {} to local disk...", file.name);
info!("File saved: {}", file.name);
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ark-metadata/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn main() -> Result<()> {
dotenv().ok();

let storage = DefaultStorage::new();
let file_manager = LocalFileManager::new();
let file_manager = LocalFileManager::default();

let starknet_client =
StarknetClientHttp::new("https://starknode.thearkproject.dev/mainnet").unwrap();
Expand All @@ -24,7 +24,7 @@ async fn main() -> Result<()> {
let token_id_low = FieldElement::from_dec_str("1").unwrap();
let token_id_high = FieldElement::from_dec_str("0").unwrap();
let force_refresh = Some(true);
let cache_image = Some(false);
let cache_image = Some(true);

match metadata_manager
.refresh_token_metadata(
Expand Down
16 changes: 8 additions & 8 deletions crates/ark-metadata/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use base64::{engine::general_purpose, Engine as _};
use reqwest::Client;
use urlencoding;

use crate::file_manager::{FileManager, FileInfo};


#[derive(Debug, PartialEq)]
pub enum MetadataType<'a> {
Http(&'a str),
Expand All @@ -24,11 +21,14 @@ pub struct MetadataImage {
pub async fn get_token_metadata(client: &Client, uri: &str) -> Result<TokenMetadata> {
let metadata_type = get_metadata_type(uri);
println!("Metadata type: {:?}", metadata_type);
match metadata_type {
MetadataType::Ipfs(uri) => Ok(get_ipfs_metadata(uri, &client).await?),
MetadataType::Http(uri) => Ok(get_http_metadata(uri, &client).await?),
MetadataType::OnChain(uri) => Ok(get_onchain_metadata(uri)?),
}

let metadata = match metadata_type {
MetadataType::Ipfs(uri) => get_ipfs_metadata(uri, &client).await?,
MetadataType::Http(uri) => get_http_metadata(uri, &client).await?,
MetadataType::OnChain(uri) => get_onchain_metadata(uri)?,
};

Ok(metadata)
}

pub fn get_metadata_type(uri: &str) -> MetadataType<'_> {
Expand Down
27 changes: 23 additions & 4 deletions crates/ark-metadata/src/metadata_manager.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::env;

use crate::cairo_string_parser::parse_cairo_long_string;
use crate::file_manager::{FileInfo, FileManager};
use crate::metadata::{get_token_metadata, MetadataImage};
Expand Down Expand Up @@ -86,7 +88,13 @@ impl<'a, T: StorageManager, C: StarknetClient, F: FileManager> MetadataManager<'
.map_err(|_| MetadataError::RequestTokenUriError)?;

if token_metadata.image.is_some() {
let url = token_metadata.image.clone().unwrap();
let ipfs_url = env::var("IPFS_GATEWAY_URI").expect("IPFS_GATEWAY_URI must be set");

let url = token_metadata
.image
.clone()
.unwrap()
.replace("ipfs://", ipfs_url.as_str());
let image_name = url.split("/").last().unwrap();

self.fetch_token_image(url.as_str(), image_name, cache_image.unwrap_or(false))
Expand All @@ -95,7 +103,14 @@ impl<'a, T: StorageManager, C: StarknetClient, F: FileManager> MetadataManager<'
}

self.storage
.register_token_metadata(&contract_address, TokenId { low: token_id_low, high: token_id_high }, token_metadata)
.register_token_metadata(
&contract_address,
TokenId {
low: token_id_low,
high: token_id_high,
},
token_metadata,
)
.map_err(|_e| MetadataError::DatabaseError)?;

Ok(())
Expand Down Expand Up @@ -127,7 +142,12 @@ impl<'a, T: StorageManager, C: StarknetClient, F: FileManager> MetadataManager<'
file_type: content_type,
});
} else {
println!("Fetching image... {}", url);

let response = reqwest::get(url).await?;

println!("LA");

let bytes = response.bytes().await?;

self.file_manager
Expand All @@ -136,7 +156,6 @@ impl<'a, T: StorageManager, C: StarknetClient, F: FileManager> MetadataManager<'
content: bytes.to_vec(),
})
.await?;

}

Ok(MetadataImage {
Expand Down Expand Up @@ -282,7 +301,7 @@ mod tests {
mock_storage
.expect_register_token_metadata()
.times(1)
.returning(|_,_,_| Ok(()));
.returning(|_, _, _| Ok(()));

let contract_address = FieldElement::from_hex_be(
"0x0727a63f78ee3f1bd18f78009067411ab369c31dece1ae22e16f567906409905",
Expand Down

0 comments on commit 1b00907

Please sign in to comment.