Skip to content

Commit

Permalink
Add to cli option to take input from stdin (#15)
Browse files Browse the repository at this point in the history
* Add to cli option to take input from stdin

* update README.md
  • Loading branch information
PKopel authored Nov 28, 2023
1 parent 553ce6f commit d4d4612
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# filegram

![tests](https://github.com/PKopel/filegram/actions/workflows/build.yml/badge.svg)
![pages](https://github.com/PKopel/filegram/actions/workflows/pages.yml/badge.svg)

Store files as images.

Expand All @@ -12,4 +13,4 @@ Filegram is inspired by the ["Using YouTube as Unlimited Cloud Storage??"](https

- [`filegram`](./filegram/): library for encodeing and decodeing files
- [`filegram-cli`](./filegram-cli/): command line interface for filegram
- [`filegram-web`](./filegram-web/): web tool build with webassembly
- [`filegram-web`](./filegram-web/): filegram web tool
2 changes: 1 addition & 1 deletion filegram-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"

[dependencies]
filegram = { path = "../filegram" }
clap = { version = "4.4.8", features = ["derive"] }
clap = { version = "4.4.10", features = ["derive"] }
serde_json = "1.0.108"

[[bin]]
Expand Down
25 changes: 15 additions & 10 deletions filegram-cli/src/fig.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod utils;

use std::{
fs::{self, File},
io::{BufReader, Error, ErrorKind, Read},
io::{self, BufReader, Error, ErrorKind},
path::Path,
};

Expand Down Expand Up @@ -40,8 +42,8 @@ trait CommandTrait {

#[derive(Args)]
struct Encode {
#[arg(short, long)]
file: String,
#[arg(short, long, help = "path to input file, default is stdin")]
file: Option<String>,
#[arg(short, long)]
output: Option<String>,
#[arg(short, long)]
Expand All @@ -51,18 +53,18 @@ struct Encode {
impl CommandTrait for Encode {
fn execute(&self) -> Result<(), Error> {
let output = self.output.clone().unwrap_or_else(|| self.default_output());
let file = File::open(self.file.clone())?;
let file_size = file.metadata()?.len() as usize;
let mut file = BufReader::new(file);
let data = if let Some(file) = self.file.clone() {
utils::read_to_end(File::open(file.clone())?)
} else {
utils::read_to_end(io::stdin())
}?;
let rgb = if self.encrypted {
let cipher = Cipher::new();
save_cipher_key(cipher.get_key_struct())?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;
let data = cipher.encrypt(&data);
encode::from_slice(&data)
} else {
encode::from_reader(&mut file, file_size)
encode::from_slice(&data)
};
let path = Path::new(&output);
rgb.save(path)
Expand All @@ -71,7 +73,10 @@ impl CommandTrait for Encode {
}

fn default_output(&self) -> String {
self.file.clone() + ".png"
match self.file.clone() {
Some(file) => file + ".png",
None => "output.png".to_owned(),
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions filegram-cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::io::{BufReader, Error, Read};

pub fn read_to_end<R: Read>(reader: R) -> Result<Vec<u8>, Error> {
let mut buffer = BufReader::new(reader);
let mut data = Vec::new();
buffer.read_to_end(&mut data)?;
Ok(data)
}
4 changes: 2 additions & 2 deletions filegram-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ edition = "2021"
filegram = { path = "../filegram" }

getrandom = { version = "0.2.11", features = ["js"] }
js-sys = "0.3.65"
js-sys = "0.3.66"
image = "0.24.7"
web-sys = { version = "0.3.63", features = [
"File",
"Blob",
"Url",
"ReadableStream",
] }
wasm-bindgen = "0.2.86"
wasm-bindgen = "0.2.89"
yew = { version = "0.21.0", features = ["csr"] }
gloo = "0.10.0"
gloo-file = "0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion filegram/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ block-padding = "0.3.3"
chacha20poly1305 = { version = "0.10.1", features = ["heapless"] }
image = "0.24.7"
imageproc = "0.23.0"
serde = { version = "1.0.192", features = ["derive"] }
serde = { version = "1.0.193", features = ["derive"] }

[lib]
name = "filegram"
Expand Down

0 comments on commit d4d4612

Please sign in to comment.