Skip to content

Commit

Permalink
make 1.3.6 release to fix image loading
Browse files Browse the repository at this point in the history
  • Loading branch information
markusdd committed Feb 4, 2025
1 parent 13d0f28 commit 95c0a59
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "easyeda_to_kicad_lib_ui"
version = "1.3.5"
version = "1.3.6"
authors = ["Markus Krause <[email protected]>"]
edition = "2021"
rust-version = "1.82"
Expand Down
67 changes: 63 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,52 @@ impl MyApp {
Default::default()
}

fn get_imglist(lcscnumber: &str, client: &reqwest::blocking::Client) -> Option<Vec<String>> {
// this is the fallback function for when JLCPCB gives us no images, then we resort to asking LCSC
let res_or_err = client
.get(format!(
"https://wmsc.lcsc.com/ftps/wm/product/detail?productCode={}",
lcscnumber
))
.header(reqwest::header::ACCEPT, "application/json")
.header(
reqwest::header::USER_AGENT,
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:134.0) Gecko/20100101 Firefox/134.0",
)
.send();
if let Ok(res) = res_or_err {
if res.status().is_success() {
let pictext = res
.text()
.expect("Issue decoding received response from LCSC.");
let json: serde_json::Value =
serde_json::from_str(&pictext).expect("Issue parsing search result JSON.");

// there is a case where we get a fully valid response in an HTML
// and JSON sense but it tells us via a code field in the JSON
// that no part could be found, in that case we return early with nothing
if let Some(code) = json.get("code") {
if code != 200 {
return None;
}
}
let mut imglist = vec![];
if let Some(data) = json.get("result") {
if let Some(imagelist) = data.get("productImages") {
if let Some(imagevec) = imagelist.as_array() {
for img in imagevec.iter() {
imglist.push(img.to_string().trim_matches('"').to_owned());
}
return Some(imglist);
}
}
}
}
}
// if we fall through to here, we failed getting data somewhere along the way
None
}

fn get_part(search_term: &str) -> Option<IndexMap<String, String>> {
let term = search_term.trim();
let re_jlc = Regex::new(r"/(C\d+)$").unwrap();
Expand Down Expand Up @@ -199,13 +245,26 @@ impl MyApp {
if let Some(imagevec) = imagelist.as_array() {
for (idx, i) in imagevec.iter().enumerate() {
if let Some(imageurl) = i.get("productBigImage") {
// this is a f*ed up case where JLC returns API IDs instead of URLs
if imageurl.is_null() {
if let Some(imageid) = i.get("productBigImageAccessId")
// this does not work right now because of MIME type issues, get from LCSC instead
// if let Some(imageid) = i.get("productBigImageAccessId")
// {
// let apiurl = format!("https://jlcpcb.com/api/file/downloadByFileSystemAccessId/{}.jpg", imageid.to_string().trim_matches('"').to_owned());
// tabledata
// .insert(format!("meta_image{}", idx), apiurl);
// }
if let Some(lcsc_imglist) =
MyApp::get_imglist(lcscnumber, &client)
{
let apiurl = format!("https://jlcpcb.com/api/file/downloadByFileSystemAccessId/{}.jpg", imageid.to_string().trim_matches('"').to_owned());
tabledata
.insert(format!("meta_image{}", idx), apiurl);
for (idx, i) in lcsc_imglist.iter().enumerate() {
tabledata.insert(
format!("meta_image{}", idx),
i.to_owned(),
);
}
}
break;
} else {
tabledata.insert(
format!("meta_image{}", idx),
Expand Down

0 comments on commit 95c0a59

Please sign in to comment.