half-working build

cli-redesign
Avril 4 years ago
parent e1b1539a57
commit 725374d2e2
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1 +1,2 @@
implement output to dir
Remove Cargo.lock when termprogress is uploaded and Cargo.toml is properly formatted Remove Cargo.lock when termprogress is uploaded and Cargo.toml is properly formatted

@ -3,7 +3,6 @@ use super::*;
use std::{ use std::{
path::PathBuf, path::PathBuf,
str, str,
fmt,
}; };
#[derive(Debug, PartialEq, Eq, Hash)] #[derive(Debug, PartialEq, Eq, Hash)]

@ -1,4 +1,4 @@
#![allow(unused_macros)]
pub trait JoinStrsExt: Sized pub trait JoinStrsExt: Sized
{ {
/// Join an iterator of `str` with a seperator /// Join an iterator of `str` with a seperator
@ -25,3 +25,16 @@ where I: Iterator<Item=T>,
output output
} }
} }
macro_rules! unwrap {
($value:expr) => {
{
match $value {
Ok(value) => value,
Err(error) => {
panic!("Fatal error: {}", error)
},
}
}
};
}

@ -1,16 +1,16 @@
use super::*; use super::*;
use std::{ use std::{
str, str,
io::{
self,
Write,
},
}; };
const HEADER_BASE64_JPEG: &'static [u8] = b"/9j/"; const HEADER_BASE64_JPEG: &'static [u8] = b"/9j/";
const HEADER_BASE64_PNG: &'static [u8] = b"iVBO"; const HEADER_BASE64_PNG: &'static [u8] = b"iVBO";
const HEADER_BASE64_GIF: &'static [u8] = b"R0lG"; 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 /// An image type header
#[derive(Debug, PartialEq, Eq, Hash, Clone)] #[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum ImageType pub enum ImageType
@ -50,6 +50,16 @@ impl ImageType
Err(error::Error::InvalidFormat) 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 impl str::FromStr for ImageType

