Fix img reader (#480)

* fix: image reader

* fix: image
main
bokuweb 2022-05-20 17:00:24 +09:00 committed by GitHub
parent 4165c8b107
commit dc21e50877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 10 deletions

View File

@ -24,6 +24,11 @@ impl DocumentRels {
self.custom_xml_count += 1; self.custom_xml_count += 1;
self self
} }
pub fn add_image(mut self, id: impl Into<String>, path: impl Into<String>) -> Self {
self.images.push((id.into(), path.into()));
self
}
} }
impl BuildXML for DocumentRels { impl BuildXML for DocumentRels {

View File

@ -111,7 +111,7 @@ pub struct Docx {
// reader only // reader only
pub themes: Vec<Theme>, pub themes: Vec<Theme>,
// reader only // reader only
pub images: Vec<Image>, pub images: Vec<(String, String, Image)>,
} }
impl Default for Docx { impl Default for Docx {
@ -208,8 +208,13 @@ impl Docx {
} }
// reader only // reader only
pub(crate) fn add_image(mut self, buf: Vec<u8>) -> Self { pub(crate) fn add_image(
self.images.push(Image(buf)); mut self,
id: impl Into<String>,
path: impl Into<String>,
buf: Vec<u8>,
) -> Self {
self.images.push((id.into(), path.into(), Image(buf)));
self self
} }

View File

@ -411,9 +411,9 @@ pub fn read_docx(buf: &[u8]) -> Result<Docx, ReaderError> {
// Read media // Read media
let media = rels.find_target_path(IMAGE_TYPE); let media = rels.find_target_path(IMAGE_TYPE);
if let Some(paths) = media { if let Some(paths) = media {
for (_, media) in paths { for (id, media) in paths {
if let Ok(data) = read_zip(&mut archive, media.to_str().expect("should have media")) { if let Ok(data) = read_zip(&mut archive, media.to_str().expect("should have media")) {
docx = docx.add_image(data); docx = docx.add_image(id, media.to_str().unwrap().to_string(), data);
} }
} }
} }

View File

@ -47,8 +47,8 @@ export type DocxJSON = {
webSettings: WebSettingsJSON; webSettings: WebSettingsJSON;
fontTable: {}; fontTable: {};
themes: ThemeJSON[]; themes: ThemeJSON[];
// base64 encoded iamges //(id, path, base64 encoded image data)
images: string[]; images: [string, string, string][];
}; };
export type SettingsJSON = { export type SettingsJSON = {

View File

@ -1,6 +1,6 @@
{ {
"name": "docx-wasm", "name": "docx-wasm",
"version": "0.0.259", "version": "0.0.260",
"main": "dist/node/index.js", "main": "dist/node/index.js",
"browser": "dist/web/index.js", "browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>", "author": "bokuweb <bokuweb12@gmail.com>",

File diff suppressed because one or more lines are too long