make validation more smarter and rustier, might still be too complicated, but whatever.
This commit is contained in:
parent
7ef6d0e767
commit
55b3286bfa
@ -7,9 +7,9 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rocket = "0.4.5"
|
||||
rocket = "0.4.10"
|
||||
|
||||
[dependencies.rocket_contrib]
|
||||
version = "0.4.5"
|
||||
version = "0.4.10"
|
||||
default-features = false
|
||||
features = ["serve"]
|
||||
|
100
src/main.rs
100
src/main.rs
@ -1,31 +1,90 @@
|
||||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
|
||||
#[macro_use] extern crate rocket;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
use std::env;
|
||||
use std::fmt;
|
||||
use std::path::Path;
|
||||
|
||||
use std::process::Command;
|
||||
use rocket::request::Form;
|
||||
use rocket_contrib::serve::StaticFiles;
|
||||
use rocket::response::Redirect;
|
||||
use rocket::request::FromFormValue;
|
||||
use rocket::http::RawStr;
|
||||
|
||||
use rocket::http::uri::Absolute;
|
||||
use rocket::request::Form;
|
||||
use rocket::response::Redirect;
|
||||
use rocket_contrib::serve::StaticFiles;
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Link {
|
||||
uri: String
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct Video {
|
||||
link: String,
|
||||
link: Link,
|
||||
}
|
||||
|
||||
// Always use a limit to prevent DoS attacks.
|
||||
const LIMIT: u64 = 1024;
|
||||
|
||||
impl fmt::Display for Link {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.uri)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
for part in video.link.split(" ") {
|
||||
if part.starts_with("https://"){
|
||||
Command::new("/bin/env")
|
||||
// .args(&[&video.link])
|
||||
.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");
|
||||
}
|
||||
}
|
||||
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("/")
|
||||
}
|
||||
|
||||
@ -36,13 +95,14 @@ fn main() {
|
||||
};
|
||||
|
||||
let config_path = Path::new(&env_config_path);
|
||||
match env::set_current_dir(&config_path).is_ok() {
|
||||
true => println!("Config path set"),
|
||||
false => panic!("Config path could not be set!"),
|
||||
};
|
||||
if env::set_current_dir(&config_path).is_ok() {
|
||||
println!("Config path set");
|
||||
} else {
|
||||
panic!("Config path could not be set!");
|
||||
}
|
||||
|
||||
rocket::ignite()
|
||||
.mount("/", routes![new])
|
||||
.mount("/", StaticFiles::from("static"))
|
||||
.launch();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user