Compare commits
4 Commits
e1fcfd1b22
...
3f804a388a
Author | SHA1 | Date |
---|---|---|
|
3f804a388a | |
|
cd9dd8b34b | |
|
4fadbc2d4b | |
|
23167ae077 |
|
@ -375,7 +375,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "imgurx"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"quick-xml",
|
||||
"reqwest",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "imgurx"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
authors = ["blank X <theblankx@protonmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -31,11 +31,21 @@ async fn async_main(port: u16) {
|
|||
.and(client.clone())
|
||||
.and_then(handle_album);
|
||||
let media_path = warp::path!(String).and(client).and_then(handle_media);
|
||||
let root_handler = warp::path::end().map(|| warp::reply::html(
|
||||
format!("<html><head><title>ImgurX v{0}</title><style>body {{ background-color: black; color: black; text-align: center; }}\ndiv {{ border: 1px solid #008; background: #eef; padding: 0.5em 1em 0.5em 1em; }}</style></head><body><div><b><a href=\"https://gitlab.com/blankX/imgurx\" style=\"text-decoration: none;\">ImgurX v{0}</a></b><br>An alternative JS-less Imgur frontend</div></body></html>", env!("CARGO_PKG_VERSION"))
|
||||
));
|
||||
let index_handler = warp::path("index.html").and(root_handler);
|
||||
let not_found_handler = warp::any().map(|| warp::reply::with_status(
|
||||
warp::reply::html("<html><head><style>body { background-color: black; color: white; text-align: center; }\ndiv { border: 1px solid #fcc; background: #fee; padding: 0.5em 1em 0.5em 1em; color: black; }</style></head><body><div><b>404: Not Found</b></div></body></html>"),
|
||||
warp::reply::html("<html><head><title>404: Not Found</title><style>body { background-color: black; color: white; text-align: center; }\ndiv { border: 1px solid #fcc; background: #fee; padding: 0.5em 1em 0.5em 1em; color: black; }</style></head><body><div><b>404: Not Found</b></div></body></html>"),
|
||||
404.try_into().unwrap(),
|
||||
));
|
||||
let routes = warp::filters::method::get().and(album_path.or(media_path).or(not_found_handler));
|
||||
let routes = warp::filters::method::get().and(
|
||||
album_path
|
||||
.or(index_handler)
|
||||
.or(media_path)
|
||||
.or(root_handler)
|
||||
.or(not_found_handler),
|
||||
);
|
||||
eprintln!("Serving on 0.0.0.0:{}", port);
|
||||
warp::serve(routes).run(([0u8, 0, 0, 0], port)).await;
|
||||
}
|
||||
|
|
|
@ -46,16 +46,6 @@ impl From<serde_json::Error> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
//impl fmt::Display for Error {
|
||||
// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
// formatter.write_str(&match self {
|
||||
// Error::Reqwest(err) => format!("reqwest: {}", err),
|
||||
// Error::SerdeJSON(err) => format!("serde_json: {}", err),
|
||||
// Error::ApiErrors(err) =>
|
||||
// })
|
||||
// }
|
||||
//}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct APIErrors {
|
||||
pub errors: Vec<APIError>,
|
||||
|
|
26
src/utils.rs
26
src/utils.rs
|
@ -48,6 +48,24 @@ pub fn generate_html(album: Result<Album, Error>) -> (u16, Vec<u8>) {
|
|||
let elem = BytesStart::owned(b"head".to_vec(), 4);
|
||||
writer.write_event(Event::Start(elem)).unwrap();
|
||||
|
||||
let (show_details, title) = match album {
|
||||
Ok(ref album) if !album.title.is_empty() => (false, Some(album.title.clone())),
|
||||
Err(Error::APIErrors(ref err)) if err.errors.len() == 1 && err.errors[0].code == "404" => {
|
||||
(false, Some(format!("404: {}", &err.errors[0].detail)))
|
||||
}
|
||||
_ => (true, None),
|
||||
};
|
||||
if let Some(title) = title {
|
||||
let elem = BytesStart::owned(b"title".to_vec(), 5);
|
||||
writer.write_event(Event::Start(elem)).unwrap();
|
||||
|
||||
let elem = BytesText::from_plain_str(&title);
|
||||
writer.write_event(Event::Text(elem)).unwrap();
|
||||
|
||||
let elem = BytesEnd::owned(b"title".to_vec());
|
||||
writer.write_event(Event::End(elem)).unwrap();
|
||||
}
|
||||
|
||||
let elem = BytesStart::owned(b"style".to_vec(), 5);
|
||||
writer.write_event(Event::Start(elem)).unwrap();
|
||||
|
||||
|
@ -155,14 +173,6 @@ pub fn generate_html(album: Result<Album, Error>) -> (u16, Vec<u8>) {
|
|||
200
|
||||
}
|
||||
Err(err) => {
|
||||
let show_details = match err {
|
||||
Error::APIErrors(ref err)
|
||||
if err.errors.len() == 1 && err.errors[0].code == "404" =>
|
||||
{
|
||||
false
|
||||
}
|
||||
_ => true,
|
||||
};
|
||||
let mut elem = BytesStart::owned(b"div".to_vec(), 3);
|
||||
if show_details {
|
||||
elem.push_attribute(("style", "text-align: left;"));
|
||||
|
|
Loading…
Reference in New Issue