Skip to content

Commit

Permalink
Clean code and reword README
Browse files Browse the repository at this point in the history
  • Loading branch information
hongquan committed Aug 24, 2021
1 parent bd69298 commit def28be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "duri"
version = "0.1.2"
version = "0.1.3"
authors = ["Nguyễn Hồng Quân <[email protected]>"]
edition = "2018"
license = "GPL-3.0-or-later"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cargo install duri

## Use cases

Assume that you need to upload file to a RESTful HTTP API. The HTTP API may require posted data to be JSON string and the file content to be in form of base64-encoding [data URI](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
Assume that you need to upload file to a RESTful HTTP API. The HTTP API may require posted data to be JSON string and the file content to be in form of base64-encoded [data URI](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).

You can combine Duri with [jo](https://github.com/jpmens/jo) to build JSON, like:

Expand Down Expand Up @@ -67,7 +67,7 @@ The `duri` + `jo` combo will generate a JSON like
The string is passed to HTTPie via standard input and HTTPie will build a POST request with that JSON data.

Note that, if your HTTP API requires file to be in plain base64 string, not beginning with `data:xxx`, you don't need Duri.
In that case, just use `jo` alone, with its `%` directive:
In that case, just use `jo` alone, with its `%` modifier:

```console
jo -d. file.name=image.png file.content=%image.png | http example-api.vn/ekyc/
Expand All @@ -78,4 +78,4 @@ Credit
------

- Brought to you by [Nguyễn Hồng Quân](https://quan.hoabinh.vn).
- Application is from [Freepik](https://www.flaticon.com/free-icon/durian_765534).
- Icon is from [Freepik](https://www.flaticon.com/free-icon/durian_765534).
33 changes: 17 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use std::path::Path;
use std::io::{stdin, Read};
use std::str;
use std::path::Path;

use log;
use base64;
use tree_magic_mini;
use urlencoding::encode as urlencode;
use clap::{crate_authors, crate_description, crate_version, Clap};
use color_eyre::eyre::Result;
use eyre::eyre;
use clap::{crate_version, crate_authors, crate_description, Clap};
use flexi_logger::Logger;

use urlencoding::encode as urlencode;

#[derive(Clap)]
#[clap(version = crate_version!(), author = crate_authors!(), about = crate_description!())]
Expand All @@ -19,44 +14,46 @@ struct Opts {
infile: String,
#[clap(short, long, about = "Prefer percent encoding for text file.")]
text: bool,
#[clap(short, long, parse(from_occurrences), about = "Verbosity level (repeat to increase).")]
#[clap(
short,
long,
parse(from_occurrences),
about = "Verbosity level (repeat to increase)."
)]
verbose: i8,
}


fn read_input(infile: &str) -> Result<Vec<u8>> {
match infile {
"-" => {
let mut buf = Vec::<u8>::new();
let mut stdin = stdin();
stdin.read_to_end(&mut buf)?;
Ok(buf)
},
}
_ => {
let path = Path::new(infile);
std::fs::read(path).map_err(|e| eyre!(e))
}
}
}


fn encode(content: Vec<u8>, mtype: &str, prefer_percent: bool) -> Result<String> {
let mut body = String::new();
if prefer_percent && mtype.starts_with("text/") {
body = urlencode(str::from_utf8(&content)?)
body = urlencode(std::str::from_utf8(&content)?)
} else {
base64::encode_config_buf(&content, base64::URL_SAFE, &mut body)
}
Ok(body)
}


fn main() -> Result<()> {
let opts = Opts::parse();
let level = match opts.verbose {
0 => "warning",
1 => "info",
_ => "debug"
_ => "debug",
};
Logger::try_with_str(level)?.start()?;
color_eyre::install()?;
Expand All @@ -67,7 +64,11 @@ fn main() -> Result<()> {
// Note: "content" is moved to encode() to be dropped early
let body = encode(content, mtype, opts.text)?;
log::info!("MIME type: {}", mtype);
let encoding = if opts.text && mtype.starts_with("text/") { "" } else { "base64" };
let encoding = if opts.text && mtype.starts_with("text/") {
""
} else {
"base64"
};
print!("data:{};{},{}", mtype, encoding, body);
Ok(())
}

0 comments on commit def28be

Please sign in to comment.