Skip to content

Commit

Permalink
fix: avoid attachment id duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
crisdut committed Jan 12, 2024
1 parent 377c605 commit 76aeab6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ mod server {
let (id, source) = match encode {
MediaEncode::Base64 => {
let source = base64::encode(&content);
let id = blake3::hash(&content).to_hex().to_string();
let id = blake3::hash(blake3::hash(&content).as_bytes())
.to_hex()
.to_string();
(id, source)
}
MediaEncode::Sha2 => {
Expand All @@ -105,15 +107,15 @@ mod server {
}
MediaEncode::Blake3 => {
let source = blake3::hash(&content).to_hex().to_string();
let id = source.clone();
let id = source.to_string();
(id, source)
}
};

let metadata = MediaMetadata::new(&id, &media.ty, &media.uri, &source);

// Store File
let attachment_id = blake3::hash(source.as_bytes()).to_hex().to_lowercase();
let attachment_id = id;
let file_req = RgbProxyMediaFileReq {
params: RgbProxyMedia {
attachment_id: attachment_id.clone(),
Expand All @@ -134,6 +136,7 @@ mod server {
file_name: metadata_id.clone(),
bytes: metadata_content,
};

proxy_media_store(file_req).await?;

Ok(metadata)
Expand Down
2 changes: 1 addition & 1 deletion src/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ pub async fn finish_auction_offers(
iface,
buyer_invoice,
..
}) = all_bids.clone().get(0)
}) = all_bids.clone().first()
{
let all_invoices = all_bids
.clone()
Expand Down
20 changes: 20 additions & 0 deletions tests/rgb/web/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,23 @@ async fn create_uda_with_medias() {
let issue_resp: JsValue = resolve(full_issue_contract(issuer_sk.to_string(), issue_req)).await;
let issuer_resp: IssueResponse = json_parse(&issue_resp);
}

#[wasm_bindgen_test]
#[allow(unused_assignments)]
async fn import_attachments_into_proxy() {
set_panic_hook();
info!("Import Attachments (Static Files Media) on the RGB Proxy");
let media_item = MediaItemRequest {
ty: "image/svg+xml".to_string(),
uri: "https://www.torproject.org/static/images/yec-2023/main-illo.svg".to_string(),
};
let import_media_req = MediaRequest {
preview: Some(media_item.clone()),
media: Some(media_item.clone()),
attachments: vec![media_item.clone()],
};

let import_media_req = serde_wasm_bindgen::to_value(&import_media_req).expect("");
let import_media_resp: JsValue = resolve(import_uda_data(import_media_req)).await;
let import_media_resp: MediaResponse = json_parse(&import_media_resp);
}

0 comments on commit 76aeab6

Please sign in to comment.