107 lines
2.6 KiB
Rust
107 lines
2.6 KiB
Rust
#![feature(proc_macro_hygiene, decl_macro)]
|
|
|
|
#[macro_use]
|
|
extern crate rocket;
|
|
|
|
use std::env;
|
|
use std::fmt;
|
|
use std::path::Path;
|
|
|
|
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: 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)
|
|
};
|
|
|
|
// 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)
|
|
};
|
|
|
|
// 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("/")
|
|
}
|
|
|
|
fn main() {
|
|
let env_config_path = match env::var("CONFIG_PATH") {
|
|
Ok(val) => val,
|
|
Err(_e) => panic!("Please provide a config path via the CONFIG_PATH env variable!"),
|
|
};
|
|
|
|
let config_path = Path::new(&env_config_path);
|
|
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();
|
|
}
|