@ -1,4 +1,3 @@
use super::*;
use std::{ use std::{
error, error,
fmt, fmt,

@ -112,6 +112,18 @@ impl BasedLoli
impl<'a> LoliBounds<'a> impl<'a> LoliBounds<'a>
{ {
/// Get the type of image
pub fn image(&self) -> &encoding::ImageType
{
&self.image_type
}
pub fn bounds(&self) -> &Range<usize>
{
&self.range
}
/// Try to create a decoding container for this `BasedLoli` to the file in `path`. /// Try to create a decoding container for this `BasedLoli` to the file in `path`.
pub fn create_child(&self, path: impl AsRef<Path>) -> Result<Loli, error::Error> pub fn create_child(&self, path: impl AsRef<Path>) -> Result<Loli, error::Error>
{ {

@ -1,11 +1,12 @@
#![feature(linked_list_remove)] #![feature(linked_list_remove)]
#![feature(label_break_value)]
#![allow(dead_code)] #![allow(dead_code)]
use termprogress::{ use termprogress::{
progress,
ProgressBar, ProgressBar,
}; };
#[macro_use]
mod ext; mod ext;
use ext::*; use ext::*;

@ -16,8 +16,8 @@ impl error::Error for Error
fn source(&self) -> Option<&(dyn error::Error + 'static)> fn source(&self) -> Option<&(dyn error::Error + 'static)>
{ {
match &self { match &self {
_ => None,
Self::Format(rng) => Some(rng), Self::Format(rng) => Some(rng),
_ => None,
} }
} }
} }

@ -17,12 +17,15 @@ mod progress;
/// Decode a loli from path /// Decode a loli from path
pub async fn decode(from: impl AsRef<Path>, to: impl AsRef<Path>, progress: &mut progress::CommandSender) -> Result<loli::Loli, error::Error> pub async fn decode(from: impl AsRef<Path>, to: impl AsRef<Path>, progress: &mut progress::CommandSender) -> Result<loli::Loli, error::Error>
{ {
prog_send!(link progress.println("Mapping child")); prog_send!(progress.println("Mapping child"));
let base = loli::BasedLoli::map(from)?; 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. 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()))); prog_send!(progress.println(format!("Finding bounds ({:?}) {} -> {} bytes", bounds, base.as_ref().len(), base.decoded_size())));
let mut decoded = bounds.create_child(to)?;
//Find extension
let mut decoded = bounds.create_child(to.as_ref().with_extension(bounds.image().ext()))?;
prog_send!(progress.println("Decoding...")); prog_send!(progress.println("Decoding..."));
let sz = bounds.decode(&mut decoded)?; let sz = bounds.decode(&mut decoded)?;
prog_send!(link progress.println(format!("Decode complete ({} bytes)", sz))); prog_send!(link progress.println(format!("Decode complete ({} bytes)", sz)));
@ -37,7 +40,7 @@ pub async fn perform(url: impl AsRef<str>, path: impl AsRef<Path>, progress: &mu
let path = path.as_ref(); let path = path.as_ref();
let mut resp = reqwest::get(url).await?; let resp = reqwest::get(url).await?;
let len = resp.content_length(); let len = resp.content_length();
//prog_send!(link progress.push_task(&task)); //prog_send!(link progress.push_task(&task));
@ -93,39 +96,54 @@ pub async fn work(conf: config::Config) -> Result<(), Box<dyn std::error::Error>
let task = format!("{:?}", path); //TODO: Real task name let task = format!("{:?}", path); //TODO: Real task name
prog_send!(link unwind prog.push_task(&task)); prog_send!(link unwind prog.push_task(&task));
let temp = tempfile::TempFile::new(); let temp = tempfile::TempFile::new();
match perform(&url, &temp, &mut prog).await { let return_value = 'clean: {
Err(e) => panic!("Failed downloading {} -> {:?}: {}", url, temp, e), //TODO: Make real error handler match perform(&url, &temp, &mut prog).await {
Ok(_) => { Err(e) => prog_send!(link prog.println(format!("Failed downloading {} -> {:?}: {}", url, temp, e))),
let path = match path { Ok(_) => {
config::OutputType::File(file) => file, let path = match path {
config::OutputType::Directory(dir) => unimplemented!(), //TODO: implement get hash to file 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, let loli = match decode(&temp, &path, &mut prog).await {
Err(e) => panic!("Failed decoding: {}", e), Ok(v) => v,
}; Err(e) => {
}, prog_send!(link prog.println(format!("Failed decoding: {}", e)));
} break 'clean false;
},
prog_send!(link prog.pop_task(task)); //TODO: Make sure we don't return gracefully before this is called. };
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...")); prog_send!(link prog_writer.println("Children working..."));
let mut done =0;
let total = children.len();
for child in children.into_iter() for child in children.into_iter()
{ {
match child.await { match child.await {
Ok(v) => (), Ok(true) => done+=1,
Err(err) => { 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!(link prog_writer.set_title(""));
prog_send!(try link prog_writer.kill()); prog_send!(try link prog_writer.kill());
prog.await.expect("mpsc fatal"); prog.await.expect("mpsc fatal");
println!("Completed {} / {} lolis ({} failed).", done, total, total-done);
Ok(()) Ok(())
} }

@ -9,7 +9,6 @@ use tokio::{
UnboundedReceiver, UnboundedReceiver,
UnboundedSender UnboundedSender
}, },
Mutex,
}, },
task::{ task::{
self, self,
@ -18,14 +17,7 @@ use tokio::{
}; };
use termprogress::{ use termprogress::{
spinner,
Display, Display,
Spinner,
};
use std::{
sync::Arc,
fmt,
}; };
enum CommandInternal enum CommandInternal

@ -1,4 +1,3 @@
use super::*;
use tokio::{ use tokio::{
sync::{ sync::{
mpsc::{ mpsc::{

Loading…
Cancel
Save