From 725374d2e22c42219b44e726699269917ad9c945 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 16 Jul 2020 17:32:42 +0100 Subject: [PATCH] half-working build --- TODO | 1 + src/config.rs | 1 - src/ext.rs | 15 +++++++- src/loli/encoding.rs | 18 +++++++-- src/loli/error.rs | 1 - src/loli/mod.rs | 12 ++++++ src/main.rs | 3 +- src/tempfile/error.rs | 2 +- src/work_async.rs | 64 ++++++++++++++++++++------------ src/work_async/progress.rs | 8 ---- src/work_async/progress/error.rs | 1 - 11 files changed, 85 insertions(+), 41 deletions(-) diff --git a/TODO b/TODO index 3a0de40..a36aa86 100644 --- a/TODO +++ b/TODO @@ -1 +1,2 @@ +implement output to dir Remove Cargo.lock when termprogress is uploaded and Cargo.toml is properly formatted diff --git a/src/config.rs b/src/config.rs index 1a080fe..9924c35 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,6 @@ use super::*; use std::{ path::PathBuf, str, - fmt, }; #[derive(Debug, PartialEq, Eq, Hash)] diff --git a/src/ext.rs b/src/ext.rs index 8fb6bf2..df4e98c 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -1,4 +1,4 @@ - +#![allow(unused_macros)] pub trait JoinStrsExt: Sized { /// Join an iterator of `str` with a seperator @@ -25,3 +25,16 @@ where I: Iterator, output } } + +macro_rules! unwrap { + ($value:expr) => { + { + match $value { + Ok(value) => value, + Err(error) => { + panic!("Fatal error: {}", error) + }, + } + } + }; +} diff --git a/src/loli/encoding.rs b/src/loli/encoding.rs index cf4cd9f..4708b48 100644 --- a/src/loli/encoding.rs +++ b/src/loli/encoding.rs @@ -1,16 +1,16 @@ use super::*; use std::{ str, - io::{ - self, - Write, - }, }; const HEADER_BASE64_JPEG: &'static [u8] = b"/9j/"; const HEADER_BASE64_PNG: &'static [u8] = b"iVBO"; const HEADER_BASE64_GIF: &'static [u8] = b"R0lG"; +const EXT_JPEG: &'static str = "jpg"; +const EXT_PNG: &'static str = "png"; +const EXT_GIF: &'static str = "gif"; + /// An image type header #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub enum ImageType @@ -50,6 +50,16 @@ impl ImageType Err(error::Error::InvalidFormat) } } + + /// Get the extension for this image type + pub fn ext(&self) -> &str + { + match self { + Self::Jpeg => EXT_JPEG, + Self::Png => EXT_PNG, + Self::Gif => EXT_GIF, + } + } } impl str::FromStr for ImageType diff --git a/src/loli/error.rs b/src/loli/error.rs index 38c3689..25a2482 100644 --- a/src/loli/error.rs +++ b/src/loli/error.rs @@ -1,4 +1,3 @@ -use super::*; use std::{ error, fmt, diff --git a/src/loli/mod.rs b/src/loli/mod.rs index eed2e14..3c67050 100644 --- a/src/loli/mod.rs +++ b/src/loli/mod.rs @@ -112,6 +112,18 @@ impl BasedLoli impl<'a> LoliBounds<'a> { + + /// Get the type of image + pub fn image(&self) -> &encoding::ImageType + { + &self.image_type + } + + pub fn bounds(&self) -> &Range + { + &self.range + } + /// Try to create a decoding container for this `BasedLoli` to the file in `path`. pub fn create_child(&self, path: impl AsRef) -> Result { diff --git a/src/main.rs b/src/main.rs index fd08678..5cbcd0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,12 @@ #![feature(linked_list_remove)] +#![feature(label_break_value)] #![allow(dead_code)] use termprogress::{ - progress, ProgressBar, }; +#[macro_use] mod ext; use ext::*; diff --git a/src/tempfile/error.rs b/src/tempfile/error.rs index 207fb86..d3ac5f3 100644 --- a/src/tempfile/error.rs +++ b/src/tempfile/error.rs @@ -16,8 +16,8 @@ impl error::Error for Error fn source(&self) -> Option<&(dyn error::Error + 'static)> { match &self { - _ => None, Self::Format(rng) => Some(rng), + _ => None, } } } diff --git a/src/work_async.rs b/src/work_async.rs index 2af4b7d..b5000ab 100644 --- a/src/work_async.rs +++ b/src/work_async.rs @@ -17,12 +17,15 @@ mod progress; /// Decode a loli from path pub async fn decode(from: impl AsRef, to: impl AsRef, progress: &mut progress::CommandSender) -> Result { - prog_send!(link progress.println("Mapping child")); + prog_send!(progress.println("Mapping child")); let base = loli::BasedLoli::map(from)?; - prog_send!(link progress.println("Calculating bounds")); + prog_send!(progress.println("Calculating bounds")); let bounds = base.calculate_bounds()?; // If server is returning error code, this will fail. - prog_send!(link progress.println(format!("Decoding ({:?}) {} -> {} bytes", bounds, base.as_ref().len(), base.decoded_size()))); - let mut decoded = bounds.create_child(to)?; + prog_send!(progress.println(format!("Finding bounds ({:?}) {} -> {} bytes", bounds, base.as_ref().len(), base.decoded_size()))); + + //Find extension + + let mut decoded = bounds.create_child(to.as_ref().with_extension(bounds.image().ext()))?; prog_send!(progress.println("Decoding...")); let sz = bounds.decode(&mut decoded)?; prog_send!(link progress.println(format!("Decode complete ({} bytes)", sz))); @@ -37,7 +40,7 @@ pub async fn perform(url: impl AsRef, path: impl AsRef, progress: &mu let path = path.as_ref(); - let mut resp = reqwest::get(url).await?; + let resp = reqwest::get(url).await?; let len = resp.content_length(); //prog_send!(link progress.push_task(&task)); @@ -93,39 +96,54 @@ pub async fn work(conf: config::Config) -> Result<(), Box let task = format!("{:?}", path); //TODO: Real task name prog_send!(link unwind prog.push_task(&task)); - + let temp = tempfile::TempFile::new(); - match perform(&url, &temp, &mut prog).await { - Err(e) => panic!("Failed downloading {} -> {:?}: {}", url, temp, e), //TODO: Make real error handler - Ok(_) => { - let path = match path { - config::OutputType::File(file) => file, - config::OutputType::Directory(dir) => unimplemented!(), //TODO: implement get hash to file - }; - let loli = match decode(&temp, &path, &mut prog).await { - Ok(v) => v, - Err(e) => panic!("Failed decoding: {}", e), - }; - }, - } - - prog_send!(link prog.pop_task(task)); //TODO: Make sure we don't return gracefully before this is called. + let return_value = 'clean: { + match perform(&url, &temp, &mut prog).await { + Err(e) => prog_send!(link prog.println(format!("Failed downloading {} -> {:?}: {}", url, temp, e))), + Ok(_) => { + let path = match path { + config::OutputType::File(file) => file, + config::OutputType::Directory(dir) => unimplemented!(), //TODO: implement get hash to file + }; + let loli = match decode(&temp, &path, &mut prog).await { + Ok(v) => v, + Err(e) => { + prog_send!(link prog.println(format!("Failed decoding: {}", e))); + break 'clean false; + }, + }; + + prog_send!(link prog.println(format!("{:?} Complete", loli))); + break 'clean true; + }, + }; + false + }; + + prog_send!(link prog.pop_task(task)); + return_value })); } prog_send!(link prog_writer.println("Children working...")); + let mut done =0; + let total = children.len(); for child in children.into_iter() { match child.await { - Ok(v) => (), + Ok(true) => done+=1, Err(err) => { - println!("Child failed: {}", err); + prog_send!(try link unwind prog_writer.println(format!("Child panic: {}", err))); }, + _ => (), } } prog_send!(link prog_writer.set_title("")); prog_send!(try link prog_writer.kill()); prog.await.expect("mpsc fatal"); + println!("Completed {} / {} lolis ({} failed).", done, total, total-done); + Ok(()) } diff --git a/src/work_async/progress.rs b/src/work_async/progress.rs index ce6f0d5..6072647 100644 --- a/src/work_async/progress.rs +++ b/src/work_async/progress.rs @@ -9,7 +9,6 @@ use tokio::{ UnboundedReceiver, UnboundedSender }, - Mutex, }, task::{ self, @@ -18,14 +17,7 @@ use tokio::{ }; use termprogress::{ - spinner, Display, - Spinner, -}; - -use std::{ - sync::Arc, - fmt, }; enum CommandInternal diff --git a/src/work_async/progress/error.rs b/src/work_async/progress/error.rs index 88049ed..209e35b 100644 --- a/src/work_async/progress/error.rs +++ b/src/work_async/progress/error.rs @@ -1,4 +1,3 @@ -use super::*; use tokio::{ sync::{ mpsc::{