Added basic Photo::add support, untested as of now
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
230cb328d5
commit
53e56f9122
32
Cargo.lock
generated
32
Cargo.lock
generated
@ -86,6 +86,12 @@ version = "3.8.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8f1e260c3a9040a7c19a12468758f4c16f31a81a1fe087482be9570ec864bb6c"
|
checksum = "8f1e260c3a9040a7c19a12468758f4c16f31a81a1fe087482be9570ec864bb6c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "byteorder"
|
||||||
|
version = "1.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytes"
|
name = "bytes"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@ -98,6 +104,16 @@ version = "1.0.71"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "79c2681d6594606957bbb8631c4b90a7fcaaa72cdb714743a437b156d6a7eedd"
|
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]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@ -391,6 +407,15 @@ dependencies = [
|
|||||||
"hashbrown",
|
"hashbrown",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "infer"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ea70330449622910e0edebab230734569516269fb32342fb0a8956340fa48c6c"
|
||||||
|
dependencies = [
|
||||||
|
"cfb",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "instant"
|
name = "instant"
|
||||||
version = "0.1.11"
|
version = "0.1.11"
|
||||||
@ -445,6 +470,7 @@ dependencies = [
|
|||||||
"dirs",
|
"dirs",
|
||||||
"hyper",
|
"hyper",
|
||||||
"hyper-tls",
|
"hyper-tls",
|
||||||
|
"infer",
|
||||||
"mockito",
|
"mockito",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
@ -1193,6 +1219,12 @@ version = "0.2.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uuid"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "vcpkg"
|
name = "vcpkg"
|
||||||
version = "0.2.15"
|
version = "0.2.15"
|
||||||
|
@ -17,6 +17,7 @@ toml = "0.5.8"
|
|||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
tabled = "0.3.0"
|
tabled = "0.3.0"
|
||||||
|
infer = "0.5.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
mockito = "0.30.0"
|
mockito = "0.30.0"
|
||||||
|
24
src/main.rs
24
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_tls::HttpsConnector;
|
||||||
use hyper::{Client, client::HttpConnector};
|
use hyper::{Client, client::HttpConnector};
|
||||||
use clap::{App, Arg, SubCommand, crate_version};
|
use clap::{App, Arg, SubCommand, crate_version};
|
||||||
@ -11,6 +11,7 @@ mod album;
|
|||||||
mod session;
|
mod session;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod config;
|
mod config;
|
||||||
|
mod photo;
|
||||||
|
|
||||||
pub struct LycheeClient {
|
pub struct LycheeClient {
|
||||||
client: Client<HttpsConnector<HttpConnector>>,
|
client: Client<HttpsConnector<HttpConnector>>,
|
||||||
@ -152,6 +153,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
.short("P")
|
.short("P")
|
||||||
.help("Set the password for the album. Leave empty for none.")
|
.help("Set the password for the album. Leave empty for none.")
|
||||||
.value_name("PASSWORD"))
|
.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();
|
let matches = app.clone().get_matches();
|
||||||
|
|
||||||
@ -167,7 +184,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
let lychee_session = Cookie::parse(read_from_file(session_path.to_str().unwrap()))
|
let lychee_session = Cookie::parse(read_from_file(session_path.to_str().unwrap()))
|
||||||
.expect("Cannot parse cookie from session storage.");
|
.expect("Cannot parse cookie from session storage.");
|
||||||
let lychee_session_cookie = lychee_session.name().to_string() + "=" + lychee_session.value();
|
let lychee_session_cookie = lychee_session.name().to_string() + "=" + lychee_session.value();
|
||||||
|
|
||||||
if let Some(matches) = matches.subcommand_matches("login") {
|
if let Some(matches) = matches.subcommand_matches("login") {
|
||||||
let username = matches.value_of("username").unwrap();
|
let username = matches.value_of("username").unwrap();
|
||||||
let password = matches.value_of("password").unwrap();
|
let password = matches.value_of("password").unwrap();
|
||||||
@ -207,6 +223,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
let s = m.is_present("share_button_visible");
|
let s = m.is_present("share_button_visible");
|
||||||
let pass = m.value_of("password").unwrap_or("");
|
let pass = m.value_of("password").unwrap_or("");
|
||||||
set_public_album(&client, &lychee_session_cookie, i, p, v, d, s, f, n, pass).await;
|
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 {
|
} else {
|
||||||
App::print_long_help(&mut app).unwrap();
|
App::print_long_help(&mut app).unwrap();
|
||||||
}
|
}
|
||||||
|
44
src/photo.rs
Normal file
44
src/photo.rs
Normal file
@ -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<Vec<u8>> {
|
||||||
|
// 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user