Compare commits
10 Commits
c261ab588f
...
56d90b57bf
Author | SHA1 | Date | |
---|---|---|---|
56d90b57bf | |||
f8b872a28a | |||
85ffdfc6af | |||
f802a56913 | |||
cf0d1e2591 | |||
53e56f9122 | |||
230cb328d5 | |||
f77667b44f | |||
1fbfa1b122 | |||
50aec4c297 |
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"
|
||||||
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Lychee CLIent
|
||||||
|
|
||||||
|
> A fully CLI client written in Rust.
|
221
src/album.rs
221
src/album.rs
@ -88,15 +88,78 @@ pub async fn delete_album(c: &LycheeClient, lychee_session: &str, id: &str) {
|
|||||||
assert_eq!(b, "true", "The server returned an unexpected value: {}.", b);
|
assert_eq!(b, "true", "The server returned an unexpected value: {}.", b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn set_title_album(c: &LycheeClient, l: &str, id: &str, title: &str) {
|
||||||
|
if title.len() > 100 { panic!("Title can't be longer than 100 characters."); }
|
||||||
|
let b = json!({"albumIDs" : id, "title": title});
|
||||||
|
let req = Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri(c.endpoint.to_string() + "/api/Album::setTitle")
|
||||||
|
.header(COOKIE, l)
|
||||||
|
.header(AUTHORIZATION, c.api_key.to_string())
|
||||||
|
.header(CONTENT_TYPE, "application/json")
|
||||||
|
.body(Body::from(b.to_string()))
|
||||||
|
.expect("Cannot request /api/Album:setTitle.");
|
||||||
|
let res = c.client.request(req).await.unwrap();
|
||||||
|
let s = res.status();
|
||||||
|
let b = body_to_str(res.into_body()).await;
|
||||||
|
assert_ne!(s, 500, "The server returned an internal error.");
|
||||||
|
assert_eq!(b, "true", "The server returned an unexpected value: {}.", b);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_description_album(c: &LycheeClient, l: &str, id: &str, desc: &str) {
|
||||||
|
if desc.len() > 1000 { panic!("Description can't be longer than 1000 characters."); }
|
||||||
|
let b = json!({"albumID" : id, "description": desc});
|
||||||
|
let req = Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri(c.endpoint.to_string() + "/api/Album::setDescription")
|
||||||
|
.header(COOKIE, l)
|
||||||
|
.header(AUTHORIZATION, c.api_key.to_string())
|
||||||
|
.header(CONTENT_TYPE, "application/json")
|
||||||
|
.body(Body::from(b.to_string()))
|
||||||
|
.expect("Cannot request /api/Album:setDescription.");
|
||||||
|
let res = c.client.request(req).await.unwrap();
|
||||||
|
let s = res.status();
|
||||||
|
let b = body_to_str(res.into_body()).await;
|
||||||
|
assert_ne!(s, 500, "The server returned an internal error.");
|
||||||
|
assert_eq!(b, "true", "The server returned an unexpected value: {}.", b);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_public_album(c: &LycheeClient, l: &str, id: &str, public: bool, visible: bool, downloadable: bool, share_button_visible: bool, full_photo: bool, nsfw: bool, password: &str) {
|
||||||
|
let b = json!({
|
||||||
|
"albumID" : id,
|
||||||
|
"public": if public { "1" } else { "0" },
|
||||||
|
"visible": if visible { "1" } else { "0" },
|
||||||
|
"downloadable": if downloadable { "1" } else { "0" },
|
||||||
|
"share_button_visible": if share_button_visible { "1" } else { "0" },
|
||||||
|
"full_photo": if full_photo { "1" } else { "0" },
|
||||||
|
"nsfw": if nsfw { "1" } else { "0" },
|
||||||
|
"password": password
|
||||||
|
});
|
||||||
|
let req = Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri(c.endpoint.to_string() + "/api/Album::setPublic")
|
||||||
|
.header(COOKIE, l)
|
||||||
|
.header(AUTHORIZATION, c.api_key.to_string())
|
||||||
|
.header(CONTENT_TYPE, "application/json")
|
||||||
|
.body(Body::from(b.to_string()))
|
||||||
|
.expect("Cannot request /api/Album:setPublic.");
|
||||||
|
let res = c.client.request(req).await.unwrap();
|
||||||
|
let s = res.status();
|
||||||
|
let b = body_to_str(res.into_body()).await;
|
||||||
|
assert_ne!(s, 500, "The server returned an internal error.");
|
||||||
|
assert_eq!(b, "true", "The server returned an unexpected value: {}.", b);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod album_tests {
|
mod album_tests {
|
||||||
use hyper_tls::HttpsConnector;
|
use hyper_tls::HttpsConnector;
|
||||||
use hyper::Client;
|
use hyper::Client;
|
||||||
use mockito::mock;
|
use mockito::mock;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use crate::{LycheeClient, get_album};
|
use crate::{LycheeClient, album::{set_description_album, set_public_album}, get_album};
|
||||||
|
|
||||||
use super::{add_album, delete_album};
|
use super::{add_album, delete_album, set_title_album};
|
||||||
|
|
||||||
fn setup() -> LycheeClient {
|
fn setup() -> LycheeClient {
|
||||||
let https = HttpsConnector::new();
|
let https = HttpsConnector::new();
|
||||||
@ -107,6 +170,160 @@ mod album_tests {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn set_public_album_correct_values() {
|
||||||
|
let client = setup();
|
||||||
|
let b1 = json!({ "albumID" : "12222", "public": "1", "visible": "0",
|
||||||
|
"downloadable": "1", "share_button_visible": "0", "full_photo": "1",
|
||||||
|
"nsfw": "0", "password": ""
|
||||||
|
});
|
||||||
|
let m1 = mock("POST", "/api/Album::setPublic")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.match_body(b1.to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_public_album(&client, "v", "12222", true, false, true, false, true, false, ""));
|
||||||
|
m1.assert();
|
||||||
|
|
||||||
|
let b2 = json!({ "albumID" : "123", "public": "0", "visible": "1",
|
||||||
|
"downloadable": "0", "share_button_visible": "1", "full_photo": "0",
|
||||||
|
"nsfw": "1", "password": "pass"
|
||||||
|
});
|
||||||
|
let m2 = mock("POST", "/api/Album::setPublic")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.match_body(b2.to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_public_album(&client, "v", "123", false, true, false, true, false, true, "pass"));
|
||||||
|
m2.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn set_public_album_false_returned() {
|
||||||
|
let client = setup();
|
||||||
|
let m = mock("POST", "/api/Album::setPublic")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("false")
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_public_album(&client, "v", "123", false, false, false, false, false, false, ""));
|
||||||
|
m.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn set_description_album_correct_values() {
|
||||||
|
let client = setup();
|
||||||
|
let m1 = mock("POST", "/api/Album::setDescription")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.match_body(json!({"albumID": "1", "description": "a"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_description_album(&client, "v", "1", "a"));
|
||||||
|
m1.assert();
|
||||||
|
|
||||||
|
let m2 = mock("POST", "/api/Album::setDescription")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.match_body(json!({"albumID": "1", "description": "abc"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_description_album(&client, "v", "1", "abc"));
|
||||||
|
m2.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn set_description_album_description_too_long() {
|
||||||
|
let client = setup();
|
||||||
|
let m = mock("POST", "/api/Album::setDescription")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.create();
|
||||||
|
|
||||||
|
let mut b = "a".to_string();
|
||||||
|
for _ in 1..=1001 { b += "a"; }
|
||||||
|
tokio_test::block_on(set_description_album(&client, "v", "1", b.as_str()));
|
||||||
|
m.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn set_description_album_false_returned() {
|
||||||
|
let client = setup();
|
||||||
|
let m = mock("POST", "/api/Album::setDescription")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("false")
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_description_album(&client, "v", "1", "k"));
|
||||||
|
m.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn set_title_album_correct_values() {
|
||||||
|
let client = setup();
|
||||||
|
let m1 = mock("POST", "/api/Album::setTitle")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.match_body(json!({"albumIDs": "1", "title": "hello"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_title_album(&client, "v", "1", "hello"));
|
||||||
|
m1.assert();
|
||||||
|
|
||||||
|
let m2 = mock("POST", "/api/Album::setTitle")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.match_body(json!({"albumIDs": "1,2,3", "title": "k"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_title_album(&client, "v", "1,2,3", "k"));
|
||||||
|
m2.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn set_title_album_title_too_long() {
|
||||||
|
let client = setup();
|
||||||
|
let m = mock("POST", "/api/Album::setTitle")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.create();
|
||||||
|
|
||||||
|
let mut b = "a".to_string();
|
||||||
|
for _ in 1..=101 { b += "a"; }
|
||||||
|
tokio_test::block_on(set_title_album(&client, "v", "1", b.as_str()));
|
||||||
|
m.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn set_title_album_false_returned() {
|
||||||
|
let client = setup();
|
||||||
|
let m = mock("POST", "/api/Album::setTitle")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("false")
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_title_album(&client, "v", "1", "k"));
|
||||||
|
m.assert();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn delete_album_correct_values() {
|
fn delete_album_correct_values() {
|
||||||
let client = setup();
|
let client = setup();
|
||||||
|
148
src/main.rs
148
src/main.rs
@ -1,4 +1,4 @@
|
|||||||
use crate::{album::{add_album, delete_album, get_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, delete_photo, set_album_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>>,
|
||||||
@ -81,7 +82,120 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
.help("The album ID to delete. For multiple IDs, separate them using commas.")
|
.help("The album ID to delete. For multiple IDs, separate them using commas.")
|
||||||
.required(true)
|
.required(true)
|
||||||
.value_name("ID(s)"))
|
.value_name("ID(s)"))
|
||||||
);
|
)
|
||||||
|
.subcommand(
|
||||||
|
SubCommand::with_name("set_album_title")
|
||||||
|
.about("Rename one or more album(s).")
|
||||||
|
.arg(Arg::with_name("title")
|
||||||
|
.short("t")
|
||||||
|
.long("title")
|
||||||
|
.value_name("TITLE")
|
||||||
|
.required(true)
|
||||||
|
.help("The title to set for the albums to rename. Must be under 100 characters long."))
|
||||||
|
.arg(Arg::with_name("id")
|
||||||
|
.short("i")
|
||||||
|
.long("id")
|
||||||
|
.help("The album ID(s) to rename. For multiple IDs, separate them using commas.")
|
||||||
|
.required(true)
|
||||||
|
.value_name("ID(s)"))
|
||||||
|
)
|
||||||
|
.subcommand(
|
||||||
|
SubCommand::with_name("set_album_description")
|
||||||
|
.about("Set the description for one or more album(s).")
|
||||||
|
.arg(Arg::with_name("description")
|
||||||
|
.short("d")
|
||||||
|
.long("description")
|
||||||
|
.value_name("DESCRIPTION")
|
||||||
|
.required(true)
|
||||||
|
.help("The description to set for the album. Must be under 1000 characters long."))
|
||||||
|
.arg(Arg::with_name("id")
|
||||||
|
.short("i")
|
||||||
|
.long("id")
|
||||||
|
.help("The album ID to set the description for.")
|
||||||
|
.required(true)
|
||||||
|
.value_name("ID"))
|
||||||
|
)
|
||||||
|
.subcommand(
|
||||||
|
SubCommand::with_name("set_album_public")
|
||||||
|
.about("Set the visibility for an album.")
|
||||||
|
.arg(Arg::with_name("id")
|
||||||
|
.short("i")
|
||||||
|
.long("id")
|
||||||
|
.help("The album's id to set visibility for.")
|
||||||
|
.value_name("ID")
|
||||||
|
.required(true))
|
||||||
|
.arg(Arg::with_name("public")
|
||||||
|
.short("p")
|
||||||
|
.long("public")
|
||||||
|
.help("Set the album as public."))
|
||||||
|
.arg(Arg::with_name("visible")
|
||||||
|
.short("v")
|
||||||
|
.long("visible")
|
||||||
|
.help("Set the album as visible"))
|
||||||
|
.arg(Arg::with_name("downloadable")
|
||||||
|
.short("d")
|
||||||
|
.long("downloadable")
|
||||||
|
.help("Set the album as downloadable."))
|
||||||
|
.arg(Arg::with_name("share_button_visible")
|
||||||
|
.short("s")
|
||||||
|
.long("share_button_visible")
|
||||||
|
.help("Set the share button as visible for that album."))
|
||||||
|
.arg(Arg::with_name("full_photo")
|
||||||
|
.short("f")
|
||||||
|
.long("full_photo")
|
||||||
|
.help("Allow users to see the full resolution pictures."))
|
||||||
|
.arg(Arg::with_name("nsfw")
|
||||||
|
.short("n")
|
||||||
|
.long("nsfw")
|
||||||
|
.help("Set the album as being Not Safe For Work."))
|
||||||
|
.arg(Arg::with_name("password")
|
||||||
|
.long("password")
|
||||||
|
.short("P")
|
||||||
|
.help("Set the password for the album. Leave empty for none.")
|
||||||
|
.value_name("PASSWORD"))
|
||||||
|
)
|
||||||
|
.subcommand(
|
||||||
|
SubCommand::with_name("add_photo")
|
||||||
|
.about("Upload a picture.")
|
||||||
|
.arg(Arg::with_name("album_id")
|
||||||
|
.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"))
|
||||||
|
)
|
||||||
|
.subcommand(
|
||||||
|
SubCommand::with_name("delete_photo")
|
||||||
|
.about("Delete some pictures.")
|
||||||
|
.arg(Arg::with_name("id")
|
||||||
|
.required(true)
|
||||||
|
.long("photo_id")
|
||||||
|
.short("i")
|
||||||
|
.value_name("IDs")
|
||||||
|
.help("The id of the picture(s) which you want to delete. For multiple ids, use commas."))
|
||||||
|
)
|
||||||
|
.subcommand(
|
||||||
|
SubCommand::with_name("move_photo")
|
||||||
|
.about("Move the picture to a given album.")
|
||||||
|
.arg(Arg::with_name("photo_id")
|
||||||
|
.required(true)
|
||||||
|
.long("photo_id")
|
||||||
|
.short("i")
|
||||||
|
.value_name("IDs")
|
||||||
|
.help("The id of the picture(s) which you want to move. For multiple ids, use commas."))
|
||||||
|
.arg(Arg::with_name("album_id")
|
||||||
|
.required(true)
|
||||||
|
.short("a")
|
||||||
|
.long("album_id")
|
||||||
|
.value_name("ID")
|
||||||
|
.help("The album id to move the picture(s) to."))
|
||||||
|
)
|
||||||
|
;
|
||||||
let matches = app.clone().get_matches();
|
let matches = app.clone().get_matches();
|
||||||
|
|
||||||
let c = read_config();
|
let c = read_config();
|
||||||
@ -96,7 +210,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();
|
||||||
@ -118,6 +231,35 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
} else if let Some(m) = matches.subcommand_matches("delete_album") {
|
} else if let Some(m) = matches.subcommand_matches("delete_album") {
|
||||||
let id = m.value_of("id").unwrap();
|
let id = m.value_of("id").unwrap();
|
||||||
delete_album(&client, &lychee_session_cookie, id).await;
|
delete_album(&client, &lychee_session_cookie, id).await;
|
||||||
|
} else if let Some(m) = matches.subcommand_matches("set_album_title") {
|
||||||
|
let t = m.value_of("title").unwrap();
|
||||||
|
let i = m.value_of("id").unwrap();
|
||||||
|
set_title_album(&client, &lychee_session_cookie, i, t).await;
|
||||||
|
} else if let Some(m) = matches.subcommand_matches("set_album_description") {
|
||||||
|
let d = m.value_of("description").unwrap();
|
||||||
|
let i = m.value_of("id").unwrap();
|
||||||
|
set_description_album(&client, &lychee_session_cookie, i, d).await;
|
||||||
|
} else if let Some(m) = matches.subcommand_matches("set_album_public") {
|
||||||
|
let i = m.value_of("id").unwrap();
|
||||||
|
let f = m.is_present("full_photo");
|
||||||
|
let p = m.is_present("public");
|
||||||
|
let n = m.is_present("nsfw");
|
||||||
|
let v = m.is_present("visible");
|
||||||
|
let d = m.is_present("downloadable");
|
||||||
|
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_or("0");
|
||||||
|
let i = m.value_of("photo_path").unwrap();
|
||||||
|
println!("{}", add_photo(&client, &lychee_session_cookie, a, i).await);
|
||||||
|
} else if let Some(m) = matches.subcommand_matches("delete_photo") {
|
||||||
|
let id = m.value_of("id").unwrap();
|
||||||
|
delete_photo(&client, &lychee_session_cookie, id).await;
|
||||||
|
} else if let Some(m) = matches.subcommand_matches("move_photo") {
|
||||||
|
let i = m.value_of("photo_id").unwrap();
|
||||||
|
let a = m.value_of("album_id").unwrap();
|
||||||
|
set_album_photo(&client, &lychee_session_cookie, i, a).await;
|
||||||
} else {
|
} else {
|
||||||
App::print_long_help(&mut app).unwrap();
|
App::print_long_help(&mut app).unwrap();
|
||||||
}
|
}
|
||||||
|
185
src/photo.rs
Normal file
185
src/photo.rs
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
use std::{io::{Write, Read}, fs::File, path::Path};
|
||||||
|
use hyper::{Method, Request, header::{COOKIE, CONTENT_TYPE, AUTHORIZATION}, Body};
|
||||||
|
use serde_json::json;
|
||||||
|
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")?;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_photo(c: &LycheeClient, l: &str, i: &str) {
|
||||||
|
let req = Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri(c.endpoint.to_string() + "/api/Photo::delete")
|
||||||
|
.header(COOKIE, l)
|
||||||
|
.header(AUTHORIZATION, c.api_key.to_string())
|
||||||
|
.header(CONTENT_TYPE, "application/json")
|
||||||
|
.body(Body::from(json!({"photoIDs": i}).to_string()))
|
||||||
|
.expect("Cannot request /api/Photo::delete.");
|
||||||
|
let res = c.client.request(req).await.unwrap();
|
||||||
|
let s = res.status();
|
||||||
|
let b = body_to_str(res.into_body()).await;
|
||||||
|
assert_eq!(b, "true", "The server didn't reply true: '{}'.", b);
|
||||||
|
assert_ne!(s, 500, "The server returned an internal error.");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_album_photo(c: &LycheeClient, l: &str, i: &str, a: &str) {
|
||||||
|
let req = Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri(c.endpoint.to_string() + "/api/Photo::setAlbum")
|
||||||
|
.header(COOKIE, l)
|
||||||
|
.header(AUTHORIZATION, c.api_key.to_string())
|
||||||
|
.header(CONTENT_TYPE, "application/json")
|
||||||
|
.body(Body::from(json!({"photoIDs": i, "albumID": a}).to_string()))
|
||||||
|
.expect("Cannot request /api/Photo::setAlbum.");
|
||||||
|
let res = c.client.request(req).await.unwrap();
|
||||||
|
let s = res.status();
|
||||||
|
let b = body_to_str(res.into_body()).await;
|
||||||
|
assert_eq!(b, "true", "The server didn't reply true: '{}'.", b);
|
||||||
|
assert_ne!(s, 500, "The server returned an internal error.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod photo_tests {
|
||||||
|
use crate::{LycheeClient, photo::{delete_photo, set_album_photo}};
|
||||||
|
use hyper_tls::HttpsConnector;
|
||||||
|
use hyper::Client;
|
||||||
|
use mockito::mock;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
fn setup() -> LycheeClient {
|
||||||
|
let https = HttpsConnector::new();
|
||||||
|
return LycheeClient {
|
||||||
|
client: Client::builder().build::<_, hyper::Body>(https),
|
||||||
|
endpoint: mockito::server_url(),
|
||||||
|
api_key: "value".to_string()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn set_album_photo_correct_values() {
|
||||||
|
let client = setup();
|
||||||
|
let m1 = mock("POST", "/api/Photo::setAlbum")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.match_body(json!({"photoIDs": "123", "albumID": "12"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_album_photo(&client, "v", "123", "12"));
|
||||||
|
m1.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn set_album_photo_false_returned() {
|
||||||
|
let client = setup();
|
||||||
|
let m1 = mock("POST", "/api/Photo::setAlbum")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("false")
|
||||||
|
.match_body(json!({"photoIDs": "123", "albumID": "1234"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_album_photo(&client, "v", "123", "1234"));
|
||||||
|
m1.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn set_album_photo_500_returned() {
|
||||||
|
let client = setup();
|
||||||
|
let m1 = mock("POST", "/api/Photo::setAlbum")
|
||||||
|
.match_header("cookie", "value")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.with_status(500)
|
||||||
|
.match_body(json!({"photoIDs": "123", "albumID": "12222"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(set_album_photo(&client, "value", "123", "12222"));
|
||||||
|
m1.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn delete_photo_correct_values() {
|
||||||
|
let client = setup();
|
||||||
|
let m1 = mock("POST", "/api/Photo::delete")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.match_body(json!({"photoIDs": "123"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(delete_photo(&client, "v", "123"));
|
||||||
|
m1.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn delete_photo_false_returned() {
|
||||||
|
let client = setup();
|
||||||
|
let m1 = mock("POST", "/api/Photo::delete")
|
||||||
|
.match_header("cookie", "v")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("false")
|
||||||
|
.match_body(json!({"photoIDs": "123"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(delete_photo(&client, "v", "123"));
|
||||||
|
m1.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn delete_photo_500_returned() {
|
||||||
|
let client = setup();
|
||||||
|
let m1 = mock("POST", "/api/Photo::delete")
|
||||||
|
.match_header("cookie", "value")
|
||||||
|
.match_header("content-type", "application/json")
|
||||||
|
.with_body("true")
|
||||||
|
.with_status(500)
|
||||||
|
.match_body(json!({"photoIDs": "123"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(delete_photo(&client, "value", "123"));
|
||||||
|
m1.assert();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user