make validation more smarter and rustier, might still be too complicated, but whatever.

master
CptCaptain 3 years ago
parent 7ef6d0e767
commit 55b3286bfa
  1. 4
      Cargo.toml
  2. 96
      src/main.rs

@ -7,9 +7,9 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rocket = "0.4.5" rocket = "0.4.10"
[dependencies.rocket_contrib] [dependencies.rocket_contrib]
version = "0.4.5" version = "0.4.10"
default-features = false default-features = false
features = ["serve"] features = ["serve"]

@ -1,31 +1,90 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
use std::env; use std::env;
use std::fmt;
use std::path::Path; use std::path::Path;
use std::process::Command; use rocket::request::FromFormValue;
use rocket::http::RawStr;
use rocket::http::uri::Absolute;
use rocket::request::Form; use rocket::request::Form;
use rocket_contrib::serve::StaticFiles;
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket_contrib::serve::StaticFiles;
use std::process::Command;
#[derive(Debug)]
struct Link {
uri: String
}
#[derive(FromForm)] #[derive(FromForm)]
struct Video { struct Video {
link: String, link: Link,
} }
#[post("/new", data = "<video>")] // Always use a limit to prevent DoS attacks.
fn new(video: Form<Video>) -> Redirect { const LIMIT: u64 = 1024;
for part in video.link.split(" ") {
if part.starts_with("https://"){ impl fmt::Display for Link {
Command::new("/bin/env") fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// .args(&[&video.link]) write!(f, "{}", self.uri)
.args(&["DISPLAY=:0", "bash", "-c", &format!("mpv --ytdl-format='bestvideo[height<=?4050]+bestaudio/best' {link}", link=&part).to_string()]) }
.output() }
.expect("Failed to execute command");
impl<'v> FromFormValue<'v> for Link {
type Error = &'v RawStr;
fn from_form_value(form_value: &'v RawStr) -> Result<Self, Self::Error> {
// Decode form data into string
let string = match form_value.url_decode() {
Ok(string) => string,
Err(_) => return Err(form_value)
};
dbg!(&string);
// Try to parse a hyperlink from the string
let mut abs_uri: Option<Absolute> = None;
for part in string.split(' ') {
println!("{}", part);
abs_uri = match Absolute::parse(part) {
Ok(abs_uri) => Some(abs_uri),
Err(_) => continue,
};
break;
} }
// Check for success
let result = match abs_uri {
Some(abs_uri) => Link{uri: abs_uri.to_string()},
_ => return Err(form_value)
};
dbg!(&result);
// Return successfully.
Ok(result)
} }
}
#[post("/new", data = "<video>")]
fn new(video: Form<Video>) -> Redirect {
println!("in new with video {}", video.link);
Command::new("/bin/env")
.args(&[
"DISPLAY=:0",
"bash",
"-c",
&format!(
"mpv --ytdl-format='bestvideo[height<=?4050]+bestaudio/best' {link}",
link = &video.link
),
])
.output()
.expect("Failed to execute command");
Redirect::to("/") Redirect::to("/")
} }
@ -36,13 +95,14 @@ fn main() {
}; };
let config_path = Path::new(&env_config_path); let config_path = Path::new(&env_config_path);
match env::set_current_dir(&config_path).is_ok() { if env::set_current_dir(&config_path).is_ok() {
true => println!("Config path set"), println!("Config path set");
false => panic!("Config path could not be set!"), } else {
}; panic!("Config path could not be set!");
}
rocket::ignite() rocket::ignite()
.mount("/", routes![new]) .mount("/", routes![new])
.mount("/", StaticFiles::from("static")) .mount("/", StaticFiles::from("static"))
.launch(); .launch();
} }

Loading…
Cancel
Save