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,18 +545,29 @@ 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
); */ );
// Asynchronously move `InputStream` data from `SocketConnection` into the local `MemoryInputStream`
// this action allows to count the bytes for loading widget and validate max size for incoming data
gemini::gio::memory_input_stream::from_socket_connection_async(
connection.clone(),
None::<Cancellable>,
Priority::DEFAULT,
0x400, // 1024 bytes per chunk, optional step for images download tracking
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 match Pixbuf::from_stream( // @TODO async
&connection.input_stream(), &memory_input_stream,
None::<&Cancellable>, None::<&Cancellable>,
) { ) {
Ok(buffer) => { Ok(buffer) => {
@ -591,6 +602,34 @@ impl Page {
} }
} }
}, },
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(
ClientMime::AudioFlac | ClientMime::AudioMpeg | ClientMime::AudioOgg ClientMime::AudioFlac | ClientMime::AudioMpeg | ClientMime::AudioOgg