From 2e8a7731d60aac39b13998d474959a5442825c96 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Fri, 22 Jul 2022 11:46:07 +0900 Subject: [PATCH] Fix image reader (#512) * ignore table * ignore unsupported image * fix * fix * fix * fix Co-authored-by: bokuweb --- Cargo.lock | 11 +++++++++++ docx-core/src/documents/mod.rs | 20 +++++++++++--------- docx-core/src/reader/table_cell.rs | 1 + docx-wasm/Cargo.toml | 1 + docx-wasm/package.json | 2 +- docx-wasm/src/doc.rs | 4 ++++ 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25fc37b..2c5cc24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,6 +102,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if 1.0.0", + "wasm-bindgen", +] + [[package]] name = "crc32fast" version = "1.3.2" @@ -167,6 +177,7 @@ dependencies = [ name = "docx-wasm" version = "0.1.0" dependencies = [ + "console_error_panic_hook", "docx-rs", "wasm-bindgen", ] diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 4505cde..a7dca31 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -230,14 +230,15 @@ impl Docx { path: impl Into, buf: Vec, ) -> Self { - let dimg = image::load_from_memory(&buf).expect("Should load image from memory."); - let mut png = std::io::Cursor::new(vec![]); - // For now only png supported - dimg.write_to(&mut png, ImageFormat::Png) - .expect("Unable to write dynamic image"); + if let Ok(dimg) = image::load_from_memory(&buf) { + let mut png = std::io::Cursor::new(vec![]); + // For now only png supported + dimg.write_to(&mut png, ImageFormat::Png) + .expect("Unable to write dynamic image"); - self.images - .push((id.into(), path.into(), Image(buf), Png(png.into_inner()))); + self.images + .push((id.into(), path.into(), Image(buf), Png(png.into_inner()))); + } self } @@ -987,8 +988,9 @@ fn push_comment_and_comment_extended( comments.push(c.get_comment()); let comment_extended = CommentExtended::new(para_id); if let Some(parent_comment_id) = comment.parent_comment_id { - let parent_para_id = comment_map.get(&parent_comment_id).unwrap().clone(); - comments_extended.push(comment_extended.parent_paragraph_id(parent_para_id)); + if let Some(parent_para_id) = comment_map.get(&parent_comment_id) { + comments_extended.push(comment_extended.parent_paragraph_id(parent_para_id.clone())); + } } else { comments_extended.push(comment_extended); } diff --git a/docx-core/src/reader/table_cell.rs b/docx-core/src/reader/table_cell.rs index 39f823d..a7c66f6 100644 --- a/docx-core/src/reader/table_cell.rs +++ b/docx-core/src/reader/table_cell.rs @@ -11,6 +11,7 @@ impl ElementReader for TableCell { let mut cell = TableCell::new(); loop { let e = r.next(); + match e { Ok(XmlEvent::StartElement { attributes, name, .. diff --git a/docx-wasm/Cargo.toml b/docx-wasm/Cargo.toml index dcc25be..ed7a235 100644 --- a/docx-wasm/Cargo.toml +++ b/docx-wasm/Cargo.toml @@ -11,4 +11,5 @@ crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2.78" +console_error_panic_hook = "0.1.7" docx-rs= { path = "../docx-core" } diff --git a/docx-wasm/package.json b/docx-wasm/package.json index a0a1b86..8c5703a 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.273", + "version": "0.0.274-image-test8", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/src/doc.rs b/docx-wasm/src/doc.rs index 56dd020..4007e64 100644 --- a/docx-wasm/src/doc.rs +++ b/docx-wasm/src/doc.rs @@ -1,6 +1,8 @@ use super::*; use wasm_bindgen::prelude::*; +extern crate console_error_panic_hook; + #[wasm_bindgen] #[derive(Debug)] pub struct Docx(docx_rs::Docx); @@ -8,6 +10,8 @@ pub struct Docx(docx_rs::Docx); #[wasm_bindgen] #[allow(non_snake_case)] pub fn createDocx() -> Docx { + use std::panic; + panic::set_hook(Box::new(console_error_panic_hook::hook)); Docx(docx_rs::Docx::new()) }