+ api::list_categories now re-added and working as intended

! This patch is massive but idc right now im sprinting to finish this conversion
This commit is contained in:
shockrah 2021-10-11 14:34:50 -07:00
parent e4373560a4
commit b763329abd
2 changed files with 58 additions and 53 deletions

View File

@ -4,6 +4,7 @@ use std::env;
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
mod page; mod page;
mod video;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -15,6 +16,7 @@ async fn main() {
*/ */
let _ = rocket::build() let _ = rocket::build()
.mount("/", routes![page::home, page::category, page::video, page::files]) .mount("/", routes![page::home, page::category, page::video, page::files])
.mount("/api", routes![video::list_categories])
.attach(Template::fairing()) .attach(Template::fairing())
.launch().await; .launch().await;
} }

View File

@ -1,78 +1,81 @@
use rocket_contrib::json;
use rocket_contrib::json::JsonValue;
use serde::Serialize;
use base64; use base64;
use std::path::Path; use serde::Serialize;
use std::{fs, env}; use std::fs::DirEntry;
use std::fs::File; use std::{io, env};
use std::io::prelude::*; use rocket::serde::json::Json;
#[derive(Serialize)] #[derive(Serialize)]
struct Category { pub struct Category {
name: String, name: String,
thumbnail: Option<String> thumbnail: Option<String>
} }
#[derive(Serialize)] #[derive(Serialize)]
struct Video { struct List {
name: String, categories: Vec<Category>
category: String,
thumbnail: Option<String>
} }
fn get_nail(dirpath: &Path) -> std::io::Result<Option<String>> {
let mut t_path = dirpath.to_path_buf(); t_path.push(".thumbnail.jpg");
if t_path.is_file() {
fn get_category_metadata(entry: DirEntry) -> io::Result<(String, Option<String>)> {
use std::io::Read;
let name = entry.file_name().to_string_lossy().to_string();
let mut p = entry.path(); p.push(".thumbnail.jpg");
if p.is_file() {
let mut buf = Vec::new(); let mut buf = Vec::new();
let mut file = File::open(t_path)?; let mut file = std::fs::File::open(p)?;
file.read_to_end(&mut buf)?; file.read_to_end(&mut buf)?;
return Ok(Some(base64::encode(buf).to_string()));
let nail = base64::encode(buf).to_string();
return match nail.len() {
0 => Ok((name, None)),
_ => Ok((name, Some(nail)))
};
} }
Ok(None) return Ok((String::new(), None));
}
fn get_category_dirs(path: &str) -> std::io::Result<Vec<DirEntry>> {
let path = std::path::Path::new(path);
if !path.is_dir() {
panic!("<{:?}> is not a valid directory", path);
}
let mut ret: Vec<DirEntry> = Vec::new();
for entry in std::fs::read_dir(path)? {
if let Ok(entry) = entry {
ret.push(entry)
}
}
return Ok(ret);
} }
#[get("/categories")] #[get("/categories")]
pub fn list_categories() -> JsonValue { pub async fn list_categories() -> Json<Vec<Category>> {
// WARN: misconfigured servers are just going to get shafted and serve up
// a tonne of 500's
let dir = match env::var("CLIPS_DIR") { let dir = match env::var("CLIPS_DIR") {
Ok(val) => val, Ok(val) => val,
Err(_) => "/media/clips/".to_string() Err(_) => "/media/clips/".to_string()
}; };
match get_category_dirs(&dir) {
// Fucking hell this is going to be dumb Ok(entries) => {
let mut cats: Vec<Category> = Vec::new(); let mut cats: Vec<Category> = Vec::new();
for file in fs::read_dir(dir).unwrap() { for ent in entries {
match file { match get_category_metadata(ent) {
Ok(file) => { Ok((name, thumbnail)) => {
let name = file.file_name() cats.push(Category {name, thumbnail});
.into_string().unwrap_or(String::new());
let nail = get_nail(file.path().as_ref());
cats.push(Category { name, thumbnail: nail.unwrap_or(None) });
}, },
_ => continue _ => continue
} }
} }
return Json(cats);
json!({"categories": cats})
}
#[get("/category/<cat>")]
pub fn list_videos(cat: String) -> JsonValue {
let dir = match env::var("CLIPS_DIR") {
Ok(val) => val,
Err(_) => "/media/clips".to_string()
};
let mut vids: Vec<Video> = Vec::new();
for file in fs::read_dir(dir).unwrap() {
match file {
Ok(file) => {
let name = file.file_name()
.into_string().unwrap_or(String::new());
vids.push(Video {name, category: cat.clone(), thumbnail: None})
}, },
_ => continue Err(e) => {
eprintln!("ERROR: {}", e);
} }
} }
return json!({"videos": vids}) Json(Vec::new())
} }