Use iron instead of rocket

This commit is contained in:
Sakuhl 2018-01-28 16:29:53 +01:00
parent 89d16fa886
commit 1a56b8d709
2 changed files with 81 additions and 35 deletions

View File

@ -6,10 +6,10 @@ version = "0.1.0"
[dependencies]
base64 = "0.9.0"
image = "0.18.0"
iron = "0.6.0"
palette = "0.2.1"
regex = "0.2.5"
reqwest = "0.8.2"
rocket = "0.3.5"
rocket_codegen = "0.3.5"
serde_json = "1.0.9"

View File

@ -1,7 +1,15 @@
#![feature(plugin, custom_derive)]
#![plugin(rocket_codegen)]
//#![feature(plugin, custom_derive)]
//#![plugin(rocket_codegen)]
extern crate rocket;
//extern crate rocket;
extern crate iron;
use iron::prelude::*;
use iron::Handler;
use iron::Headers;
use iron::Url;
use iron::TypeMap;
use iron::status::Status;
use iron::response::WriteBody;
extern crate reqwest;
use reqwest::{Client, ClientBuilder};
@ -23,38 +31,16 @@ extern crate wynnautocrop;
use wynnautocrop::*;
use std::collections::HashMap;
use std::io::{Cursor, Read};
use std::env;
use std::io::{Cursor, Read, Write};
#[derive(FromForm)]
struct UrlForm {
url: String
}
#[get("/crop?<url>")]
fn crop_url(url: UrlForm) -> String {
let url = url.url;
if url.starts_with("https://postimg.org/gallery/") {
postimg_gallery(url)
} else {
let mut buf = Vec::new();
reqwest::get(&url).unwrap().read_to_end(&mut buf).unwrap();
let mut img = image::load(Cursor::new(&*buf), image::guess_format(&buf).unwrap()).unwrap();
img = crop(img);
let mut buf = Vec::new();
img.save(&mut buf, ImageFormat::PNG).unwrap();
//let handle = imgur::Handle::new("c819d6faf0219ac".to_owned());
//handle.upload(&buf).unwrap().link().unwrap().to_owned()
upload_image(&Client::new(), &buf)
}
}
fn postimg_gallery(url: String) -> String {
let html = reqwest::get(&url).unwrap().text().unwrap();
fn postimg_gallery(out: &mut Write, url: &str) -> Result<(), std::io::Error> {
let html = reqwest::get(url).unwrap().text().unwrap();
let regex = Regex::new(r"embed_value=(\{.+\});").unwrap();
let regex2 = Regex::new(r#"href="(.*\?dl=1)""#).unwrap();
let json = regex.captures(&html).unwrap().get(1).unwrap().as_str();
let data: serde_json::Value = serde_json::from_str(json).unwrap();
let mut acc = String::new();
//let mut acc = String::new();
let mut headers = header::Headers::new();
headers.set(header::UserAgent::new("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36".to_string()));
let client = ClientBuilder::new()
@ -82,8 +68,9 @@ fn postimg_gallery(url: String) -> String {
//acc += upload.link().unwrap();
let url = upload_image(&client, &buf);
println!("uploaded to {:?}", url);
acc += &url;
acc += " ";
out.write_all(url.as_bytes())?;
out.write_all(b" ")?;
out.flush()?;
break;
}
}
@ -93,7 +80,7 @@ fn postimg_gallery(url: String) -> String {
println!("retrying..");
}
}
acc
Ok(())
}
fn upload_image(client: &Client, buf: &[u8]) -> String {
@ -111,5 +98,64 @@ fn upload_image(client: &Client, buf: &[u8]) -> String {
}
fn main() {
rocket::ignite().mount("/", routes![crop_url]).launch();
let port = env::var("PORT").expect("no $PORT");
let mut router = Router::new();
router.add_route("crop".to_string(), |req: &mut Request| {
Ok(Response {
status: Some(Status::Ok),
headers: Headers::new(),
extensions: TypeMap::new(),
body: Some(Box::new(Writer(req.url.clone())))
})
});
Iron::new(router).http(&("0.0.0.0:".to_owned() + &port)).unwrap();
}
struct Writer(Url);
impl WriteBody for Writer {
fn write_body(&mut self, res: &mut Write) -> Result<(), std::io::Error> {
let url = &self.0.query().unwrap()[4..];
if url.starts_with("https://postimg.org/gallery/") {
postimg_gallery(res, url)?;
} else {
let mut buf = Vec::new();
reqwest::get(url).unwrap().read_to_end(&mut buf).unwrap();
let mut img = image::load(Cursor::new(&*buf), image::guess_format(&buf).unwrap()).unwrap();
img = crop(img);
let mut buf = Vec::new();
img.save(&mut buf, ImageFormat::PNG).unwrap();
//let handle = imgur::Handle::new("c819d6faf0219ac".to_owned());
//handle.upload(&buf).unwrap().link().unwrap().to_owned()
let image_url = upload_image(&Client::new(), &buf);
res.write_all(&image_url.as_bytes())?;
}
Ok(())
}
}
struct Router {
// Routes here are simply matched with the url path.
routes: HashMap<String, Box<Handler>>
}
impl Router {
fn new() -> Self {
Router { routes: HashMap::new() }
}
fn add_route<H>(&mut self, path: String, handler: H) where H: Handler {
self.routes.insert(path, Box::new(handler));
}
}
impl Handler for Router {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
match self.routes.get(&req.url.path().join("/")) {
Some(handler) => handler.handle(req),
None => Ok(Response::with(Status::NotFound))
}
}
}