diff --git a/Cargo.lock b/Cargo.lock index 1c1bf00..789df0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,6 +86,12 @@ version = "3.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f1e260c3a9040a7c19a12468758f4c16f31a81a1fe087482be9570ec864bb6c" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "bytes" version = "1.1.0" @@ -98,6 +104,16 @@ version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79c2681d6594606957bbb8631c4b90a7fcaaa72cdb714743a437b156d6a7eedd" +[[package]] +name = "cfb" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca453e8624711b2f0f4eb47076a318feda166252a827ee25d067b43de83dcba0" +dependencies = [ + "byteorder", + "uuid", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -391,6 +407,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "infer" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea70330449622910e0edebab230734569516269fb32342fb0a8956340fa48c6c" +dependencies = [ + "cfb", +] + [[package]] name = "instant" version = "0.1.11" @@ -445,6 +470,7 @@ dependencies = [ "dirs", "hyper", "hyper-tls", + "infer", "mockito", "serde", "serde_json", @@ -1193,6 +1219,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index d0e92e6..cd4f247 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ toml = "0.5.8" serde = { version = "1", features = ["derive"] } serde_json = "1" tabled = "0.3.0" +infer = "0.5.0" [dev-dependencies] mockito = "0.30.0" diff --git a/src/main.rs b/src/main.rs index 71b4319..58bdc0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use crate::{album::{add_album, delete_album, get_album, set_description_album, set_public_album, set_title_album}, albums::get_albums, config::{get_session_file, read_config, setup_config_data_storage}, session::login, utils::{body_to_str, numeric_validator, read_from_file, write_to_file}}; +use crate::{album::{add_album, delete_album, get_album, set_description_album, set_public_album, set_title_album}, albums::get_albums, config::{get_session_file, read_config, setup_config_data_storage}, photo::add_photo, session::login, utils::{body_to_str, numeric_validator, read_from_file, write_to_file}}; use hyper_tls::HttpsConnector; use hyper::{Client, client::HttpConnector}; use clap::{App, Arg, SubCommand, crate_version}; @@ -11,6 +11,7 @@ mod album; mod session; mod utils; mod config; +mod photo; pub struct LycheeClient { client: Client>, @@ -152,6 +153,22 @@ async fn main() -> Result<(), Box> { .short("P") .help("Set the password for the album. Leave empty for none.") .value_name("PASSWORD")) + ) + .subcommand( + SubCommand::with_name("add_photo") + .about("Add some picture to a lychee instance.") + .arg(Arg::with_name("album_id") + .required(true) + .long("album_id") + .short("a") + .help("The album id to add the picture to.") + .value_name("ID")) + .arg(Arg::with_name("photo_path") + .required(true) + .long("photo_path") + .short("p") + .help("Where the picture that you want to add is located.") + .value_name("PATH")) ); let matches = app.clone().get_matches(); @@ -167,7 +184,6 @@ async fn main() -> Result<(), Box> { let lychee_session = Cookie::parse(read_from_file(session_path.to_str().unwrap())) .expect("Cannot parse cookie from session storage."); let lychee_session_cookie = lychee_session.name().to_string() + "=" + lychee_session.value(); - if let Some(matches) = matches.subcommand_matches("login") { let username = matches.value_of("username").unwrap(); let password = matches.value_of("password").unwrap(); @@ -207,6 +223,10 @@ async fn main() -> Result<(), Box> { let s = m.is_present("share_button_visible"); let pass = m.value_of("password").unwrap_or(""); set_public_album(&client, &lychee_session_cookie, i, p, v, d, s, f, n, pass).await; + } else if let Some(m) = matches.subcommand_matches("add_photo") { + let a = m.value_of("album_id").unwrap(); + let i = m.value_of("photo_path").unwrap(); + println!("{}", add_photo(&client, &lychee_session_cookie, a, i).await); } else { App::print_long_help(&mut app).unwrap(); } diff --git a/src/photo.rs b/src/photo.rs new file mode 100644 index 0000000..626a4d3 --- /dev/null +++ b/src/photo.rs @@ -0,0 +1,44 @@ +use std::{io::{Write, Read}, fs::File, path::Path}; +use hyper::{Method, Request, header::{COOKIE, CONTENT_TYPE, AUTHORIZATION}}; +use crate::{LycheeClient, utils::{body_to_str, numeric_validator}}; + + +fn image_data(b: &str, a: &str, i: &str) -> std::io::Result> { + // TODO check for data type + let mut data = Vec::new(); + write!(data, "--{}\r\n", b)?; + write!(data, "Content-Disposition: form-data; name=\"albumID\"\r\n")?; + write!(data, "\r\n")?; + write!(data, "{}\r\n", a)?; + write!(data, "--{}\r\n", b)?; + write!(data, "Content-Disposition: form-data; name=\"0\"; filename=\"{}\"\r\n", Path::new(i).file_name().unwrap().to_str().unwrap())?; + write!(data, "Content-Type: {}\r\n", infer::get_from_path(i).unwrap().unwrap())?; + write!(data, "\r\n")?; + + let mut f = File::open(i)?; + f.read_to_end(&mut data)?; + + write!(data, "\r\n")?; // The key thing you are missing + write!(data, "--{}--\r\n", b)?; + + Ok(data) +} + +pub async fn add_photo(c: &LycheeClient, l: &str, a: &str, i: &str) -> String { + if !Path::new(i).is_file() { panic!("Cannot upload anything else than a file.") }; + let b = "HELLOEHLO"; + let req = Request::builder() + .method(Method::POST) + .uri(c.endpoint.to_string() + "/api/Photo::add") + .header(COOKIE, l) + .header(AUTHORIZATION, c.api_key.to_string()) + .header(CONTENT_TYPE, "multipart/form-data; boundary=".to_string() + b) + .body(image_data(b, a, i).unwrap().into()) + .expect("Cannot request /api/Photo::add."); + let res = c.client.request(req).await.unwrap(); + let s = res.status(); + let b = body_to_str(res.into_body()).await; + assert!(numeric_validator(b.to_string()).is_ok(), "The server didn't reply with a photo id: '{}'.", b); + assert_ne!(s, 500, "The server returned an internal error."); + return b; +}