add gemini::gio::memory_input_stream collector

This commit is contained in:
yggverse 2024-10-29 18:37:12 +02:00
parent 15a2ef81bc
commit 66dc0ecfe8

View File

@ -545,51 +545,90 @@ impl Page {
ClientMime::ImagePng | ClientMime::ImageGif | ClientMime::ImagePng | ClientMime::ImageGif |
ClientMime::ImageJpeg | ClientMime::ImageWebp ClientMime::ImageJpeg | ClientMime::ImageWebp
) => { ) => {
// Init loading placeholder // Final image size unknown, show loading widget
/* @TODO count bytes on download
let title = gformat!("Loading..");
let description = gformat!(""); // collect totals here, invisible on start
content.set_status_loading( content.set_status_loading(
Some(&title), Some(&gformat!("Loading..")),
Some(&description) None
); */ );
match Pixbuf::from_stream( // @TODO async // Asynchronously move `InputStream` data from `SocketConnection` into the local `MemoryInputStream`
&connection.input_stream(), // this action allows to count the bytes for loading widget and validate max size for incoming data
None::<&Cancellable>, gemini::gio::memory_input_stream::from_socket_connection_async(
) { connection.clone(),
Ok(buffer) => { None::<Cancellable>,
// Update page meta Priority::DEFAULT,
meta.borrow_mut().status = Some(Status::Success); 0x400, // 1024 bytes per chunk, optional step for images download tracking
meta.borrow_mut().title = Some(gformat!("Image")); 0xA00000, // 10M bytes max to prevent memory overflow if server play with promises @TODO optional?
move |(_, total)| {
println!("{total}");
// Update loading progress
// description = gformat!("{total}");
},
move |result| match result {
Ok(memory_input_stream) => {
match Pixbuf::from_stream( // @TODO async
&memory_input_stream,
None::<&Cancellable>,
) {
Ok(buffer) => {
// Update page meta
meta.borrow_mut().status = Some(Status::Success);
meta.borrow_mut().title = Some(gformat!("Image"));
// Update page content // Update page content
content.set_image(&buffer); content.set_image(&buffer);
// Update window components // Update window components
action_update.activate(Some(&id)); action_update.activate(Some(&id));
} }
Err(reason) => { // Pixbuf::from_stream Err(reason) => { // Pixbuf::from_stream
// Define common data // Define common data
let status = Status::Failure; let status = Status::Failure;
let title = gformat!("Oops"); let title = gformat!("Oops");
let description = gformat!("{}", reason.message()); let description = gformat!("{}", reason.message());
// Update widget // Update widget
content.set_status_failure( content.set_status_failure(
Some(title.as_str()), Some(title.as_str()),
Some(description.as_str()) Some(description.as_str())
); );
// Update meta // Update meta
meta.replace(Meta { meta.replace(Meta {
status: Some(status), status: Some(status),
title: Some(title), title: Some(title),
//description: Some(description), //description: Some(description),
}); });
} }
} }
},
Err((error, reason)) => {
// Define common data
let status = Status::Failure;
let title = gformat!("Oops");
let description = match reason {
Some(message) => gformat!("{message}"),
None => match error {
gemini::gio::memory_input_stream::Error::BytesTotal => gformat!("Allowed size reached"),
gemini::gio::memory_input_stream::Error::InputStream => gformat!("Input stream error"),
},
};
// Update widget
content.set_status_failure(
Some(title.as_str()),
Some(description.as_str())
);
// Update meta
meta.replace(Meta {
status: Some(status),
title: Some(title),
//description: Some(description),
});
}
},
);
}, },
/* @TODO stream or download /* @TODO stream or download
Some( Some(