From f6fa5d7269eecde761949dc926d5fb998cb41b48 Mon Sep 17 00:00:00 2001 From: Kilian Schuettler Date: Sun, 2 Feb 2025 02:49:56 +0100 Subject: [PATCH] broken stream data --- src-tauri/src/lib.rs | 54 ++++++++++++++++++++++------- src-tauri/src/tests.rs | 6 ++++ src/components/PrimitiveView.svelte | 35 ++++++++++--------- src/components/StreamEditor.svelte | 24 ++++--------- src/components/TreeView.svelte | 40 +++++++++++++-------- src/models/FileViewState.svelte.ts | 24 +++++++++---- src/models/TreeViewState.svelte.ts | 4 --- 7 files changed, 116 insertions(+), 71 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f72b1a5..8cb9c35 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -8,7 +8,7 @@ use crate::pdf::object::Resolve; use lazy_static::lazy_static; use pdf::file::{File, FileOptions, NoLog, ObjectCache, StreamCache}; use pdf::object::{Object, ObjectWrite, PlainRef, Stream, Trace}; -use pdf::primitive::Primitive; +use pdf::primitive::{Dictionary, Primitive}; use pdf::xref::XRef; use regex::Regex; use serde::{Deserialize, Serialize}; @@ -223,7 +223,11 @@ fn get_contents( } #[tauri::command] -fn get_stream_data(id: &str, path: &str, session: State>) -> Result { +fn get_stream_data( + id: &str, + path: &str, + session: State>, +) -> Result, String> { let session_guard = session .lock() .map_err(|_| "Failed to lock the session mutex.".to_string())?; @@ -231,7 +235,7 @@ fn get_stream_data(id: &str, path: &str, session: State>) -> Resu get_stream_data_by_path_with_file(path, &file.cos_file) } -fn get_stream_data_by_path_with_file(path: &str, file: &CosFile) -> Result { +fn get_stream_data_by_path_with_file(path: &str, file: &CosFile) -> Result, String> { let mut steps = Step::parse(path); if steps .pop_back() @@ -247,7 +251,7 @@ fn get_stream_data_by_path_with_file(path: &str, file: &CosFile) -> Result::from_stream(stream, &resolver)).data(&resolver)); - Ok(String::from_utf8_lossy(&data).into_owned()) + Ok(data.to_vec()) } #[tauri::command] @@ -563,7 +567,7 @@ impl PrimitiveModel { Primitive::Boolean(b) => b.to_string(), Primitive::String(s) => s.to_string_lossy(), Primitive::Stream(_) => "-".to_string(), - Primitive::Dictionary(_) => "-".to_string(), + Primitive::Dictionary(dict) => PrimitiveModel::format_dict_content(dict), Primitive::Array(arr) => PrimitiveModel::format_arr_content(arr), Primitive::Reference(pref) => { format!("Obj Nr: {} Gen Nr: {}", pref.id, pref.gen) @@ -592,6 +596,26 @@ impl PrimitiveModel { } } + fn format_dict_content(dict: &Dictionary) -> String { + let mut result = String::from("{"); + let mut count = 0; + for (key, value) in dict.iter() { + result.push_str(&format!( + "{}: {}", + key, + PrimitiveModel::format_prim_short(value) + )); + count += 1; + if count < 4 { + result.push_str(", "); + } else { + result.push_str(",..."); + break; + } + } + result.push_str("}"); + result + } fn format_arr_content(arr: &Vec) -> String { if arr.len() == 0 { return "[]".to_string(); @@ -600,14 +624,7 @@ impl PrimitiveModel { let contents = if arr.len() > 4 { &arr[0..4] } else { &arr[..] }; for i in 0..contents.len() { let prim = contents.get(i).unwrap(); - result.push_str(&match prim { - Primitive::Integer(i) => format!("{}", i), - Primitive::Number(n) => format!("{}", n), - Primitive::Boolean(b) => format!("{}", b), - Primitive::String(s) => s.to_string().unwrap_or(String::from("-")), - Primitive::Name(n) => n.as_str().to_string(), - _ => prim.get_debug_name().to_string(), - }); + result.push_str(&PrimitiveModel::format_prim_short(prim)); if i != contents.len() - 1 { result.push_str(", "); } @@ -620,6 +637,17 @@ impl PrimitiveModel { result } + fn format_prim_short(prim: &Primitive) -> String { + match prim { + Primitive::Integer(i) => format!("{}", i), + Primitive::Number(n) => format!("{}", n), + Primitive::Boolean(b) => format!("{}", b), + Primitive::String(s) => s.to_string().unwrap_or(String::from("-")), + Primitive::Name(n) => n.as_str().to_string(), + _ => prim.get_debug_name().to_string(), + } + } + fn from_primitive_with_children( primitive: &Primitive, trace: Vec, diff --git a/src-tauri/src/tests.rs b/src-tauri/src/tests.rs index 2b97670..180fadd 100644 --- a/src-tauri/src/tests.rs +++ b/src-tauri/src/tests.rs @@ -66,22 +66,28 @@ mod tests { children: vec![TreeViewRequest { key: "1".to_string(), children: vec![], + expand: true, }], + expand: true, }); path.push(TreeViewRequest { key: "Info".to_string(), children: vec![], + expand: true, }); path.push(TreeViewRequest { key: "Root".to_string(), children: vec![TreeViewRequest { key: "Pages".to_string(), children: vec![], + expand: true, }], + expand: true, }); let root = TreeViewRequest { key: "Trailer".to_string(), children: path, + expand: true, }; let message = format!("Retrieval of {:?}", root); diff --git a/src/components/PrimitiveView.svelte b/src/components/PrimitiveView.svelte index 8ded865..db85f73 100644 --- a/src/components/PrimitiveView.svelte +++ b/src/components/PrimitiveView.svelte @@ -22,18 +22,19 @@ let bodyHeight = $derived(height - headerOffset); let editorHeight = $derived(Math.max(800, bodyHeight - tableHeight)); - $inspect(fState.highlightedPrim); + let imageUrl = ""; + + // Example: Simulating a binary Uint8Array (normally fetched from an API) + let binaryData = new Uint8Array([fState.stream_data]); + + if (binaryData.length > 0) { + const blob = new Blob([binaryData], { type: "image/png" }); + imageUrl = URL.createObjectURL(blob); + } function handlePrimSelect(prim: Primitive) { - if (prim.isContainer()) { - if (!prim) { - fState.prim = prim; - return; - } - const _path: string[] = fState.copyPath(); - _path.push(prim?.key); - fState.selectPath(_path); - } - return; + const _path: string[] = fState.copyPath(); + _path.push(prim?.key); + fState.selectPath(_path); } function handleScroll(event: Event & { currentTarget: HTMLElement }) { @@ -45,6 +46,9 @@ } +{#if fState.stream_data} + +{/if} {#if prim && prim.children && prim.children.length > 0}
@@ -72,10 +76,10 @@ {#each entriesToDisplay as entry} - (fState.highlightedPrim = entry)} + (fState.highlighted_prim = entry.key)} ondblclick={() => handlePrimSelect(entry)} > @@ -97,10 +101,9 @@
- {#if fState.prim?.ptype === "Stream"} + {#if fState.stream_data} {/if} diff --git a/src/components/StreamEditor.svelte b/src/components/StreamEditor.svelte index 5e8bd1b..e90230c 100644 --- a/src/components/StreamEditor.svelte +++ b/src/components/StreamEditor.svelte @@ -4,11 +4,8 @@ import { onMount } from "svelte"; import * as monaco from "monaco-editor"; - let { - fileId, - path, - height, - }: { fileId: string; path: string; height: number } = $props(); + let { stream_data, height }: { stream_data: string; height: number } = + $props(); let contents: string | undefined = $state(undefined); let editorContainer: HTMLElement; @@ -30,20 +27,13 @@ }); $effect(() => { - loadContents(path, fileId); + loadContents(stream_data); }); - function loadContents(path: string | undefined, id: string) { - if (!path || !id) return; - path = path + "/Data"; - invoke("get_stream_data", { id, path }) - .then((result) => { - contents = result; - if (contents && editor) { - editor.setValue(contents); - } - }) - .catch((err) => console.error(err)); + function loadContents(stream_data: string | undefined) { + if (!stream_data || !editor) return; + + editor.setValue(stream_data); } diff --git a/src/components/TreeView.svelte b/src/components/TreeView.svelte index 5f2f943..0bb0327 100644 --- a/src/components/TreeView.svelte +++ b/src/components/TreeView.svelte @@ -123,20 +123,22 @@ {/if}
- -

+

+ +
+
{formatDisplayKey(entry.key)} -

-

- {" | " + - entry.value + - " | " + - entry.sub_type + - " | " + - entry.ptype} -

+
+ {" | " + + entry.value + + " | " + + entry.sub_type + + " | " + + entry.ptype} +
+
{/if} {/each} @@ -145,6 +147,14 @@