|
|
@ -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, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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>")] |
|
|
|
#[post("/new", data = "<video>")] |
|
|
|
fn new(video: Form<Video>) -> Redirect { |
|
|
|
fn new(video: Form<Video>) -> Redirect { |
|
|
|
for part in video.link.split(" ") { |
|
|
|
println!("in new with video {}", video.link); |
|
|
|
if part.starts_with("https://"){ |
|
|
|
|
|
|
|
Command::new("/bin/env") |
|
|
|
Command::new("/bin/env") |
|
|
|
// .args(&[&video.link])
|
|
|
|
.args(&[ |
|
|
|
.args(&["DISPLAY=:0", "bash", "-c", &format!("mpv --ytdl-format='bestvideo[height<=?4050]+bestaudio/best' {link}", link=&part).to_string()]) |
|
|
|
"DISPLAY=:0", |
|
|
|
|
|
|
|
"bash", |
|
|
|
|
|
|
|
"-c", |
|
|
|
|
|
|
|
&format!( |
|
|
|
|
|
|
|
"mpv --ytdl-format='bestvideo[height<=?4050]+bestaudio/best' {link}", |
|
|
|
|
|
|
|
link = &video.link |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
]) |
|
|
|
.output() |
|
|
|
.output() |
|
|
|
.expect("Failed to execute command"); |
|
|
|
.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(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|